繁体   English   中英

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

[英]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.

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