Интеграция части кода в мое задание - PullRequest
0 голосов
/ 28 октября 2019

Итак, у меня есть этот фрагмент кода, который я хочу вставить в свое задание, но я не знаю, как мне это сделать. И, в частности, я хочу, чтобы после каждой отдельной ставки, которую пользователь делает в качестве примера, появилось сообщение о возможности выхода: «Вы ничего не выиграли, попробуйте снова! Если вы хотите выйти, просто нажмите q». Код, о котором идет речь, такой:

if (input.equals(exitKey)); {
    System.out.println("Your total balance is " + userCoins);
        System.out.println("Your total spins were " + numOfSpins);
        System.out.println("Your total wins are " + win + " and total losses are " + loss);
        System.out.printIn("Goodbye! We hope to see you play again!")
        //and a line of code here that terminates the program
}

А вот код, в который я хочу вставить его:

import java.util.Random;
import java.util.Scanner;

public class SlotMachine {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner input = new Scanner(System.in);
        Random randInt = new Random();


        System.out.println("Welcome to The Royal Casino!");

        System.out.println("");
        System.out.println("Remember, getting:");
        System.out.println("### means x2"); 
        System.out.println("$$$ means x5");
        System.out.println("&&& means x20");
        System.out.println("@@@ means x100");

        System.out.println("");
        System.out.println("Now, you will be starting off with 100 coins. Good luck!");

        double userCoins = 100.00; //Total number of coins you have
        double userBet = 0; //The user's bet with how much coins he/she has
        double numOfSpins = 0; //The number of spins the user does
        double win = 0;
        double loss = 0;
        String quitKey = "q"; //The key the user enters to quit

        do {

            do {

             System.out.print(" How much would you like to bet? ");
             System.out.println("");
             userBet = input.nextDouble();            
             userCoins = (userCoins - userBet);
             numOfSpins++;

             if (userBet > userCoins || userBet > 10) {
                 System.out.println("The number of coins you can bet can only be from 1-10!");

             }
             else if (userBet >= 1 && userBet <= 10) {

             }

            } while (userBet < 1 || userBet > 10);

            int spin1 = randInt.nextInt(4) + 1;
            int spin2 = randInt.nextInt(4) + 1;
            int spin3 = randInt.nextInt(4) + 1;
            String spinName1 = " ", spinName2 = " ", spinName3 = " ";

            switch (spin1)
            {
                case 1:
                    spinName1 = "#"; break;
                case 2:
                    spinName1 = "$"; break;
                case 3:
                    spinName1 = "&"; break;
                case 4:
                    spinName1 = "@"; break;
            }

            switch (spin2)
            {
                case 1:
                    spinName2 = "#"; break;
                case 2:
                    spinName2 = "$"; break;
                case 3:
                    spinName2 = "&"; break;
                case 4:
                    spinName2 = "@"; break;
            }

            switch (spin3)
            {
                case 1:
                    spinName3 = "#"; break;
                case 2:
                    spinName3 = "$"; break;
                case 3:
                    spinName3 = "&"; break;
                case 4:
                    spinName3 = "@"; break;
            }

            System.out.println("-------------------------------");
            System.out.printf("%-12s%-10s%5s\n", spinName1, spinName2, spinName3);
            System.out.print("-------------------------------\n");

            if (spin1 == 1 && spin2 == 1 && spin3 == 1)
            {
                userCoins = userCoins + (userBet * 2);
                win++;
                System.out.printf("3 in a row! You have won: $%.2f",(userBet * 2));
                System.out.println("You have spun " + numOfSpins);
                System.out.printf(" You currently have: $%.2f", userCoins);
            }
            else if (spin1 == 2 && spin2 == 2 && spin3 == 2)
            {       
                userCoins = userCoins + (userBet * 5);
                win++;
                System.out.printf("3 in a row! You have won: $%.2f",(userBet * 5));
                System.out.println("You have spun " + numOfSpins);
                System.out.printf(" You currently have: $%.2f", userCoins);
            }
            else if (spin1 == 3 && spin2 == 3 && spin3 == 3)
            {
                userCoins = userCoins + (userBet * 20);
                win++;
                System.out.printf("3 in a row! You have won: $%.2f",(userBet * 20));
                System.out.println("You have spun " + numOfSpins);
                System.out.printf(" You currently have: $%.2f", userCoins);
            }
            else if (spin1 == 4 && spin2 == 4 && spin3 == 4)
            {
                userCoins = userCoins + (userBet * 100);
                win++;
                System.out.printf("3 in a row! You have won: $%.2f",(userBet * 100));
                System.out.println("You have spun " + numOfSpins);
                System.out.printf(" You currently have: $%.2f", userCoins);
            }
            else
            {
                loss++;
                System.out.println("You have won nothing. Try again!");
                System.out.println("You have spun " + numOfSpins);
                System.out.printf(" You currently have: $%.2f", userCoins);

            }
        } while (userCoins > 0);
        System.out.println(" Oops! You have no more coins. Maybe next time you'll be more lucky!");
        System.out.println("Your total balance is " + userCoins);
        System.out.println("Your total spins were " + numOfSpins);
        System.out.println("Your total wins are " + win + " and total losses are " + loss);
    }

}

Спасибо!

1 Ответ

0 голосов
/ 28 октября 2019

Согласно MarsAtomic, вы буквально на расстоянии нескольких строк от того, что было там, основная линия, на которой нужно сосредоточиться, это "userBet = input.nextDouble ();"- вам нужно как-то приспособиться к символу "q" - одна быстрая вещь, на которую стоит обратить внимание, - приведение.

Также - когда я запустил ваш код, я заметил логическую ошибку. Линия, которая проверяет, находится ли ставка между 1 и 10, вычитает деньги.

if (userBet > userCoins || userBet > 10) {
                 System.out.println("The number of coins you can bet can only be from 1-10!");

}

Итак, после запуска и внесения нескольких ставок, это мой баланс:

Now, you will be starting off with 100 coins. Good luck!
 How much would you like to bet? 
10
-------------------------------
&           #             #
-------------------------------
You have won nothing. Try again!
You have spun 1.0
 You currently have: $90.00 How much would you like to bet? 
30
The number of coins you can bet can only be from 1-10!
 How much would you like to bet? 
10
-------------------------------
@           &             &
-------------------------------
You have won nothing. Try again!
You have spun 3.0
 You currently have: $50.00 How much would you like to bet?

Это связано с тем, где вы поместили логику для вычета суммы, необходимой для вращения.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...