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