英文:
Setting a number in range in arrays for java
问题
以下是您要翻译的代码部分:
在编写一个用于打印从0到99范围内的随机数的代码时,我遇到了一些困难。我尝试将for语句中的条件更改为e<100,然而出现了一个错误。我应该在哪里设置条件,以便我的输出显示0到99之间的数字?
public class EvensOdds {
public static void printArray(int[] ear) {
System.out.println("奇数:");
for (int e = 0; e<ear.length ; e ++) {
ear[e] = (int)(Math.random()* 100);
if(ear[e]%2!=0)
System.out.print(ear[e] + " ");
}
System.out.println("\n" + "偶数:");
for (int e = 0; e<ear.length ; e ++) {
ear[e] = (int)(Math.random()* 100);
if(ear[e]%2==0)
System.out.print(ear[e] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] numberArray = new int[25];
printArray(numberArray);
}
}
英文:
While writing a code for printing out random numbers ranging from 0 to 99, I had faced some difficulties. I tried to change the condition in the for statement to e<100, however, it had occurred an error. Where should I put the conditions in order for my output to show numbers between 0 to 99?
public class EvensOdds {
public static void printArray(int[] ear) {
System.out.println("ODD NUMBERS : ");
for (int e = 0; e<ear.length ; e ++) {
ear[e] = (int)(Math.random()* 10);
if(ear[e]%2!=0)
System.out.print(ear[e] + " ");
}
System.out.println("\n" + "EVEN NUMBERS : ");
for (int e = 0; e<ear.length ; e ++) {
ear[e] = (int)(Math.random()* 10);
if(ear[e]%2==0)
System.out.print(ear[e] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] numberArray = new int[25];
printArray(numberArray);
}
}
答案1
得分: 1
在Java 8中,下面的代码创建了一个大小为25的数组,其中包含介于0和99之间的随机条目。
Random random = new Random();
int[] array = random.ints(25, 0, 100).toArray();
英文:
In Java 8, below creates an array with size 25, with random entries between 0,99
Random random = new Random();
int[] array = random.ints(25, 0, 100).toArray();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论