为什么我的Java扫描程序不接受输入? [英] Why Wont My Java Scanner Take In input?

查看:136
本文介绍了为什么我的Java扫描程序不接受输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I do not understand why line 15 does not take any input, any help would be appreciated:3

输出


请输入您问题的第一个号码! 2552所以你选择2552
选择你的第二个数字41你选择41作为你的第二个数字现在
输入你的运算符

please enter the first number of your question! 2552 So you chose 2552 Choose ur second number 41 You chose 41 for your second number Now enter ur operator

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

Output ends and stops at the last line for some reason and does not take any information!

推荐答案

您需要致电 in.nextLine()就在您调用 in.nextInt()的行之后原因只是要求下一个整数不会消耗输入中的整行,因此您需要通过调用 in.nextLine()来跳转到输入中的下一个换行符。

You need to call in.nextLine() right after the line where you call in.nextInt() The reason is that just asking for the next integer doesn't consume the entire line from the input, and so you need skip ahead to the next new-line character in the input by calling in.nextLine().

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

每次调用一个方法后需要获取新行时,这几乎必须完成不消耗整行,例如当你调用 nextBoolean()等时。

This pretty much has to be done each time you need to get a new line after calling a method that doesn't consume the entire line, such as when you call nextBoolean() etc.

这篇关于为什么我的Java扫描程序不接受输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆