-1

好的,所以这个程序应该从用户那里得到一个赌注,并打印出一些与该赌注相等的随机生成的数字(即赌注 = 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%。):

在此先感谢您的帮助!

4

1 回答 1

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 美元 :)

于 2013-10-03T03:33:48.773 回答