Java 为什么可以';我不能按';输入';

Java 为什么可以';我不能按';输入';,java,loops,while-loop,Java,Loops,While Loop,任何人请帮助我打破while循环,我只想在用户没有输入任何内容时结束程序,但为什么它不能工作?请帮忙,非常感谢 import java.util.Random; import java.util.Scanner; import java.lang.Math; public class Determining_Pi_Experiment { public static void main(String[] args) { while (true) {

任何人请帮助我打破while循环,我只想在用户没有输入任何内容时结束程序,但为什么它不能工作?请帮忙,非常感谢

import java.util.Random;
import java.util.Scanner;
import java.lang.Math;

public class Determining_Pi_Experiment {
    public static void main(String[] args) {
        while (true) {
            System.out.println("Press 'enter' to exit, or type an integer number indicating for how many times you " +
                    "want the experiment run: ");
            Scanner input = new Scanner(System.in);
            if(!input.equals(null)) {
                if(input.hasNextInt()) {

                    System.out.println("Processing...");
                    Random rand = new Random();
                    int ExperimentTimes = input.nextInt();
                    double count_success = 0;
                    double Pi = 0;

                    for (int i = 0; i < ExperimentTimes; ++i) {

                        double x = rand.nextDouble();
                        double y = rand.nextDouble();

                        double distance = Math.pow(Math.pow((x - 0.5), 2) + Math.pow((y - 0.5), 2), 0.5);

                        if (distance <= 0.5) {
                            ++count_success;
                        }
                    }
                    Pi = (count_success / ExperimentTimes) * 4;
                    System.out.println("Pi is approximately equal to: " + Pi);
                }
                else {
                    System.out.println("Invalid input.");
                }
            }
            else if(input.equals(null)) {
                System.exit(0);
            }
        }
    }
}
import java.util.Random;
导入java.util.Scanner;
导入java.lang.Math;
公共班级确定实验{
公共静态void main(字符串[]args){
while(true){
System.out.println(“按“回车”键退出,或键入一个整数,指示您的输入次数”+
“希望实验运行:”;
扫描仪输入=新扫描仪(System.in);
如果(!input.equals(null)){
if(input.hasNextInt()){
System.out.println(“处理…”);
Random rand=新的Random();
int ExperimentTimes=input.nextInt();
重复计数成功率=0;
双Pi=0;
for(int i=0;i如果(distance我能看到你的代码中有很多错误,我会带你检查它们

1) 过于复杂、过于冗长、不需要的检查 2) #equals方法的误用 3) 不遵循标准命名约定 4) 对如何构造输入-读取循环的一般误解

要扩展它们:

1)尽量简化代码,删除while-true循环和else子句(参见第4点),只声明一次变量,在外部删除冗余括号。 此外,距离可以计算为
Math.hypot(x1-x2,y1-y2)
(参见)

2)请注意,应使用
equals
方法检查一个对象是否等于另一个对象。如果在您的示例中返回true,则意味着扫描仪本身为null(不是它读取的内容),因此检查无法工作,因为将引发NullPointerException(调用空扫描仪上的方法)。要检查扫描仪(或任何对象)是否为空,您需要执行
anyObject==null
。请注意,这与扫描仪输入无关(请参见第4点)

3)请正确命名变量()

4)如果要继续读取用户输入,直到没有更多输入可用,则应使用。如果要在输入空字符串时结束,则应确实检查该字符串是否为空。这与扫描仪为空无关。
someString.isEmpty()
将为您完成这项工作

伪循环:


你的
扫描器
(事实上什么都没有)将永远
.equals(null)
。这根本不可能,因为在
null
上调用
.equals
会引发
空点异常
。你的
扫描器
也永远不会等于
字符串
。你必须调用
下一个()
方法。但我如何修改它以实现这一点?当我按enter键时,它会中断?如果你只按enter键,那么输入将是一个空字符串
“”
?你的输入是什么?回答很好。
while(scanner.hasNextLine() && !((line = scanner.nextLine()).isEmpty()))
 //do something with the input, stored in the line String

//Exits when enter is pressed (or EOF)