[英]How to make my while-loop along with the user prompts work?
在這里,我有一個機器人應用程序,主要由三個用戶提示組成。 在第三個提示之后,它必須循環回到第一個提示,並且一直持續到用戶鍵入QUIT為止,然后QUIT將顯示矢量元素中的所有詳細信息。 現在,我在應用程序方面遇到問題,因為當我將第一個提示放入while-loop
,當應用程序啟動時,除非我將其退出循環,否則什么也不會發生,但這不會循環回來。 因為我是編程新手,所以有人可以在這里給我一些指導。 非常感謝!
class KillerRobot {
private String name;
private String mainWeapon;
private int numberOfKills;
KillerRobot()
{
}
public String getName()
{
return name;
}
public String getMainWeapon()
{
return mainWeapon;
}
public int getNumberOfKills()
{
return numberOfKills;
}
public String toString()
{
return name + " used a " + mainWeapon + " to destroy " + numberOfKills + " enemies ";
}
public void setName(String a)
{
name = a;
}
public void setMainWeapon(String b)
{
mainWeapon = b;
}
public void setNumberOfKills(int c)
{
numberOfKills = c;
}
}
主要方法類別:
import java.util.Scanner;
import java.util.Vector;
class TestVector2 {
public static void main(String [] args)
{
String prompt = "Please enter the name of the Killer Robot or QUIT to finish";
String prompt2 = "Please enter the main weapon for the robot";
String prompt3 = "Please enter the number of kills for this robot";
System.out.println(prompt); //The first prompt and has to loop back unless QUIT from user
Scanner userInput = new Scanner(System.in);
Vector <KillerRobot> robotDetails = new Vector <KillerRobot>(); //adding object to Vector elements
do {
System.out.println(prompt); //The first prompt and has to loop back unless QUIT from user
robot = new KillerRobot();
String a = userInput.next();
robot.setName(a);
System.out.println(prompt2);
String b = userInput.next();
robot.setMainWeapon(b);
System.out.println(prompt3);
int c = userInput.nextInt();
robot.setNumberOfKills(c);
robotDetails.add(robot);
System.out.println(robot);
} while (!userInput.nextLine().equals("QUIT"));
}
}
程序的邏輯有些偏離,這給您帶來了問題。 您要一直循環瀏覽所有三個提示,直到用戶鍵入“ QUIT”為止。 這意味着第一個提示必須在循環內。 因此,訣竅是使您的while
有所不同。 而不是檢查是否有任何輸入(使用hasNext()
),而是檢查最后一個輸入是否為'QUIT'。
一種方法是在循環外設置一個簡單的boolean
標志,然后將其用作while
-condition,如下所示:
boolean quit = false;
while(!quit) {
System.out.println(prompt);
String a = userInput.next();
quit = "QUIT".equals(a);
...
}
這實際上仍然不是理想的,因為即使用戶在第一個提示上鍵入“ QUIT”,它也會繼續顯示所有三個提示。 我假設您想在發生這種情況時立即跳出循環,因此您可以考慮進行無限循環(例如,使用while(true)
),而當用戶使用時立即跳出循環(帶有break
)輸入quit命令。
這應該符合您的要求。 有一些更改-一個代碼使提示符顯示兩次,第二個希望您在擁有任何機器人之前能夠退出,因此do-while循環會帶來一些不便,最后在我使用的循環結束時再次提示1,如果它們退出,則打印所有機器人(如果有的話),而不是在循環的每次迭代中將它們打印出來(因為此時,如果僅將其打印到控制台,則根本不使用矢量。
public static void main(String[] args) {
String prompt1 = "Please enter the name of the Killer Robot or enter QUIT to exit.";
String prompt2 = "Please enter the main weapon for the robot";
String prompt3 = "Please enter the number of kills for this robot";
Scanner userInput = new Scanner(System.in);
Vector <KillerRobot> robotDetails = new Vector <KillerRobot>();
KillerRobot robot;
//prime the loop
System.out.println(prompt1);
String userEntry = userInput.next();
while(!userEntry.equals("QUIT")){
robot = new KillerRobot();
robot.setName(userEntry);
System.out.println(prompt2);
String b = userInput.next();
robot.setMainWeapon(b);
System.out.println(prompt3);
int c = userInput.nextInt();
robot.setNumberOfKills(c);
robotDetails.add(robot);
//verify repeat
System.out.println(prompt1);
userEntry = userInput.next();
}
if(robotDetails.size() < 1){
System.out.println("No robots");
} else {
for(KillerRobot i : robotDetails){
System.out.println(i);
}
}
System.out.println("done");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.