首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用用户输入Java的平均计算器-“java.util.NoSuchElementException:找不到行”

使用用户输入Java的平均计算器-“java.util.NoSuchElementException:找不到行”
EN

Stack Overflow用户
提问于 2021-04-01 12:39:15
回答 3查看 90关注 0票数 0

我正在使用Eclipse上的用户输入创建一个简单的平均计算器,并收到以下错误:“java.util.NoSuchElementException: No line found”

代码语言:javascript
运行
复制
String input = sc.nextLine();

此外,我认为还会有后续错误,因为我不确定是否可以使用两个变量string和float作为用户输入。

代码语言:javascript
运行
复制
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);

    }

}

非常感谢:)

EN

回答 3

Stack Overflow用户

发布于 2021-04-01 13:18:01

首先,float的精确度太差了,你使用它是在伤害自己。除非您有非常特殊的需要使用float,否则您应该始终使用double

比较字符串时,请使用equals()。有关更多信息,请参阅How do I compare strings in Java?

由于您似乎希望用户继续输入数字,因此需要调用nextDouble()作为循环的一部分。而且,由于您似乎希望用户输入文本以结束输入,因此需要调用hasNextDouble()来防止获取InputMismatchException。使用next()获取单个单词,这样您就可以检查它是否是单词"done"

如下所示:

代码语言:javascript
运行
复制
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);

样本输出

代码语言:javascript
运行
复制
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
票数 3
EN

Stack Overflow用户

发布于 2021-04-01 13:14:30

所以这段代码有几个问题:

  1. 由于您想让用户输入一个数字或命令"done",因此您必须使用sc.nextLine();。这是因为如果您同时使用sc.nextLine();sc.nextFloat();,程序将首先尝试接收一个字符串,然后再接收一个数字。

  1. 你不会在循环中更新输入变量,它只会要求一个输入并停止。

  1. 和字符串比较在Java语言中很奇怪(不能使用!===)。您需要使用stra.equals(strb).

要实施更改,请执行以下操作:

代码语言:javascript
运行
复制
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);

    }

}
票数 0
EN

Stack Overflow用户

发布于 2021-04-01 14:42:15

代码语言:javascript
运行
复制
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);

    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66898510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档