0

在尝试制作这个简单的程序时,我无法根据字符串获取用户输入。输入整数时,我没有问题,但是当我的程序要求用户输入另一个字符时,光标会闪烁等待我输入内容,但它不会让我输入。如果我注释掉所有整数的东西,我就可以输入一个字符串。我不能同时输入有什么原因吗?谢谢你

导入 java.util.Scanner;

public class math {
public static void main(String args[]){
    int int1,int2,int3;
    String operator;
    Scanner ahmad=new Scanner(System.in);
    System.out.print("Enter three integers: ");
    int1=ahmad.nextInt();
    int2=ahmad.nextInt();
    int3=ahmad.nextInt();
    System.out.print("Enter a (for average), s (for sum) or p (for product):");
    operator=ahmad.nextLine();
    System.out.println("Thank you");



}

}

4

1 回答 1

3

nextInt()仅消耗整数,不消耗空格字符(在这种情况下为 EOL)。使用两个nextLine(),一个消耗 EOL 字符,一个提示您输入。

System.out.print("Enter a (for average), s (for sum) or p (for product):");
operator=ahmad.nextLine();
operator=ahmad.nextLine();

System.out.println("Thank you");
于 2013-10-10T01:26:50.087 回答