[英]Java Arrays - Where too put Strings? Why doesn't it work?
My program should look like this:我的程序应该是这样的:
How many random numbers between 0 - 999 do you want?
你想要多少个 0 - 999 之间的随机数? 12 (user input)
12 (用户输入)
Here are the random numbers: 145 538 56 241 954 194 681 42 876 323 2 87
以下是随机数:145 538 56 241 954 194 681 42 876 323 2 87
These 7 numbers are even: 538 56 954 194 42 876 2
这 7 个数字是偶数: 538 56 954 194 42 876 2
These 5 numbers are odd: 145 241 681 323 87
这 5 个数字是奇数:145 241 681 323 87
I cannot seem too place the String
s ( "These 7 numbers are even"
and "These 5 numbers are odd"
at the right spot in the code. And how do I print the exact number ( 7
and 5
) of even and odd numbers that are displayed in the String
? The code does not seem to work either way for me. Do you see the problem(s)? Well I hope you can help me figure this out, have been doing this for 12 hours straight now.我似乎不能在代码的正确位置放置
String
s( "These 7 numbers are even"
和"These 5 numbers are odd"
。我如何打印偶数和奇数的确切数字( 7
和5
)显示在String
?代码似乎对我不起作用。你看到问题了吗?我希望你能帮我解决这个问题,我已经连续做了 12 个小时了。
Here is my code:这是我的代码:
import java.util.Scanner;
public class SlumpadeTal {
public static void main(String[] args) {
int evenCounter = 0;
int oddCounter = 0;
Scanner input = new Scanner(System.in);
System.out.println("How many random numbers between 0 - 999 do you want?");
int antal = input.nextInt();
System.out.println("Here are the random numbers:");
int[] arrayen = new int[antal];
for (int i = 0; i < arrayen.length; i++) {
arrayen[i] = (int) (Math.random() * 999 + 1);
System.out.print(arrayen[i] + " ");
if ((arrayen[i] % 2) == 0) {
evenCounter++;
}
else {
oddCounter++;
}
}
int[] evenArray = new int [evenCounter];
int[] oddArray = new int[oddCounter];
evenCounter = 0;
oddCounter = 0;
for (int i = 0; i < arrayen.length; i++){
if ((arrayen[i] % 2) == 0) {
evenArray[evenCounter] = arrayen[i];
evenCounter++;
}
else {
oddArray[oddCounter] = arrayen[i];
oddCounter++;
}
}
}
}
NOTE I cannot use any class such as ArrayList
, Vector
and so on.注意我不能使用任何类,例如
ArrayList
、 Vector
等。
When evenArray & oddArray have values after the last for-loop finished.当evenArray 和oddArray 在最后一个for 循环完成后有值时。 You just use 2 another loop to print this 2 array.
您只需使用另外 2 个循环来打印这 2 个数组。
7 is evenCounter + 1 7是偶数计数器 + 1
5 is oddCounter + 1 5是奇数计数器 + 1
Ex: String s = "These " + (evenCounter + 1) + "numbers are even: " .... print your array.例如:字符串 s = "这些 " + (evenCounter + 1) + "数字是偶数:" .... 打印你的数组。
Just for your output:只为你的输出:
System.out.print("These "+evenCounter+" numbers are even: ");
for (int i = 0; i < evenCounter; i++){
System.out.print(evenArray[i]+" ");
}
System.out.println();
System.out.print("These "+oddCounter+" numbers are odd: ");
for (int i = 0; i < oddCounter; i++){
System.out.print(oddArray[i]+" ");
}
System.out.println();
Could work, there are other solutions.可以工作,还有其他解决方案。 For example:
例如:
String evStr=new String();
String odStr=new String();
int curNum=0;
for (int i = 0; i < antal; i++) {
curNum = (int) (Math.random() * 999 + 1);
System.out.print(curNum + " ");
if ((curNum % 2) == 0) {
evStr+=" "+curNum;
evenCounter++;
}
else {
odStr+=" "+curNum;
oddCounter++;
}
}
System.out.println();
System.out.println("These "+evenCounter+" numbers are even: "+evStr);
System.out.println("These "+oddCounter+" numbers are odd: "+odStr);
So the whole second part with even/odd arrays of your code disappears.因此,带有偶数/奇数代码数组的整个第二部分消失了。 And there are no arrays at all any more.
并且根本没有数组了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.