0

当我键入存储在字符串变量b中的内容p时,它不会响应,直到我多次单击回车键。

在这一点之后,程序只打印出 else 语句中的内容。程序响应的唯一字符串变量是j.

编码:

package legit;

import java.util.Scanner;

public class Gamee {


   public static void main(String args[]){

       Scanner sc = new Scanner(System.in);
        String j = "good, how are you?";
        String b = "good";
        String p = "bad";

        System.out.println("Hello, my name is Julie the Robot");

        System.out.println("How Are You?");


        if (j.equals(sc.nextLine())) {
        System.out.println("Im Doing Great!");


        }else if (b.equals(sc.nextLine())) {                
        System.out.println("Thats Great! :)");


        }else if (p.equals(sc.nextLine())){             
        System.out.println("Thats not good");

        }else {
        System.out.println("I see...");
        }
4

1 回答 1

1

您知道每次使用sc.nextLine()时都在向用户询问新的输入吗?

尝试在您的 s 之前使用它一次if并将接收到的输入存储在值中,然后在条件中使用该值。

于 2013-06-21T22:12:03.847 回答