我正在编写一个程序,它将10个浮点数作为输入,并显示数字的平均值,后跟所有大于平均值的数字。我正在使用一个方法,该方法将一个双精度数组作为参数,并返回数组中数据的平均值。但是,我的问题是,当我运行程序时,输出窗口是完全空白的。我认为这是因为我没有在我的方法中调用main方法。但是,我不知道如何编写该代码。谢谢。
import java.util.Scanner;
public class Average {
public static void main(String[] args) {
}
public double average(double[] number) {
Scanner scanner = new Scanner(System.in);
int x = 0;
double sum = 0;
double[] numberList = new double[10]; //array to hold all numbers
double[] largerList = new double[10]; //array to hold numbers greater than the average
int numberIndex = 0;
int largerIndex = 0;
System.out.printf("Please enter 10 floating-point numberes.\nIf more than 10 values are entered, the numbers following 10 are ignored.\nIf less than 10 numbers are entered, the program will wait for you to enter 10.\n");
for (int i = 0; i < 10; i++) {
try { //try catch exception to catch decimal inputs as well as more /less than 10 integers
x = scanner.nextInt();
sum += numberList[x]; //add up all inputs to find sum
} catch (Exception e) {
System.out.println("Invalid input! Please reenter 10 integer values.");
scanner = new Scanner(System.in);
i = -1;
numberIndex = 0;
largerIndex = 0;
numberList = new double[10];
largerList = new double[10];
continue;
}
}
for (int i = 0; i < number.length; i++) {
sum = sum + number[i];
double average = sum / number.length;
System.out.println("Average value of your input is: " + average);
System.out.println();
//return average;
if (x > average) {
largerList[largerIndex] = x; //add negative input to negativeList array
largerIndex = largerIndex + 1;
}
}
for (int i = 0; i < largerIndex; i++) {
System.out.println(largerList[i]);
}
return 0;
}
}
答案 0 :(得分:2)
回答主要问题......
但是,我的问题是,当我运行程序时,输出窗口是完全空白的。我认为这是因为我没有在我的方法中调用main方法。但是,我不确定如何编写该代码。
public static void main(String[] args) {
new Average().average(new double[10]);
}
或许你在想这样的事情......
public static void main(String[] args) {
double[] numbers = {2,3,4,5,6,4,3,2,1,3};
new Average().average(numbers);
}
输出从上方开始(给出双打):
Please enter 10 floating-point numberes.
If more than 10 values are entered, the numbers following 10 are ignored.
If less than 10 numbers are entered, the program will wait for you to enter 10.
2
3
3
4
1
2
3
4
5
1
Average value of your input is: 0.2
Average value of your input is: 0.5
Average value of your input is: 0.9
Average value of your input is: 1.4
Average value of your input is: 2.0
Average value of your input is: 2.4
Average value of your input is: 2.7
Average value of your input is: 2.9
Average value of your input is: 3.0
Average value of your input is: 3.3
1.0
1.0
1.0
Press any key to continue . . .
如果您对代码本身有疑问,那么最好创建一个新问题或对其进行编辑以使其更清晰。
祝你好运。
答案 1 :(得分:1)
您的方法average()
采用一系列双精度数,但随后从标准输入中检索另一个数组。这没有意义。
获取双打并将它们传递给方法,或者不将它们传递给方法并从标准输入中获取它们。
答案 2 :(得分:0)
您需要做的是在Average
方法中创建main
类的实例,然后调用average()
方法。
为什么在从用户那里获取输入时,在double
方法中解析average()
数组?
答案 3 :(得分:0)
well,you are using a non static method "average()" for your task which needs an instances of the class to run which are not creating anywhere.so there are only two options:-
#1.create an instances of your class then call it.
public static void main(String... s)
{
Average obj=new Average();
obj.average();
}
#2.make "average()" a static method by adding static keyword.
public static double average()
{
//your code.....
}
public sttatic void main(String... s)
{
average();
}
your dont need to keep an argument in your method.