随机平均数在Java中不起作用。

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

Random Average Number Not Working in Java

问题

public class AverageRandom{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入数量:");
        int amount = sc.nextInt();

        Random randNo = new Random();
        double sum = 0;
        int max = 9;
        double average = 0;
        int above;
        above = 0;
        
        for(int i = 0; i < amount; i++){
            int next = randNo.nextInt(max) + 1;
            System.out.print(next + " ");

            sum += next;
            average = sum / amount;

            if(next > average){
                above++;
            }
        }

        System.out.println("\n平均值 = " + average);
        System.out.print("高于平均值的数字数量 = " + above + "\n");
    }
}
英文:

I need to find the average of random numbers (1-9) and then find the amount of random numbers that are above the average.

I found the average and I tried to find amount of numbers above the average but it keeps giving me the wrong answer.


public class AverageRandom{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		System.out.print(&quot;n? &quot;);
		int amount = sc.nextInt();

		Random randNo = new Random();
		double sum = 0;
		int max = 9;
		double average = 0;
		int above;
		above = 0;
		
		for(int i = 0; i &lt; amount; i++){
			int next = randNo.nextInt(max) +1;
			System.out.print(next + &quot; &quot;);

			sum += next;
			average = sum/amount;

			if(next &gt; average){
				above++;
			}
		}

		System.out.println(&quot;\nAverage = &quot; + average);
		System.out.print(&quot;Number of values above the average = &quot; + above + &quot;\n&quot;);
	}
}

答案1

得分: 0

你可以使用两个循环和一个数组来存储值:

public class AverageRandom {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("n? ");
        int amount = sc.nextInt();

        Random randNo = new Random();
        double sum = 0;
        int max = 9;
        double average = 0;

        int[] array = new int[amount]; // 定义大小为 'amount' 的数组

        for (int i = 0; i < amount; i++) {
            array[i] = randNo.nextInt(max) + 1; // 填充数组
            
            System.out.print(array[i] + " ");

            sum += array[i];
        }
        average = sum / array.length; // 你也可以在循环内计算平均值

        int above = 0;

        for (int i = 0; i < array.length; i++) { // 遍历数组(随机值)
            if (array[i] > average) {
                above++;
            }
        }

        System.out.println("\nAverage = " + average);
        System.out.print("Number of values above the average = " + above + "\n");
    }
}
英文:

you can use 2 loops and an array to store the values:

public class AverageRandom{
 public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    System.out.print(&quot;n? &quot;);
    int amount = sc.nextInt();

    Random randNo = new Random();
    double sum = 0;
    int max = 9;
    double average = 0;

    int[] array = new int[amount]; // define an array of size &#39;amount&#39;

    for(int i = 0; i &lt; amount; i++) {
        array[i] = randNo.nextInt(max) + 1; // fill the array
        
        System.out.print(array[i] + &quot; &quot;);

        sum += array[i];
    }
    average = sum/array.length; // you can also calculate the average inside the loop

    int above = 0;

    for(int i = 0; i &lt; array.length; i++) { // iterate the array (your random values)
        if(array[i] &gt; average){
            above++;
        }
    }

    System.out.println(&quot;\nAverage = &quot; + average);
    System.out.print(&quot;Number of values above the average = &quot; + above + &quot;\n&quot;);
 }
}

huangapple
  • 本文由 发表于 2020年5月5日 16:43:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/61609155.html
匿名

发表评论

匿名网友

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

确定