如何在Java中从控制台获取String输入?

时间:2016-12-27 12:38:10

标签: java

这是我在Java中的代码

import java.util.Scanner;
class calc
{
public static void main(String[] args)
{
    boolean go=true;
    Scanner input=new Scanner(System.in);
    while(go)
    {
        int num1;
        int num2;
        int total;
        int choice;

        System.out.println("\nHi This is Console type Calculator");
        System.out.println("1. Addition");
        System.out.println("2. Substraction");
        System.out.println("3. Multiply");
        System.out.println("4. Divisoin");
        System.out.print("Enter Your Choice : ");
        choice=input.nextInt();

        switch(choice)
        {

            case 1:
                System.out.println("Enter First Number");
                num1=input.nextInt();
                System.out.println("Enter Second Number");
                num2=input.nextInt();
                total=num1+num2;
                System.out.println("Addition of "+num1+" and "+num2+" are "+total);
                break;
            case 2:
                System.out.println("Enter First Number");
                num1=input.nextInt();
                System.out.println("Enter Second Number");
                num2=input.nextInt();
                total=num1-num2;
                System.out.println("Substraction of "+num1+" and "+num2+" are "+total);
                break;
            case 3:
                System.out.println("Enter First Number");
                num1=input.nextInt();
                System.out.println("Enter Second Number");
                num2=input.nextInt();
                total=num1*num2;
                System.out.println("Multiplication of "+num1+" and "+num2+" are "+total);
                break;
            case 4:
                System.out.println("Enter First Number");
                num1=input.nextInt();
                System.out.println("Enter Second Number");
                num2=input.nextInt();
                total=num1/num2;
                System.out.println("Divistion of "+num1+" and "+num2+" are "+total);
                break;
            default:
                System.out.println("Please Choose right option...Try again");
                break;  
        }

        System.out.println("Do You Want more Calculation...Yes/No");
        String str=input.nextLine();
        System.out.println("Do You Want more Calculation...Yes/No");
        String str1=input.nextLine();

        if("no".equals(str1))
        {
            go=false;
            System.out.println("Thanks For using...Bye");
        }   
    }   
}
}

我在下面的代码部分中遇到输入问题。这不是用户的任何输入跳过此部分。这段代码有什么问题。

        System.out.println("Do You Want more Calculation...Yes/No");
        String str1=input.nextLine();

        if("no".equals(str1))
        {
            go=false;
            System.out.println("Thanks For using...Bye");
        }   

2 个答案:

答案 0 :(得分:3)

问题在于您之前选择了nextInt。用户输入数字和新行字符。您选择号码,新行字符保持缓冲。当您执行nextLine()时,它会读取指向的内容与下一个EOL之间的所有字符。它在\n之前读取缓冲区中的空字符串,然后另一个nextLine()要求程序等待输入。

当您在String str = input.nextLine()之后设置断点时,您会发现它实际上是空的String""

enter image description here

所以而不是:

    System.out.println("Do You Want more Calculation...Yes/No");
    String str=input.nextLine();
    System.out.println("Do You Want more Calculation...Yes/No");
    String str1=input.nextLine();

你应该写:

    input.nextLine();
    System.out.println("Do You Want more Calculation...Yes/No");
    String str1=input.nextLine();

答案 1 :(得分:-1)

您的问题是您在案例中使用nextInt(),因此存储的字符串是新行。您所要做的就是替换:

    System.out.println("Do You Want more Calculation...Yes/No");
    String str1=input.nextLine();

    if("no".equals(str1))
    {
        go=false;
        System.out.println("Thanks For using...Bye");
    }   

由:

    System.out.println("Do You Want more Calculation...Yes/No");
    input.nextLine(); // this is what I added
    String str1=input.nextLine();

    if("no".equals(str1))
    {
        go=false;
        System.out.println("Thanks For using...Bye");
    }  

您可以删除以下内容,因为这两行没有用处:

    System.out.println("Do You Want more Calculation...Yes/No");
    String str=input.nextLine();

说明:我添加的input.nextLine();基本上会在您按Enter键或返回时捕获新行字符。接下来,您必须再次调用input.nextLine()并将其分配给str1以捕获您要使用的字符串。 此外,您的程序上有一个小注释:如果用户输入No,您的程序仍将该值视为是,因为只有在小写的情况下,它才会变为false。解决方法是通过调用str1.toLowerCase()将您收到的字符串转换为小写字母。此外,您可能想要检查明确的是答案,因为用户可能会输入dog并且您的程序仍将其视为是。