2D阵列分区代码出错?

时间:2016-02-29 08:38:15

标签: java arrays

我似乎无法弄清楚如何让它查找数组中的任何随机数并检查它们是否可被用户输入整除。它只是给了我

的恒定结果
  

"可分割的总分数:(0)"

import java.util.Random;
import java.util.Scanner;
public class Divisor
{
    public static void main (String []args)
    {
        Scanner keyboard = new Scanner(System.in);
        Random rand = new Random();
        int[][]strings = new int [4][4];
        for (int i = 0; i < strings.length;i++)
        {
           for(int j = 0; j < strings.length;j++)
           {
               System.out.println("Please enter the 16 numbers to be checked for division!");
               strings[i][j]=keyboard.nextInt();
           }
        }
        int count = 0;
        for (int i = 0; i < strings.length;i++)
        {
            for(int j = 0; j < strings[i].length;j++)
            {
              strings[i][j] = 1 + (int)(Math.random()*100);
                if (strings[i][j]%keyboard.nextInt()==0)
                {
                    count++;
                }
                System.out.println("Number of total divisible inputted numbers: "+"("+count+")");
            }

        }
    }
}

3 个答案:

答案 0 :(得分:1)

你可能想要这样的东西:

import java.util.Random;
import java.util.Scanner;
public class Divisor
{
    public static void main (String []args)
    {
        Scanner keyboard = new Scanner(System.in);
        Random rand = new Random();
        int[][]strings = new int [4][4];
        int input = 0;

        // ask the user for input
       System.out.println("Please enter the 16 numbers to be checked for division!");
       input=keyboard.nextInt();

        // generate random numbers
        for (int i = 0; i < strings.length;i++)
        {
            for(int j = 0; j < strings[i].length;j++)
            {
                strings[i][j] = 1 + rand.nextInt(100);
                //System.out.println(strings[i][j]); // print what is created for testing
            }
        }

        // count divisible numbers for each input
        int count = 0;
        for (int i = 0; i < strings.length; i++)
        {
            for (int j = 0; j < strings[i].length; j++)
            {
                if (input != 0 && strings[i][j] % input == 0) count++;
            }
        }
        System.out.println("Number of total divisible inputted numbers: "+"("+count+")");
    }
}

答案 1 :(得分:0)

您知道您的代码在做什么吗?这是一个简短的描述:

  1. 让用户在数组中输入16个数字。
  2.   System.out.println("Please enter the 16 numbers to be checked for division!");
          strings[i][j]=keyboard.nextInt();
    
    1. 使用Random.nextInt()随机int覆盖每个数字。
    2.     strings[i][j] = rand.nextInt();
      
      1. 比较新的随机数(不相同)是否可被用户输入的数字整除。
      2. if (rand.nextInt()%keyboard.nextInt()==0)
        

        你看到了问题吗?

答案 2 :(得分:0)

   public class Divisor
    {
        public static void main (String []args)
        {
            Scanner keyboard = new Scanner(System.in);
            Random rand = new Random();

            int[][]strings = new int [4][4];  //Strange calling an array of ints for "strings".

            for (int i = 0; i < strings.length;i++)
            {
                for(int j = 0; j < strings[i].length;j++)
                {
                    strings[i][j] = rand.nextInt();     // Lets add some random number to a 4x4 array.
                }
            }

            int count = 0;
            int comparable = keyboard.nextInt();       // We are looking for this number.

            for (int i = 0; i < strings.length;i++)
            {
                for(int j = 0; j < strings[i].length;j++)
                {

                    if (strings[i][j] % comparable == 0)     //Does the number match the one we are looking for?
                    {
                        count++;
                        System.out.println("Found a number!");
                    }

                }

            }
            //We have now looked in the entire array
            System.out.println("Number of total divisible inputted numbers: "+"("+count+")");
        }
    }