我似乎无法弄清楚如何让它查找数组中的任何随机数并检查它们是否可被用户输入整除。它只是给了我
的恒定结果"可分割的总分数:(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+")");
}
}
}
}
答案 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)
您知道您的代码在做什么吗?这是一个简短的描述:
System.out.println("Please enter the 16 numbers to be checked for division!"); strings[i][j]=keyboard.nextInt();
strings[i][j] = rand.nextInt();
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+")");
}
}