0

我必须在我的代码中使用一个循环,这样当有人输入是时,他们可以根据需要多次重新输入他们的名字,但我不知道该怎么做。任何帮助表示赞赏,这是我的代码:

public static void main(String [] args)
  {
    // Create a Scanner object to read input.
    Scanner keyboard = new Scanner(System.in);

    //Get the user's name.
    System.out.print("What is your name?");
    String name = keyboard.nextLine();
    System.out.println("Hello there," + name);

    System.out.println("Would you like to enter another name? Please enter Yes Or No.");
    String reply = keyboard.nextLine();

    if (reply == "yes")
    {

    }
  }
}
4

5 回答 5

7

reply == "yes"不是您String在 Java 中比较 s 的方式。这比较了内存​​位置,而不是内容(内存位置不太可能相等)。

相反,您需要使用reply.equals("yes"),或者如果您不关心进行案例比较,则可以reply.equalsIgnoreCase("yes")使用

do {
    // The remainder of your code...
} while (reply.equalsIgnoreCase("yes"));

更新

您可能还希望阅读while 和 do-while 语句以及for 语句,其中涵盖了 Java 循环的基础知识

于 2013-04-08T01:23:30.237 回答
2

使用do-while循环:

public static void main(String[] args) {

        // Create a Scanner object to read input.
        Scanner keyboard = new Scanner(System.in);
        do {
            //Get the user's name.
            System.out.print("What is your name?");
            String name = keyboard.nextLine();
            System.out.println("Hello there," + name);
            System.out.println("Would you like to enter another name? Please enter Yes Or No.");

        } while (keyboard.nextLine().equalsIgnoreCase("yes"));

        System.out.println("Bye!");
        keyboard.close();
      }
    }
于 2013-04-08T01:32:04.933 回答
0

我认为这会起作用(未经测试):

public static Scanner keyboard = new Scanner(System.in); // global

    public static void main(String [] args)
    {
        getName();
    }

    public static void getName()
    {
        System.out.print("What is your name?");
        String name = keyboard.nextLine();
        System.out.println("Hello there," + name);
        rerun();
    }

    public static void rerun()
    {
        System.out.println("Would you like to enter another name? Please enter \"yes\" or \"no\".");
        String reply = keyboard.nextLine();

        if (reply.equals("yes")) getName();
        else System.exit();
    }

}

首先,我们调用该getName()方法并运行一次。然后我们调用该rerun()方法。此方法将测试我们是否要重新运行程序。如果用户输入“是”,那么我们重复整个过程。如果我们输入“yes”以外的任何内容,程序就会退出。

除了您的代码未完成这一事实之外,您的代码唯一真正的问题是您尝试将字符串与==运算符进行比较。请参阅 MadProgrammer 的回答,了解为什么这是错误的。

于 2013-04-08T01:24:45.307 回答
0

用这个

import java.util.*;
public class prob13 {
public static void main(String [] args)
  {
    // Create a Scanner object to read input.
    Scanner keyboard = new Scanner(System.in);

    //Get the user's name.
    while(true){
        System.out.print("What is your name?");
        String name = keyboard.nextLine();
        System.out.println("Hello there," + name);

        System.out.println("Would you like to enter another name? Please enter Yes Or No.");
        String reply = keyboard.nextLine();

        if(reply.equals("no"))
            break;
    }
 }
}

这样做的原因是只要答案不是“否”就循环。

或者如果您希望答案始终是肯定的,您可以使用它

import java.util.*;
public class prob13 {
public static void main(String [] args)
  {
    // Create a Scanner object to read input.
    Scanner keyboard = new Scanner(System.in);
        String reply="yes";
    //Get the user's name.
    while(reply.equals("yes")){
        System.out.print("What is your name?");
        String name = keyboard.nextLine();
        System.out.println("Hello there," + name);

        System.out.println("Would you like to enter another name? Please enter Yes Or No.");
        reply = keyboard.nextLine();

    }
 }
}
于 2013-04-08T01:28:14.707 回答
0

最简单(也可能是最清晰)的方法是将要重复的内容包装在 do-while 语句中

public static void main(String [] args)
  {
    // Create a Scanner object to read input.
    Scanner keyboard = new Scanner(System.in);

    String reply;
    do {
        //Get the user's name.
        System.out.print("What is your name?");
        String name = keyboard.nextLine();
        System.out.println("Hello there," + name);

        System.out.println("Would you like to enter another name? Please enter Yes Or No.");
        reply = keyboard.nextLine();

    } while ("yes".equals(reply));
  }
}

回复变量必须在块之前声明,因为它是在循环条件中访问的(一个变量只在它声明的块中可见,所以如果回复是在循环中声明的,那么循环条件将无法使用它)。

我更改了循环条件,因为 == 运算符通过引用比较字符串,即它将检查双方是否指向同一个字符串对象。相反,equals 方法检查字符串的内容是否相等(即它们以相同的顺序包含相同的字符)。

于 2013-04-08T01:39:21.550 回答