我必须在我的代码中使用循环,以便当有人输入yes时,他们可以根据需要重复输入他们的名字,但我不知道如何执行此操作。任何帮助表示赞赏,这是我的代码:
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")
{
}
}
}
答案 0 :(得分:7)
这个reply == "yes"
不是你在Java中比较String
的方式。这比较了内存位置,而不是内容(并且内存位置不太可能相等)。
相反,您需要使用reply.equals("yes")
,或者如果您不关心进行案例比较,则可以使用reply.equalsIgnoreCase("yes")
代替
do {
// The remainder of your code...
} while (reply.equalsIgnoreCase("yes"));
<强>更新强>
您可能还希望阅读The while and do-while statements和The for Statement,其中介绍了使用Java循环的基础知识
答案 1 :(得分: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();
}
}
答案 2 :(得分: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()
方法。此方法将测试是否要重新运行该程序。如果用户输入“是”,那么我们重复整个过程。如果我们输入除“是”之外的任何内容,程序将退出。
除了您的代码未完成之外,您的代码唯一真正的问题是您尝试将字符串与==
运算符进行比较。请参阅MadProgrammer的答案,了解为什么这是错误的。
答案 3 :(得分: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();
}
}
}
答案 4 :(得分:0)
最简单(也可能是最清晰)的方法是在do-while statement中包装您想要重复的内容:
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));
}
}
必须在块之前声明reply变量,因为它是在循环条件下访问的(变量只在声明的块中可见,因此如果在循环中声明了回复,则它不可用于循环条件)。
我改变了循环条件,因为==运算符通过引用比较字符串,即它将检查双方是否指向同一个String对象。相反,equals方法检查字符串的内容是否相等(即它们包含相同顺序的相同字符)。
答案 5 :(得分:-1)
您希望在回复为是时继续运行循环,因此您需要一个回复变量。
public static void main(String [] args)
{
String reply = "yes";
while(reply.equals("yes")) {
// 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.");
reply = keyboard.nextLine();
}
}
}
另外,请注意您不能使用reply == "yes"
,因为这永远不会成立。这与String在内存中的含义有关。相反,请使用reply.equals("yes")
。