如何让Java Scanner实用程序输入两次

如何让Java Scanner实用程序输入两次,java,termination,Java,Termination,这是我的密码 public class Practice01{ public static void main (String[] args){ System.out.println("Hi there"); Scanner scr = new Scanner(System.in); String response = 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");
                 
                 
                 String responseG;
                 responseG = scr.nextLine();
             
             if (responseG.equalsIgnoreCase("good")) {
                 System.out.println("Glad to hear");
                 
             }
         }     
        }
    }  
说到java,我有点不知所措,我今天刚开始,但是在这里的else陈述之后,java出于某种原因终止了它自己,它只是不关心其余的代码。我在网上查看,发现如果您想获取其他输入,可以使用
.nextLine()函数(我不认为它被称为函数,但你知道我的意思)但在我键入“嗨,你好”或“嗨”后,它会打印“哦,你不是很有礼貌吗?你好,你好吗?”然后我不能键入任何其他内容,它会说。有人能帮忙吗?谢谢


编辑:显然我应该将“responseG”变量和下一行移到if语句中。当我这样做时,它不会激活(使用EclipseIDE,它只是显示为白色和错误),并告诉我删除其他内容。描述发生的事情。此外,如果我尝试使用else-if语句,它还表示要删除它,您必须使用循环来保持程序工作。打印“哦,你不是很有礼貌吗?你好,你好吗?”后,它终止了没有更多任务要做的事实。

嗨,欢迎来到Stackoverflow! 如果你不键入“hello”或“hello”或“hey”,你的程序只会从键盘请求另一个输入,你知道我的意思。如果您键入“hello”,它将打印一行

“哦,你不是很有礼貌吗?你好,你好吗?”

程序将被终止

这就是你的代码告诉你的程序要做的事情,因为你只需要在else语句的主体中请求另一个输入

正如我所看到的,您不想使用for循环,因为您似乎只关心一条表示hi的路径。。。你好吗?。。。好的。。。很高兴听到。。。但是如果您喜欢它,您可以使用
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语句移动到your response if语句中,因为这会阻止代码完成,因为您输入了正确的输入。此外,对于未来的项目,要更加有组织,请在代码的开头创建变量

公共课堂练习01{
公共静态void main(字符串[]args){
System.out.println(“你好”);
字符串响应;
扫描仪scr=新扫描仪(System.in);
字符串响应=scr.nextLine();
if(response.equalsIgnoreCase(“hello”)||
答复.同等信号情况(“hi”)||
回复:equalsIgnoreCase(“嘿”)){
System.out.println(“哦,你不是很有礼貌吗?你好,你好吗?”);
responseG=scr.nextLine();
如果(响应,如同等信号(“良好”)){
System.out.println(“很高兴听到”);
}
}否则{
System.out.println(“无效输入”);
}
}  
}

使用循环重复请求输入。阅读
while
for
。清理缩进,你会发现你有一个误解。我的回答对你有帮助吗?如果有,请接受。