简体   繁体   中英

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. 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 :)

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. 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. It does not consume the new line you typed using the Enter on the keyboard while entering the two numbers. When the execution reaches 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:

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

I believe the in.nextLine(); operation is reading only to the end of the line where you input 2 numbers. 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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