如何使Java扫描仪实用程序输入两次

问题描述

所以这是我的代码


public class Practice01{

    public static void main (String[] args){
        
        
        System.out.println("Hi there");

          Scanner scr = new Scanner(system.in);

          String response = scr.nextLine();
          
          
         if (response.equalsIgnoreCase("hello") || response.equalsIgnoreCase("hi") || response.equalsIgnoreCase("hey")) {
             System.out.println("Oh,well arent you well-mannered. Hello there,how are you?");}
             
         
             else {
                 System.out.println("Invalid Input");
                 
                 
                 String responseG;
                 responseG = scr.nextLine();
             
             if (responseG.equalsIgnoreCase("good")) {
                 System.out.println("Glad to hear");
                 
             }
         }     
        }
    }  

关于Java,我今天才刚开始,但是在这里的else语句之后,java由于某种原因终止了自身,它只是不在乎其余的代码。我在网上查看,发现如果您想再输入一次,可以使用.nextLine();函数(我不认为它称为函数,但您知道我的意思),但是在键入hey,hello或hi之后,它显示“哦,好吧,您好人。你好,你好吗?”然后我无法输入其他任何内容,并且显示。有人可以帮忙吗?谢谢

编辑:显然,我应该将“ responseG”变量和下一行移到if语句中。当我这样做时,它不会激活(使用eclipse IDE,它只是显示为白色并显示错误),并告诉我删除其他内容https://gyazo.com/1a27fa9ab8802d594cccb35ecc0cb663有关发生的情况的图片。此外,如果我尝试使用else if陈述,它也会说删除

解决方法

您必须使用循环来保持程序正常运行。当您收到“哦,您的态度不是很好。您好,您好吗?”印刷,它终止了没有更多任务要做的事实。

,

嗨,欢迎来到Stackoverflow!

仅当您不键入“ hello” 或“ Hello”或“ hey”时,您的程序才会要求键盘输入其他信息,您知道我的意思。如果您输入“ hello”,它将使用

“哦,你不是很好吗?你好,你好吗?”

该程序将终止...

这就是您的代码告诉程序要执行的操作,因为您只是在else语句的主体中要求其他输入。

正如我所看到的,您不想使用for循环,因为您似乎只在乎一个打招呼的路径……您好吗?……很好……很高兴听到……但是如果您愿意的话,可以使用while()语句或do..while()for()将其保存到变量中,并使用scr.nextLine();函数(它 是一个函数),在这种情况下,您必须定义程序何时/如何停止输入请求并终止。


我相信这是您要尝试做的事情,使用适当的缩进,并且不声明不必要的变量:

public class Practice01{
    public static void main (String[] args){

        System.out.println("Hi there");

        Scanner scr = new Scanner(System.in);
        String response = scr.nextLine();
        
        if (response.equalsIgnoreCase("hello") || response.equalsIgnoreCase("hi") || response.equalsIgnoreCase("hey")) {
            System.out.println("Oh,well arent you well-mannered. Hello there,how are you?");
        }     
        else {
                System.out.println("Invalid Input");
        }

        //You don't need ResponseG... you've already created the response variable
        response = scr.nextLine();
            
        if (response.equalsIgnoreCase("good")) {
                System.out.println("Glad to hear");
        }
        else {
                System.out.println("Invalid Input");
        }
    }
}
,

因此,您要做的是将ResponseG if语句移至您的if响应语句中,因为由于您输入了正确的输入,因此该代码使代码无法完成。同样对于将来的项目,要使其更有条理,请在代码的开头创建变量。

    public class Practice01{
      public static void main (String[] args){  
        System.out.println("Hi there");

        String responseG;
        Scanner scr = new Scanner(System.in);
        String response = scr.nextLine();

        if (response.equalsIgnoreCase("hello") || 
            response.equalsIgnoreCase("hi") || 
            response.equalsIgnoreCase("hey")) {
          System.out.println("Oh,how are you?");
             
          responseG = scr.nextLine();
             
          if (responseG.equalsIgnoreCase("good")) {
            System.out.println("Glad to hear");
          }
        } else {
          System.out.println("Invalid Input");
        }
      }  
    }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...