好的,这让我整整 48 小时都在琢磨 :(
添加游戏假设给出 2 个随机数(添加它们),跟踪正确回答所需的尝试次数,并根据最多 4 次尝试的次数给出分数,在 4 次尝试时,数字重置你得到零分,它告诉你答案然后再生成 2 个数字。在任何时候,如果您的猜测包括 99,则游戏结束。不使用 afor
或do while
包括 a break;
。
我如何让它在什么时候停止ans == 99
?
import java.util.Scanner;
import java.lang.Math;
public class AdderGame {
public static void main (String [] args){
int ans = 0;
int tries;
int score = 0;
Player one = new Player (0 , 0);
while (ans != 99) {
tries = one.getTry();
ans = one.getAns();
switch (tries) {
case 1: score += 5; break;
case 2: score +=3; break;
case 3: score += 1; break;
case 4: score += 0;
System.out.print("The right answer is "); break;
}
}
System.out.print("Your Score is " + score);
}
}
import java.lang.Math;
import java.util.Scanner;
public class Player {
int ran1, ran2;
int tries = 1;
int answer;
int guess;
public Player(int trys, int ans) {
tries = trys;
answer = ans;
}
public int getTry() m{
Scanner input = new Scanner(System.in);
ran1 = (int) (19 * Math.random());
ran2 = (int) (19 * Math.random());
answer = ran1 + ran2;
do {
System.out.print(ran1 + " + " + ran2 + ": ");
guess = input.nextInt();
if (guess == answer) {
return (tries);
} else {
tries += 1;
}
} while (tries <= 4 && answer != guess || guess != 99);
return (tries);
}
public int getAns() {
return (answer);
}
}