好的,所以这个程序应该从用户那里得到一个赌注,并打印出一些与该赌注相等的随机生成的数字(即赌注 = 5,游戏打印 num num num num num)。然后必须通过质数方法检查这些生成的数字,如果任何数字最终成为质数,则用户赢得 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%。):
在此先感谢您的帮助!