我正在制作一个简单的猜测程序。猜数字1-20,如果你弄错了,它 不能再打印那个数字。但它仍在这样做,我需要帮助弄清楚如何记住它。
请不要责怪我,我是编程新手。 =)
以下是代码朋友:
import java.util.Scanner;
public class TG_UN5EX5 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int characternumber = 0;
int characterrandom = (int) (Math.random() * (100 + 1 - 1) + 1);
int[] list = new int[20];
int e = 0;
int random = (int) (Math.random() * (20 - 1 + 1) + 1);
int guesstimes = 5;
int guess = 0;
int guessed = 0;
while (guess != random && guesstimes > 0) {
for (int b = 0; b < list.length; b++) {
e++;
list[b] = e;
if (guess == e)
list[e] = -1;
if (e >= 20)
e = 0;
if (list[e] != -1)
System.out.print("[" + list[b] + "] ");
}
System.out.print("What is the number?");
guess = scan.nextInt();
if (guess > random)
System.out.println("Too High!");
else if (guess < random)
System.out.println("Too Low!");
else
System.out.println("You got it!");
guesstimes--;
}
if (guess != random)
System.out.println("Game over! \n You ran out of tries!");
else
System.out.println("You won!");
}
}
答案 0 :(得分:1)
尝试用Set替换你的数组。它们将帮助您使用.contains()等方法跟踪以前的猜测。在设置Set的类型时,请使用Integer对象而不是int原语。
final Set<Integer> guesses = new HashSet<>();
答案 1 :(得分:0)
您可以在声明变量列表之后初始化它:
for (int b = 0; b < list.length; b++) {
if (guess == list[b])
list[b] = -1;
if (list[b] != -1)
System.out.print("[" + list[b] + "] "); }
然后将for循环重写为:
Neo4j::ActiveBase.current_session.query(cypher_query_string)