简体   繁体   English

如何将这段代码放入循环中,以便每次都要求用户输入,直到结果为“kill”?

[英]How to get this piece of code into loop, so that it asks a user input each time until the result is “kill”?

I was practicing this piece of code from the book 'Head First Java' and I'm quite confused on the positioning of the loop here.The code is for creating a kind of game that has a random dotcom word(ex: abc.com) occupying some array elements. 我正在练习“Head First Java”一书中的这段代码,我对这里的循环定位感到很困惑。这段代码用于创建一种具有随机网络词的游戏(例如:abc.com )占用一些数组元素。 here I gave that dotcom word the positions from 3 to 5 in the array, and the user tries guessing the position. 在这里,我给了dotcom字,数组中的位置从3到5,用户尝试猜测位置。

import java.util.Scanner;

public class RunTheGame {

    public static void main(String[] args) {

        MainGameClass sampleObj= new MainGameClass();
        int[] location = {3,4,5};
        sampleObj.setdotcomLocationCells(location);


        Scanner input= new Scanner(System.in);
        System.out.println("Enter your guess");
        int userGuess=input.nextInt();

        String answer = sampleObj.checkForDotcom(userGuess);
        System.out.println(answer);

    }
}

package simpleDotComGame;

public class MainGameClass {
    int[] DotcomLocationCells;
    int numOfHits=0;

    public void setdotcomLocationCells(int[] location) {
        DotcomLocationCells= location;
    }

    public String checkForDotcom(int userGuess) {
        String result="miss";
        for(int cell:DotcomLocationCells) {
            if(cell == userGuess) {
                result ="hit";
                numOfHits++;
                break;
            }
        } // end for loop

        if(numOfHits == DotcomLocationCells.length) {
            result = "kill";            
            System.out.println("The number of tries= "+numOfHits);
        }
}
do {
        <insert code where answer result is created>
} while (!answer.equals("kill"))

upd.: but you must override the equals method for correct use, because if you see how method declared in Object.class you find upd。但是你必须覆盖equals方法才能正确使用,因为如果你看到你在Object.class中声明的方法是什么

public boolean equals(Object obj) {
    return (this == obj);

Rachna, the loop will be positioned, around the following code: Rachna,循环将定位在以下代码周围:

Scanner input= new Scanner(System.in);
System.out.println("Enter your guess");
int userGuess=input.nextInt();

String answer=sampleObj.checkForDotcom(userGuess);
System.out.println(answer);
//For string you must use the equals
if ("kill".equals(answer)){
   break;
}

The reason why is that the kill command must be evaluated inside the loop to break it, and the input to continuously ask the user input until he hits all targets. 原因是必须在循环内部对kill命令进行评估以打破它,并且输入将持续询问用户输入,直到他击中所有目标。

Here's how the loop should be positioned. 这是循环应该如何定位。

String answer = "";
do{
    System.out.println("Enter your guess");
    int userGuess=input.nextInt();

    answer=sampleObj.checkForDotcom(userGuess);
    System.out.println(answer);
}
while(!answer.equals("kill"));  

NOTE : Never Check for String equality using == in Java unless you know what you're doing (also read as, unless you know the concept of String Constant Pool). 注意 :除非您知道自己在做什么,否则永远不要在Java中使用==检查字符串是否相等(除非您知道字符串常量池的概念,否则也将其读作)。

You are allowed to declare the variable before initializing it: 您可以在初始化之前声明变量:

String answer;
do {
  answer = sampleObj.checkForDotcom(userGuess);
  System.out.println(answer);
} while (!answer.equals("kill");

Also beware of the semantics of Scanner.nextInt() : if it can't parse the input as an int (for example, it contains letters), it will throw an exception, but won't jump over the invalid input. 还要注意Scanner.nextInt()的语义:如果它不能将输入解析为int(例如,它包含字母),它将抛出异常,但不会跳过无效输入。 You'll have to use Scanner.nextLine() to force-jump over it, otherwise you'll get an infinite loop. 你必须使用Scanner.nextLine()来强制跳过它,否则你会得到一个无限循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何为我的代码创建一个循环,直到用户输入正确的代码为止? - How can i make a loop for my code so it runs until user gives correct input? 如何使用扫描仪将用户输入到这段代码中 - How to get user input into this piece of code using scanner 如何让用户输入进入for循环并使程序循环直到用户输入退出 - How to get user input into a for loop and make the program loop until user enters quit 如何循环用户输入直到输入整数? - How to loop user input until an integer is inputted? 如何循环用户输入直到输入 integer? - How to loop a user input until integer is entered? vs code 中的 output window 是不可编辑的,那我们怎么在程序要求的时候输入呢? - The output window in vs code is not editable, so how we can type the input when the program asks for it? 编写一个输入验证循环,要求用户输入“是”或“否” - Write an input validation loop that asks the user to enter “Yes” or “No” 如何遍历链表并打印与输入匹配的每条数据? - How can I loop through a linked list and print each piece of data that matches the input? 如何循环直到用户输入满足条件? - How to loop until user's input satisfies a condition? 我如何循环直到用户在 java 上输入正确的输入? - How do I loop until the user put the correct input on java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM