英文:
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("n? ");
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("\nAverage = " + average);
System.out.print("Number of values above the average = " + above + "\n");
}
}
答案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("n? ");
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 'amount'
for(int i = 0; i < amount; i++) {
array[i] = randNo.nextInt(max) + 1; // fill the array
System.out.print(array[i] + " ");
sum += array[i];
}
average = sum/array.length; // you can also calculate the average inside the loop
int above = 0;
for(int i = 0; i < array.length; i++) { // iterate the array (your random values)
if(array[i] > average){
above++;
}
}
System.out.println("\nAverage = " + average);
System.out.print("Number of values above the average = " + above + "\n");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论