我正在使用Eclipse上的用户输入创建一个简单的平均计算器,并收到以下错误:“java.util.NoSuchElementException: No line found”
String input = sc.nextLine();此外,我认为还会有后续错误,因为我不确定是否可以使用两个变量string和float作为用户输入。
import java.util.Scanner;
public class AverageCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the numbers you would like to average. Enter \"done\"");
String input = sc.nextLine();
float num = sc.nextFloat();
float sum = 0;
int counter = 0;
float average = 0;
while(input != "done"){
sum += num;
counter ++;
average = sum / counter;
}
System.out.println("The average of the "+ counter + " numbers you entered is " + average);
}
}非常感谢:)
发布于 2021-04-01 13:18:01
首先,float的精确度太差了,你使用它是在伤害自己。除非您有非常特殊的需要使用float,否则您应该始终使用double。
比较字符串时,请使用equals()。有关更多信息,请参阅How do I compare strings in Java?。
由于您似乎希望用户继续输入数字,因此需要调用nextDouble()作为循环的一部分。而且,由于您似乎希望用户输入文本以结束输入,因此需要调用hasNextDouble()来防止获取InputMismatchException。使用next()获取单个单词,这样您就可以检查它是否是单词"done"。
如下所示:
Scanner sc = new Scanner(System.in);
double sum = 0;
int counter = 0;
System.out.println("Enter the numbers you would like to average. Enter \"done\"");
for (;;) { // forever loop. You could also use 'while (true)' if you prefer
if (sc.hasNextDouble()) {
double num = sc.nextDouble();
sum += num;
counter++;
} else {
String word = sc.next();
if (word.equalsIgnoreCase("done"))
break; // exit the forever loop
sc.nextLine(); // discard rest of line
System.out.println("\"" + word + "\" is not a valid number. Enter valid number or enter \"done\" (without the quotes)");
}
}
double average = sum / counter;
System.out.println("The average of the "+ counter + " numbers you entered is " + average);样本输出
Enter the numbers you would like to average. Enter "done"
1
2 O done
"O" is not a valid number. Enter valid number or enter "done" (without the quotes)
0 done
The average of the 3 numbers you entered is 1.0发布于 2021-04-01 13:14:30
所以这段代码有几个问题:
sc.nextLine();。这是因为如果您同时使用sc.nextLine();和sc.nextFloat();,程序将首先尝试接收一个字符串,然后再接收一个数字。、
!=或==)。您需要使用stra.equals(strb).要实施更改,请执行以下操作:
import java.util.Scanner;
public class AverageCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the numbers you would like to average. Enter \"done\"");
float sum = 0;
int counter = 0;
String input = sc.nextLine();
while (true) {
try {
//Try interpreting input as float
sum += Float.parseFloat(input);
counter++;
} catch (NumberFormatException e) {
//Turns out we were wrong!
//Check if the user entered done, if not notify them of the error!
if (input.equalsIgnoreCase("done"))
break;
else
System.out.println("'" + input + "'" + " is not a valid number!");
}
// read another line
input = sc.nextLine();
}
// Avoid a divide by zero error!
if (counter == 0) {
System.out.println("You entered no numbers!");
return;
}
// As @Andreas said in the comments, even though counter is an int, since sum is a float, Java will implicitly cast coutner to an float.
float average = sum / counter;
System.out.println("The average of the "+ counter + " numbers you entered is " + average);
}
}发布于 2021-04-01 14:42:15
import java.util.Scanner;
public class AverageCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the numbers you would like to average. Enter \"done\" at end : ");
String input = scanner.nextLine();
float num = 0;
float sum = 0;
int counter = 0;
float average = 0;
while(!"done".equals(input)){
num = Float.parseFloat(input); // parse inside loop if its float value
sum += num;
counter ++;
average = sum / counter;
input = scanner.nextLine(); // get next input at the end
}
System.out.println("The average of the "+ counter + " numbers you entered is " + average);
}
}https://stackoverflow.com/questions/66898510
复制相似问题