JAVA基础学习(5)之数组

5数组

  • 5.1数组
    • 5.1.1初识数组
      •   
         1 //        输出大于平均数的所有数
         2         Scanner in = new Scanner(System.in);
         3         int n;
         4         int[] a = new int[100];
         5         int count = 0;
         6         double sum = 0;
         7         while ((n = in.nextInt()) != -1)
         8         {
         9             a[count] = n;
        10             sum += n;
        11             count++;
        12         }
        13         if (count > 0)
        14         {
        15 
        16             for (int i = 0; i < count; i++)
        17             {
        18                 if (a[i] > (sum / count))
        19                 {
        20                     System.out.println(a[i]);
        21                 }
        22             }
        23             System.out.println("平均数为:" + sum / count);
        24         }
    • 5.1.2创建数组
    • 5.1.3数组的元素
      • 数组的大小不能改变
    • 5.1.4数组变量
      • 普通变量所有者
      • 数组变量管理者
      •  1 int[] a = new int[10];
         2         a[0] = 5;
         3         int[] b = a;
         4         System.out.println(a[0]);
         5         System.out.println(b[0]);
         6         for (int i = 0; i < b.length; i++)
         7         {
         8             if (i == b.length - 1)
         9             {
        10                 System.out.println(b[i]);
        11                 break;
        12             }
        13             System.out.print(b[i]);
        14         }
        15         System.out.println(a);
        16         System.out.println(b);
  • 5.2数组计算
    • 5.2.1投票统计
      •   
         1 // 投票统计
         2         Scanner in = new Scanner(System.in);
         3         int x;
         4         int[] numbers = new int[10];
         5         while ((x = in.nextInt()) != -1)
         6         {
         7             if ((x >= 0) && (x <= 9))
         8             {
         9                 numbers[x]++;
        10             }
        11         }
        12         for (int i = 0; i < numbers.length; i++)
        13         {
        14             System.out.println(i + ": " + numbers[i]);
        15         }
    • 5.2.2遍历数组
      •   
         1 // 遍历数组
         2         Scanner in = new Scanner(System.in);
         3         int x = in.nextInt();
         4         int[] a =
         5         { 3, 4, 5, 6, 7, 8, 32, 45 };
         6         int loc = -1;
         7         boolean flag = false;
         8         for (int i = 0; i < a.length; i++)
         9         {
        10             if (x == a[i])
        11             {
        12                 loc = i;
        13                 break;
        14             }
        15         }
        16         for (int k : a)
        17         {
        18             if (k == x)
        19             {
        20                 flag = true;
        21             }
        22         }
        23         if (flag)
        24         {
        25             System.out.println(x + "存在");
        26             System.out.println(x + "在第" + (loc + 1) + "个");
        27         } else
        28         {
        29             System.out.println(x + "不在其中");
        30         }
    • 5.2.3素数
      •   
         1 // 素数
         2         Scanner in = new Scanner(System.in);
         3         System.out.println("请输入一个数:");
         4         int x = in.nextInt();
         5         boolean isPrime = true;
         6         if (x == 1 || x % 2 == 0 && x != 2)
         7         {
         8             isPrime = false;
         9         } else
        10         {
        11             for (int i = 3; i < Math.sqrt(x); i+=2)
        12             {
        13                 if (x%i==0)
        14                 {
        15                     isPrime=false;
        16                     break;
        17                 }
        18             }
        19         }
        20         if (isPrime)
        21         {
        22             System.out.println(x+"是素数");
        23         }else
        24         {
        25             System.out.println(x+"不是素数");
        26         }
  • 5.3二维数组
    • OX棋
原文地址:https://www.cnblogs.com/quxiangjia/p/12003945.html