我在完整打印阵列时遇到问题。设置代码以打印随机数序列,并且随机数序列(1-12)的数量旨在来自用户输入。但是,我不能让它打印超过第一个随机数。我之前设法打印了整个序列,但由于这是一个赋值而我必须使用数组,我的第一个方法不再有效。
import java.util.Scanner;
import java.util.Arrays;
public class Test4 {
public static void main(String[] args) {
int randomNumber;
int[] allNumbers = new int[12];
Scanner keyboard = new Scanner(System.in);
for (int i = 1; i <= allNumbers.length; i++) {
allNumbers[i] = (int) (Math.random() * 999 + 1);
System.out.print("How many random numbers? ");
randomNumber = keyboard.nextInt();
System.out.print("\n" + "Random numbers: ");
System.out.print(allNumbers[i] + " ");
{
System.out.print("\n");
System.out.print("\n" + "Even numbers: " + (allNumbers[i] % 2 == 0));
System.out.print("\n" + "Odd numbers: ");
break;
}
}
}
}
新代码:正在进行中......
* Update2:增加了对keyboard.nextint的限制。
在我尝试打印赔率和赔率的部分遇到“字符串文字没有被双引号关闭”错误。
* Update3:上面修正了错误(由拼写错误引起)。
新问题:已排序的数字(奇数/偶数)显示,但格式错误。它们显示如下: 奇数:475 奇数:123 甚至:62 甚至:680 甚至:870 奇:457 等等 我想要: 偶数:号码号码 奇数:号码号码
还想在那里放一个计数器,计算偶数/奇数随机数的数量 - 是否有办法在一次打印中打印所有这些?或者我首先必须制作一个system.out.print,后面跟一个系统输出 f 作为计数器?
更新代码:
import java.util.Scanner;
import java.util.Arrays;
public class Test7 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("How many random numbers?: ");
int size = keyboard.nextInt();
System.out.print("\n");
while (size>12 || size<1);
int[] randNum = new int[size];
for(int i = 0 ; i < randNum.length ; i++)
randNum[i] = (int)(Math.random() * 999 + 1);
System.out.println("Random numbers: " + Arrays.toString(randNum).replace("]", "").replace(",", "").replace("[", "") + "\n");
for(int i = 0 ; i < randNum.length ; i++)
if(randNum[i] % 2 == 0) {
System.out.print("\r" + "Even numbers: ");
System.out.print(randNum[i] + " ");
} else {
System.out.print("\r" + "Odd numbers: ");
System.out.print(randNum[i] + " ");
}
}}
答案 0 :(得分:2)
Java数组使用基于零的索引。
for(int i = 0; i < allNumbers.length ; i++)
因此,for循环应如上所述。否则你应该得到ArrayIndexOutOfBoundException。
此外,for循环中包含的break
语句会导致循环不能运行多次。您应该始终使用带有条件的 break 语句(例如:在if块中)
答案 1 :(得分:1)
Imesha所说的一切加上这句话:
{
System.out.print("\n");
System.out.print("\n" + "Even numbers: " + (allNumbers[i] % 2 == 0));
System.out.print("\n" + "Odd numbers: ");
break;
}
System.out.print("\n" + "Even numbers: " + (allNumbers[i] % 2 == 0));
//will print true or false, if you want to display the even and odd number, you should try something like this :
if(allNumbers[i] % 2 == 0) {
System.out.println("Even numbers " + allNumbers[i]");
} else {
System.out.println("Odd numbers " + allNumbers[i]");
}
答案 2 :(得分:1)
首先,如果您希望用户选择随机数的数量,则必须在创建allNumbers
数组之前获得此数字。
其次,就像Imesha Sudasingha所说的那样,Java是基于零的,因此需要将int i初始化为0以避免ArrayIndexOutOfBoundException
。
第三,for
循环最后一部分的括号在这里注明了你。可以在不更改结果的情况下删除它们。
此外,您最后会在一次迭代后基本停止for
循环。
我建议您从头开始,尝试按此顺序设置程序:
allNumbers
数组。 for
循环,仅将随机数分配给allNumbers
数组。for
循环以打印结果。 答案 3 :(得分:0)
请仔细阅读以下代码。看看你在这一行System.out.print(allNumbers[i] + " ");
之后开始的块,看来你的概念需要一些清理。这些评论可以帮助您更好地理解。
由于你没有提到关于偶数/奇数的东西,我推断你正在尝试计算偶数和奇数的数量,正如你将在代码中找到的那样。如果需要改变,请告诉我。
如果有任何问题,请告诉我。
public static void main(String[] args) {
// how many random numbers
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter array size: ");
int size = Integer.parseInt( keyboard.next() );
int[] randNum = new int[size];
// fill array with random numbers
for(int i = 0 ; i < randNum.length ; i++)
randNum[i] = (int)(Math.random() * 999 + 1);
// to print the array, put import java.util.Arrays; at the top
System.out.println(Arrays.toString(randNum));
keyboard.close();
// go over elements looking for even/odd numbers
// by going over the list 2 times, you can print the even in the
// first iteration, odd in the second iteration
// i am using auxiliary lists
// ZERO-th WAY
List<Integer> even = new ArrayList<Integer>();
List<Integer> odd = new ArrayList<Integer>();
for(int i = 0 ; i < randNum.length ; i++) {
// even number found
if(randNum[i] % 2 == 0)
even.add(randNum[i]);
else
odd.add(randNum[i]);
}
System.out.println("Even: " + even);
System.out.println("Odd: " + odd);
// FIRST WAY
// go over elements looking for even/odd numbers
// by going over the list 2 times, you can print the even in the
// first iteration, odd in the second iteration
System.out.print("Even: ");
for(int i = 0 ; i < randNum.length ; i++) {
// even number found
if(randNum[i] % 2 == 0)
System.out.print(randNum[i] + " ");
}
System.out.println();
System.out.print("Odd: ");
for(int i = 0 ; i < randNum.length ; i++) {
// even number found
if(randNum[i] % 2 == 1)
System.out.print(randNum[i] + " ");
}
System.out.println();
// SECOND WAY
int evenCount = 0, oddCount = 0;
for(int i = 0 ; i < randNum.length ; i++) {
// even number found
if(randNum[i] % 2 == 0)
evenCount++;
else
oddCount++;
}
int[] evenNums = new int[evenCount];
int[] oddNums = new int[oddCount];
int evenNumsIndx = 0, oddNumsIndx = 0;
for(int i = 0 ; i < randNum.length ; i++) {
// even number
if(randNum[i] % 2 == 0) {
evenNums[evenNumsIndx] = randNum[i];
evenNumsIndx++;
} else {
oddNums[oddNumsIndx] = randNum[i];
oddNumsIndx++;
}
}
System.out.println("Even: " + Arrays.toString(evenNums));
System.out.println("Odd: " + Arrays.toString(oddNums));
}
示例输出:
Enter array size: 12
[435, 420, 334, 248, 143, 340, 233, 940, 187, 315, 228, 132]
Even: [420, 334, 248, 340, 940, 228, 132]
Odd: [435, 143, 233, 187, 315]