數(shù)組練習(xí)

計(jì)算全班學(xué)員的平均分

方法一
     int[] a = {60, 70, 80, 90};
        int b = 0;
        int s = 0;
        for (int i = 0; i < a.length; i++) {
            b = a[i];
            s += a[i];
            System.out.println(b);
        }

        System.out.println("平均成績(jī)是:" + s / a.length);
方法二
  Scanner scanner = new Scanner(System.in);
        double[] a = new double[5];
        double d=0;
        for (int i = 0;i<a.length;i++){
            System.out.println("輸入成績(jī)");
            double g=scanner .nextInt();
            a[i]=g;
            d+=a[i];
        }
        System.out.println("平均分是"+d/a.length);

有一個(gè)數(shù)列:8,4,2,1,23,344,12 循環(huán)輸出數(shù)列的值,求數(shù)列中所有數(shù)值的和猜數(shù)游戲:從鍵盤中任意輸入一個(gè)數(shù)據(jù),判斷數(shù)列中是否包含此數(shù)

 Scanner scanner = new Scanner(System.in);
  int[] a = {8, 4, 2, 1, 23, 344, 12};
        int b = 0;
        System.out.println("輸入一個(gè)數(shù)");
        int c = scanner.nextInt();
        int i;
        for (i = 0; i < a.length; i++) {
            b += a[i];
            if (c == a[i]) {
                System.out.println("包含");
                break;
            }
        }
        if (i == a.length) {
            System.out.println("不包含");
        }
        System.out.println(b);

從鍵盤輸入本次Java考試五位學(xué)生的成績(jī),求考試成績(jī)最高分

   Scanner scanner = new Scanner(System.in);
  int[] a = new int[5];
        int max = 0;
        int i;
        for (i = 0; i < a.length; i++) {
            System.out.println("輸入成績(jī)");
            int g = scanner.nextInt();
            a[i] = g;
            if (max <= a[i]) {
                max = a[i];
            }
        }
        System.out.println("最大成績(jī)是" + max);

有一組學(xué)員的成績(jī){50,90,70,80},將它們按升序排列。要增加一個(gè)學(xué)員的成績(jī),將它插入成績(jī)序列,并保持升序。

int[] a = {50, 90, 70, 80};
        Arrays.sort(a);
        System.out.println("數(shù)");
        int b = scanner.nextInt();
        int c = 0;
        for (int i = 0; i < a.length; i++) {
            if (b <= a[i]) {
                c = i;
                break;
            }
        }
        int[] d = new int[5];
      //插入第一步  for (int i = 0; i < c; i++) {
            d[i] = a[i];
        }
     //第二部   d[c] = b;
       //第三部 for (int i = c + 1; i < d.length; i++) {
            d[i] = a[i - 1];
        }
       //遍歷 for (int i = 0; i < d.length; i++) {
            System.out.println(d[i]);
        }

將 一組亂序的字符進(jìn)行排序 進(jìn)行升序和逆序輸出

       char[] a={'s','d','b','h'};
                Arrays.sort(a);
        for (int i = 0;i<a.length;i++){
            System.out.println(a[i]);
        }
                for (int i = a.length-1;i>=0;i--){
                     System.out.print(a[i]);
        }

定義一個(gè)字符串?dāng)?shù)組,查找某個(gè)字符串在數(shù)組中出現(xiàn)的次數(shù)

  Scanner scanner = new Scanner(System.in);
  String[] a = {"ww", "dd", "ww", "aa", "dd", "ww"};
        int c = 0;
        System.out.println("輸入字符串");
        String b = scanner.next();
        for (int i = 0; i < a.length; i++) {

            if (b.equals(a[i])) {
                c++;
            }

        }
        System.out.println(b + "出現(xiàn)" + c + "次");
  
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 二維數(shù)組 : 存儲(chǔ)類型 數(shù)據(jù)類型 數(shù)組名[行數(shù)][列數(shù)] 列數(shù)不能夠省略 , 行數(shù)可以省略 數(shù)組元素的引用 : ...
    ibo閱讀 347評(píng)論 0 1
  • 1.將放在字符串?dāng)?shù)組中的M(M<=5)個(gè)字符串(每串的長(zhǎng)度不超過N(N<=10)),按順序合并組成一個(gè)新的字符串。...
    Micason閱讀 561評(píng)論 0 0
  • importFoundation println("Hello, World!") //有如下一個(gè)數(shù)組,["typ...
    冰凡513閱讀 235評(píng)論 0 0
  • 他,肥頭大耳(至少在我們宿舍算是吧),個(gè)子不高,但是絲毫不影響他北方人的豪爽大氣。如果沒有記錯(cuò),他是除我和小萌子之...
    蔥蔥躺過人生河閱讀 685評(píng)論 0 2
  • 海灘的一粒沙,靜靜的用她赤誠(chéng)的心,仰望天空,可是,風(fēng)想把沙粒卷走,他(她)用謠言或謊言吹皺晴朗的晴空,縱使天...
    lhm2008lhm閱讀 204評(píng)論 0 0