简体   繁体   English

Java代码突然停止

[英]Java code suddenly stop

Ive got a problem.I 'm new to Java,I've started today:D) ..I've programmed before so I know it little bit,but I am new to Java. 我遇到了一个问题。我是Java的新手,我从今天开始:D)..我之前已经编程过,所以我有点了解,但是我是Java的新手。 Here is my code: ` 这是我的代码:

public class Tutorial {

public static void main(String[] args) {
    double num1,num2;
    String operacia;
    Scanner in=new Scanner (System.in);
    System.out.println("Write 2 numbers");
    num1=in.nextDouble();
    num2=in.nextDouble();
    System.out.println("Choose the operation");
    operacia=in.nextLine();
    if (operacia.equals("+")){
        System.out.println("Your result is "+(num1+num2))   ;
    }
    else if (operacia.equals("-")){
        System.out.println("Your result is  "+(num1-num2))  ;
    }
    else if (operacia.equals("/")){
        System.out.println("Your result is  "+(num1/num2))  ;
    }
    else if (operacia.equals("*")){
        System.out.println("Your result is  "+(num1*num2))  ;
    }




}
}` 

It wants from me 2 numbers,I write them and them it writes "Choose the operation" and its over.No more inputs.Thank you very much :) 它需要我输入2个数字,我写了它们,并且写了“选择操作”及其结束。不再输入。非常感谢:)

Your problem is simple. 您的问题很简单。

Just replace the code with next() instead of nextLine().Effectively, the line your code is returning is receiving is a blank line. 只需将代码替换为next()而不是nextLine()。有效地,您的代码正在返回的行是空白行。 Hence when it reaches the conditional statement it has an empty string and terminates. 因此,当它到达条件语句时,它具有一个空字符串并终止。

next()
Finds and returns the next complete token from this scanner.

nextLine()
Advances this scanner past the current line and returns the input that was skipped.

Your code should be fixed by a simple change. 您的代码应通过简单的更改来修复。

public static void main(String[] args) {
    double num1,num2;
    String operacia;

    Scanner in=new Scanner (System.in);
    System.out.println("Write 2 numbers");

    num1=in.nextDouble();
    num2=in.nextDouble();

    System.out.println("Choose the operation");
    operacia=in.next();

    if (operacia.equals("+")){
        System.out.println("Your result is "+(num1+num2))   ;
    }
    else if (operacia.equals("-")){
        System.out.println("Your result is  "+(num1-num2))  ;
    }
    else if (operacia.equals("/")){
        System.out.println("Your result is  "+(num1/num2))  ;
    }
    else if (operacia.equals("*")){
        System.out.println("Your result is  "+(num1*num2))  ;
    }
}

Scanner#nextDouble() consumes only the next token as a double from the input. Scanner#nextDouble()仅使用下一个令牌作为输入的double。 It does not consume the new line you typed using the Enter on the keyboard while entering the two numbers. 输入两个数字时,它不会占用您使用键盘上的Enter键键入的新行。 When the execution reaches operacia=in.nextLine(); 当执行到达operacia=in.nextLine(); , this new line is consumed, never allowing the user a chance to type the operating string. ,此新行将被占用,从不让用户有机会键入操作字符串。

To solve this, you need to read the whole line using Scanner#nextLine() and convert it to a double: 要解决此问题,您需要使用Scanner#nextLine()阅读整行并将其转换为double:

String input = in.nextLine();
num1 = Double.parseDouble(input);
input = in.nextLine();
num2 = Double.parseDouble(input);

I believe the in.nextLine(); 我相信in.nextLine(); operation is reading only to the end of the line where you input 2 numbers. 该操作仅读取到您输入2个数字的行的末尾。 If you want your program to only consider the next line, you have to clear the current one first. 如果要让程序仅考虑下一行,则必须先清除当前行。

Try this, it should work: 试试这个,它应该可以工作:

System.out.println("Choose the operation");
in.nextLine(); //clear the current line
operacia=in.nextLine();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM