Click here to Skip to main content
15,896,430 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This code is written in java. I would like to build a GUI around this simple game. I want the gui to take input, also to display what would normally be to a terminal.

What I have tried:

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

/* The purpose of this game is to guess the same number as the number as the computer comes up with.
* When you guess the right number your money is multiplied by your bet, when you lose you bet is take from you money.
* When your completely out of money the game ends. the point in this game is not to get into betting,
* this program was made for practice and fun only. Although if anyone reading this wants to give me money feel free (; 
*/
public class Maingame {
    static int money = 100;
    static int userguess;
    static int computernumber;

    public static void main(String[] args) {
        System.out.println("Welcome to Isaac's Guessing Game Attempt number two");
        System.out.println("Pick a number between 1 and 10");

        playgame();
    }

    public static void playgame() {
        Scanner in = new Scanner(System.in);
        userguess = in.nextInt();
        computernumber = new Random().nextInt(5) + 1;

        System.out.println(" Make a bet.");
        Scanner bet = new Scanner(System.in);
        int Bet = bet.nextInt();
        int ng = 0;

        if (computernumber == userguess) {
            money *= Bet;
            ++ng;

            System.out.println("Well done You guessed the right number.");
            System.out.println("Your money is now £" + money + ". Don't spend it all in one place.");
            System.out.println("your number of guess is " + ng + ".");
            System.out.println("Make a new guess.");
        } else {
            money -= Bet;
            ++ng;

            System.out.println("Oh no you guessed the wrong number.");
            System.out.println("You have lost £" + Bet + ". You only have £" + money + " left.");
            System.out.println("Guess again");
        }

        if (money >= 0) {
            playgame();
        } else {
            System.out.println("Your out of " + "money. Game Over!!!!!!!!!!");
        }

    }
}
Posted
Updated 24-Jul-18 16:22pm
v2

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900