package Lessons;

import java.util.Scanner;

public class Math {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("please enter the first number of your question! ");
        int a1 = s.nextInt();
        System.out.print("So you chose " + a1);
        System.out.println(" Choose ur second number");
        int a2 = s.nextInt();
        System.out.print("You chose " + a2 + " for your second number");
        System.out.print(" Now enter ur operator ");
        String b1 = s.nextLine();
        if (b1.equalsIgnoreCase("-")) {
            System.out.println("You chose " + b1 + "as ur operator");
        }
    }

}


我不明白为什么第15行不做任何输入,将不胜感激:3

输出量


  请输入您的问题的第一个数字! 2552所以您选择了2552
  选择您的第二个数字41您选择第二个数字41
  输入您的操作员


由于某种原因,输出在最后一行结束并停止,并且不接收任何信息!

最佳答案

您需要在调用in.nextLine()的行之后立即调用in.nextInt(),原因是仅要求下一个整数不会占用输入中的整个行,因此您需要跳到下一个新行通过调用in.nextLine()在输入中输入字符。

int a2 = s.nextInt();
s.nextLine();


每次您需要在调用不消耗整行的方法后(例如,调用nextBoolean()等时)获得新行时,都必须执行此操作。

关于java - 为什么我的Java扫描仪不接受输入?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30137511/

10-11 22:15
查看更多