在Java中设置数组范围内的数字。

huangapple go评论114阅读模式
英文:

Setting a number in range in arrays for java

问题

以下是您要翻译的代码部分:

  1. 在编写一个用于打印从099范围内的随机数的代码时我遇到了一些困难我尝试将for语句中的条件更改为e<100然而出现了一个错误我应该在哪里设置条件以便我的输出显示099之间的数字
  2. public class EvensOdds {
  3. public static void printArray(int[] ear) {
  4. System.out.println("奇数:");
  5. for (int e = 0; e<ear.length ; e ++) {
  6. ear[e] = (int)(Math.random()* 100);
  7. if(ear[e]%2!=0)
  8. System.out.print(ear[e] + " ");
  9. }
  10. System.out.println("\n" + "偶数:");
  11. for (int e = 0; e<ear.length ; e ++) {
  12. ear[e] = (int)(Math.random()* 100);
  13. if(ear[e]%2==0)
  14. System.out.print(ear[e] + " ");
  15. }
  16. System.out.println();
  17. }
  18. public static void main(String[] args) {
  19. int[] numberArray = new int[25];
  20. printArray(numberArray);
  21. }
  22. }
英文:

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?

  1. public class EvensOdds {
  2. public static void printArray(int[] ear) {
  3. System.out.println(&quot;ODD NUMBERS : &quot;);
  4. for (int e = 0; e&lt;ear.length ; e ++) {
  5. ear[e] = (int)(Math.random()* 10);
  6. if(ear[e]%2!=0)
  7. System.out.print(ear[e] + &quot; &quot;);
  8. }
  9. System.out.println(&quot;\n&quot; + &quot;EVEN NUMBERS : &quot;);
  10. for (int e = 0; e&lt;ear.length ; e ++) {
  11. ear[e] = (int)(Math.random()* 10);
  12. if(ear[e]%2==0)
  13. System.out.print(ear[e] + &quot; &quot;);
  14. }
  15. System.out.println();
  16. }
  17. public static void main(String[] args) {
  18. int[] numberArray = new int[25];
  19. printArray(numberArray);
  20. }
  21. }

答案1

得分: 1

在Java 8中,下面的代码创建了一个大小为25的数组,其中包含介于0和99之间的随机条目。

  1. Random random = new Random();
  2. int[] array = random.ints(25, 0, 100).toArray();
英文:

In Java 8, below creates an array with size 25, with random entries between 0,99

  1. Random random = new Random();
  2. int[] array = random.ints(25, 0, 100).toArray();

huangapple
  • 本文由 发表于 2020年7月22日 21:26:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63035290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定