我无法在我的代码中使用循环,所以如果用户说“不”,它会将它们带回第一个问题,他们必须回答“是”。
System.out.println("Adventurer soliders need your aid in Silverkeep can you deliver supplys to them? ");
String choice;
System.out.print("So can you deliver the supplys to them? ");
choice = in.next();
if (choice.equals("Yes"))
System.out.println("Thank you so much, he are the supplys they need.");
else if (choice.equals("No"))
System.out.println("But these soliders lives depend on these supplys! You must help us.");
因此,如果他们说不,它会将它们带回System.out.print行。
System.out.println("Adventurer soliders need your aid in Silverkeep can you deliver supplys to them? ");
while (run) {
String choice;
System.out.print("So can you deliver the supplys to them? ");
choice = in.next();
if (choice.equals("Yes"))
System.out.println("Thank you so much, he are the supplys they need.");
else if(choice.equals("No"))
System.out.println("But these soliders lives depend on these supplys! You must help us.");
run = true;
}
当我尝试运行上面的代码时,它只是向控制台发送垃圾邮件,“那么你可以将耗材交付给他们吗?”
所以我哪里出错了......我尝试了多件事,但没有运气。
答案 0 :(得分:2)
你并没有在if和else之后放置{},所以只做下一个语句。 run=true;
总是在发生。
答案 1 :(得分:0)
立即试用代码,
System.out.println("Adventurer soliders need your aid in Silverkeep can you deliver supplys to them? ");
while (run) {
String choice;
System.out.print("So can you deliver the supplys to them? ");
choice = in.next();
if (choice.equals("Yes")) {
System.out.println("Thank you so much, he are the supplys they need.");
run = false;
} else if (choice.equals("No")) {
System.out.println("But these soliders lives depend on these supplys! You must help us.");
run = true;
}
}
您忘记使用布尔值false;
初始化运行值答案 2 :(得分:0)
您可以使用 执行........而 循环,因为您希望根据用户选择决定是否必须提示问题再次或不。但首先你必须向用户询问........
System.out.println(“Adventurer soliders需要你在Silverkeep的帮助,你可以向他们提供补给吗?”);
do {
String choice;
System.out.print("So can you deliver the supplys to them? ");
choice = in.next();
if (choice.equals("Yes")) {
System.out.println("Thank you so much, he are the supplys they need.");
run = false;
} else if (choice.equals("No")) {
System.out.println("But these soliders lives depend on these supplys! You must help us.");
run = true;
}
}while (run)
你需要声明运行
答案 3 :(得分:0)
你可以尝试
do{
//Your code
}while(choice.equals("No"))