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

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

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(&quot;ODD NUMBERS : &quot;);
	for (int e = 0; e&lt;ear.length ; e ++) {
		ear[e] = (int)(Math.random()* 10);
			if(ear[e]%2!=0)
			System.out.print(ear[e] + &quot;  &quot;);
	}
	System.out.println(&quot;\n&quot; + &quot;EVEN NUMBERS : &quot;);
	for (int e = 0; e&lt;ear.length ; e ++) {
		ear[e] = (int)(Math.random()* 10);
			if(ear[e]%2==0)
			System.out.print(ear[e] + &quot;  &quot;);
	}
	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();

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:

确定