简体   繁体   English

如何使我的while循环与用户提示一起工作?

[英]How to make my while-loop along with the user prompts work?

Over here, I have a robot application which consists of mainly three user prompts. 在这里,我有一个机器人应用程序,主要由三个用户提示组成。 Right after the third prompt, and it has to loop back to the first one and this goes on until when the user types QUIT which will then display all the details from the vector elements. 在第三个提示之后,它必须循​​环回到第一个提示,并且一直持续到用户键入QUIT为止,然后QUIT将显示矢量元素中的所有详细信息。 Right now, I'm having problem with the application as when I put the first prompt in the while-loop and when the application starts, nothing happens unless I put it out of the loop but this will not be looping back. 现在,我在应用程序方面遇到问题,因为当我将第一个提示放入while-loop ,当应用程序启动时,除非我将其退出循环,否则什么也不会发生,但这不会循环回来。 So could someone give me some guidance here as I'm a newbie in programming. 因为我是编程新手,所以有人可以在这里给我一些指导。 Thanks so much! 非常感谢!

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;
}
}

Main method class: 主要方法类别:

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"));
}           
}

The logic of your program is a bit off, and this is giving you problems. 程序的逻辑有些偏离,这给您带来了问题。 You want to keep looping over all three prompts until the user types 'QUIT'. 您要一直循环浏览所有三个提示,直到用户键入“ QUIT”为止。 This means that the first prompt must be inside the loop. 这意味着第一个提示必须在循环内。 So the trick is to make your while -condition different. 因此,诀窍是使您的while有所不同。 Instead of checking whether there is any input (with hasNext() ), check whether the last input was 'QUIT'. 而不是检查是否有任何输入(使用hasNext() ),而是检查最后一个输入是否为'QUIT'。

One way to do that is to set a simple boolean flag outside the loop and then use that as your while -condition, like so: 一种方法是在循环外设置一个简单的boolean标志,然后将其用作while -condition,如下所示:

 boolean quit = false;

 while(!quit) {
   System.out.println(prompt);
   String a = userInput.next(); 
   quit = "QUIT".equals(a); 
   ...
 }

This is actually still not ideal, since this will continue showing all three prompts, even when the user types 'QUIT' on the first one. 这实际上仍然不是理想的,因为即使用户在第一个提示上键入“ QUIT”,它也会继续显示所有三个提示。 I assume you want to jump out of the loop immediately when that happens, so you might consider making an infinite loop (using while(true) , for example), and just immediately jumping out of the loop (with a break ) when the user enters the quit command. 我假设您想在发生这种情况时立即跳出循环,因此您可以考虑进行无限循环(例如,使用while(true) ),而当用户使用时立即跳出循环(带有break )输入quit命令。

This should in line with what you want. 这应该符合您的要求。 There are a few changes - one your code has the prompt displayed twice, second you want them to be able to QUIT before having any robots so a do-while loop would be a little inconvenient, and lastly at the end of the loop I use prompt1 again and if they quit, print all the robots if there are any instead of printing them out in each iteration of the loop (because at that point if you just print it to the console you aren't using the vector at all. 有一些更改-一个代码使提示符显示两次,第二个希望您在拥有任何机器人之前能够退出,因此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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM