好的,所以这个程序应该从用户下注,并打印出一些等于该下注的随机生成的数字(即下注= 5,游戏打印数字数字)。 然后必须通过素数方法检查这些生成的数字,并且如果任何数字最终成为素数,则用户赢得3 *他们的下注。否则,他们输了,并被提示再次播放或退出。哦,如果他们的余额达到0,游戏结束。
我有很多我需要做的事情,但有些东西我无法工作(即打印尽可能多的数字作为下注的信用)而不破坏我的代码的另一个方面(即在找到一个素数后赢得声明打印其次是剩余的非素数)。
我正在尽我所能,但我无法理解。
我做错了什么?
到目前为止,这是我的代码:
package isPrime;
import java.util.Random;
import java.util.Scanner;
public class isPrime {
public static boolean isPrime(int number) {
if (number <= 1) {
return false;
}
if (number == 2) {
return true;
}
if (number % 2 == 0) {
return false;
}
for (int i = 3; i <= Math.sqrt(number) + 1; i = i + 2) {
if (number % i == 0) {
return false;
}
}
return true;
}
public static int readNum(String prompt) {
Scanner input = new Scanner(System.in);
System.out.print(prompt);
int number = input.nextInt();
return number;
}
public static void main(String args[]) {
boolean playing = true;
int balance = 0;
int bet = 0;
int ans = 0;
int i = 0;
Random gen = new Random();
balance = readNum("Enter a starting balance: ");
System.out.println("Current balance: " + balance);
while (playing) {
bet = readNum("Enter a wager (0 to exit): ");
balance -= bet;
System.out.println("Current balance: " + balance);
if (bet == 0) {
playing = false;
System.out.println("Thank you for playing PrimeGame!!");
} else {
for (i = bet; i > 0; i--) {
int num = gen.nextInt(100000) + 1;
System.out.print(num + "\t");
if (isPrime(num)) {
System.out.println("You found a prime!!");
bet = bet * 3;
balance += bet;
System.out.println("You won " + bet);
System.out.println("Current balance: " + balance);
ans = readNum("Would you like to continue playing? (1:yes/0:no)");
if (ans == 0) {
playing = false;
}
ans = readNum("Sorry, you lost. Would you like to play again? (1:yes/0:no)");
System.out.println("Current balance: " + balance);
if (balance == 0) {
System.out.println("Out of credits! Goodbye!");
playing = false;
}
}
}
}
}
}
我真的被困了,这个价值10%。 ):
提前感谢您的帮助!
答案 0 :(得分:1)
你基本上需要移动你的“抱歉,你失去了!”生成for
循环的投注数字中的一部分。
} else {
for (i = bet; i > 0; i--) {
int num = gen.nextInt(100000) + 1;
System.out.print(num + "\t");
if (isPrime(num)) {
// i = 0; // don't reset i here
System.out.println("You found a prime!!");
bet = bet * 3;
balance += bet;
System.out.println("You won " + bet);
System.out.println("Current balance: " + balance);
ans = readNum("Would you like to continue playing? (1:yes/0:no)");
if (ans == 0) {
playing = false;
}
break; // start from outer while loop again; (i is > 0 here)
}
}
if (i == 0) { // i.e. no prime was generated
ans = readNum("\nSorry, you lost. Would you like to play again? (1:yes/0:no)");
System.out.println("Current balance: " + balance);
if (balance == 0) {
System.out.println("Out of credits! Goodbye!");
playing = false;
}
}
}
输出:
Enter a starting balance: 10
Current balance: 10
Enter a wager (0 to exit): 5
Current balance: 5
5290 12974 6400 97078 88412
Sorry, you lost. Would you like to play again? (1:yes/0:no)1
Current balance: 5
Enter a wager (0 to exit): 3
Current balance: 2
60579 77951 You found a prime!!
You won 9
Current balance: 11
Would you like to continue playing? (1:yes/0:no)1
Enter a wager (0 to exit): 11
Current balance: 0
80100 76930 57505 64299 7932 18404 57852 79483 93270 24481 You found a prime!!
You won 33
Current balance: 33
Would you like to continue playing? (1:yes/0:no)0
(PS:我赚了23美元:)