如何让Java的Scanner工具输入两次。

huangapple go评论61阅读模式
英文:

How to get the Java Scanner utility to input twice

问题

以下是您的代码翻译:

import java.util.Scanner;

public class Practice01 {

    public static void main(String[] args) {

        System.out.println("嗨!");

        Scanner scr = new Scanner(System.in);

        String response = scr.nextLine();

        if (response.equalsIgnoreCase("你好") || response.equalsIgnoreCase("嗨") || response.equalsIgnoreCase("喂")) {
            System.out.println("哦,你真有礼貌。你好,你怎么样?");
        } else {
            System.out.println("无效输入");

            String responseG;
            responseG = scr.nextLine();

            if (responseG.equalsIgnoreCase("好")) {
                System.out.println("很高兴听到这个");
            }
        }
    }
}

对于您在Java方面的问题,您在else语句后面输入"hey"、"hello"或"hi"后,程序会打印出"哦,你真有礼貌。你好,你怎么样?",然后无法继续输入。您的问题可能是因为输入结束符的问题,导致程序无法正确读取您之后的输入。您可以尝试在输入response后添加一行 scr.nextLine(); 以清除输入缓冲区,然后再进行下一次的输入。

另外,关于您提到的将"responseG"变量和下一行代码移到if语句中的问题,您应该确保将这两行代码放在if语句的大括号内,以便它们只在满足if条件时执行。

此外,您提到在使用else if语句时出现了错误。如果您使用else if语句,确保将其与if语句对齐,并在每个else if语句后面使用花括号包裹代码块。如果您仍然遇到问题,请提供更多细节,以便我可以为您提供更准确的帮助。

英文:

so heres my code


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");
	    		 
	    	 }
	     }     
		}
	}  

im a bit of a noobie when it comes to java, I just started today, but after the else statment here, java terminates itself for some reason, and it just doesnt care about the rest of the code. I was looking online and I saw that if you wanted to take another input you used the .nextLine(); function (i dont think its called a function but you know what I mean) but after I type either hey, hello, or hi, it prints "Oh, well arent you well-mannered. Hello there, how are you?" and then I cant type anything else, and it says < terminated > . can anyone help? thanks

Edit: Apparently I'm supposed to move the "responseG" variable and next line into the if statment. When I do that it doesnt activate, (using eclipse IDE, and it just appears as white and as an error) and tells me to delete else. https://gyazo.com/1a27fa9ab8802d594cccb35ecc0cb663 picture of what happens. furthermore if i try to use an else if statment it also says to delete it

答案1

得分: 0

你必须使用循环使程序保持运行。在你打印出“哦,你是个彬彬有礼的人。你好,你好吗?”之后,它终止,因为没有更多的任务要做了。

英文:

You have to use a loop to keep the program working. After you get "Oh, well aren't you well-mannered. Hello there, how are you?" printed, it terminates the fact that there is no more tasks to do.

答案2

得分: 0

##欢迎来到Stackoverflow ##
如果您**没有输入hello**"Hello""hey"您的程序将只会从键盘要求另一个输入如果您输入了hello”,则它将打印一行内容
&gt;您真有礼貌你好你好吗?”

然后程序将被终止...

**这就是您的代码告诉程序要做的事情**因为您仅在else语句的主体中要求另一个输入

根据我所见您不想使用for循环因为您似乎只关心一个路径上面写着hi...你好吗...很好...很高兴听到...但是如果您关心的话您可以使用`while()`语句、`do..while()`语句或者`for()`语句以便使用`scr.nextLine();`函数******一个函数保存多个响应值如果这样的话您必须定义程序何时/如何停止请求输入并终止

我认为以下代码是您尝试实现的功能带有适当的缩进并且没有声明不必要的变量

```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 aren't you well-mannered. Hello there, how are you?");
        } else {
            System.out.println("Invalid Input");
        }

        // 您不需要ResponseG... 您已经创建了response变量
        response = scr.nextLine();

        if (response.equalsIgnoreCase("good")) {
            System.out.println("Glad to hear");
        } else {
            System.out.println("Invalid Input");
        }
    }
}

<details>
<summary>英文:</summary>
## Hi, welcome to Stackoverflow! ##
Your program will only ask for another input from your keyboard if you **don&#39;t type &quot;hello&quot;** or &quot;Hello&quot;, or &quot;hey&quot;, you know what I mean. If you type &quot;hello&quot; then it will print a line with 
&gt;&quot;Oh, well aren&#39;t you well-mannered. Hello there, how are you?&quot;
And the program will be terminated... 
**That&#39;s what your code it&#39;s telling your program to do**, since you are only asking for another input in the else statement&#39;s body. 
As I can see you don&#39;t want to use a for loop, since you seem to only care about a path that says hi... how are you?... good... glad to hear... but if you care for it, you can use a `while()` statement or a `do..while()` or a `for()` to make it save in the variable many responses with the `scr.nextLine();` function (it ***is*** a function), in that case you have to define when/how the program is going to stop asking for input and will terminate.
----------
I believe this is what does what you were trying to do, with the proper indentation, and not declaring unnecessary variables:
public class Practice01{
public static void main (String[] args){
System.out.println(&quot;Hi there&quot;);
Scanner scr = new Scanner(System.in);
String response = scr.nextLine();
if (response.equalsIgnoreCase(&quot;hello&quot;) || response.equalsIgnoreCase(&quot;hi&quot;) || response.equalsIgnoreCase(&quot;hey&quot;)) {
System.out.println(&quot;Oh, well arent you well-mannered. Hello there, how are you?&quot;);
}     
else {
System.out.println(&quot;Invalid Input&quot;);
}
//You don&#39;t need ResponseG... you&#39;ve already created the response variable
response = scr.nextLine();
if (response.equalsIgnoreCase(&quot;good&quot;)) {
System.out.println(&quot;Glad to hear&quot;);
}
else {
System.out.println(&quot;Invalid Input&quot;);
}
}
}
</details>
# 答案3
**得分**: 0
以下是您要求的翻译内容:
所以你想要做的是将 ResponseG 的 if 语句移到你的 response if 语句中,因为这会阻止代码完成,因为你输入了正确的内容。另外在将来的项目中,为了更有条理,可以在代码开头创建变量。
```java
public class Practice01{
public static void main (String[] args){  
System.out.println("嗨,你好");
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("哦,你真有礼貌。你好,你好吗?");
responseG = scr.nextLine();
if (responseG.equalsIgnoreCase("good")) {
System.out.println("很高兴听到这个。");
}
} else {
System.out.println("无效输入");
}
}  
}
英文:

So what you want to do is move the ResponseG if statement into the your response if statement as that is stopping the code from complete since you are putting in the correct inputs. Also for future projects, to be more organized create variables at the beginning of the code.

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

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

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

huangapple
  • 本文由 发表于 2020年9月11日 05:36:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63837997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定