How to stop a program if answer to question is false - java

I had a prompt to write a program in java that asks users for the base and height of a triangle. Then the program is supposed to find the area. I started the program asking the user if they are willing to help me. If they say "Yes" then the program continues. If they say anything other than "Yes" I want the program to stop. I don't know how to get it to reply to the user, then stop running. My code is below. If you have time to review the full thing that would be awesome! I'm brand new at this.
public static void main(String[] args) {
System.out.println("Hey can you help me practice finding the area of a triangle?");
String answerFirstQuestion = "Yes";
//if (answerFirstQuestion = Yes){
//}else{
// System.exit(0);
//}
//^^This part isn't apart of the prompt. I wanted to ask the user a question, then have them answer "Yes". If they entered anything other than that, I wanted the program to print "Oh ok, that's fine. Thanks anyways." then stop. Could I get some feedback on how to accomplish this.
switch (answerFirstQuestion) {
case "Yes":
System.out.println("Thanks kid!\nJust give me a random base and height for our imaginary triangle.\nI'll then use the equation b*h/2 to see if my program can find the area.");
break;
default:
System.out.println("Oh ok, that's fine.\nThanks anyways.");
}
int base;
base = 6;
int height;
height = 12;
int area = base*height/2;
area = base*height/2;
System.out.println("The base of the triangle is? " + base);
System.out.println("The height of the triangle is? " + height);
System.out.println("Area of Triangle is: " + area);
System.out.println("Cool I guess it works, thanks again!");
}
}

The java.lang.System.exit() method exits the current program by terminating running Java virtual machine.
The following example shows the usage of java.lang.System.exit() method.
// A Java program to demonstrate working of exit()
import java.util.*;
import java.lang.*;
class GfG
{
public static void main(String[] args)
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
for (int i = 0; i < arr.length; i++)
{
if (arr[i] >= 5)
{
System.out.println("exit...");
// Terminate JVM
System.exit(0);
}
else
System.out.println("arr["+i+"] = " +
arr[i]);
}
System.out.println("End of Program");
}
}
I hope that this helps you :).

Just return.
System.out.println("Hey can you help me practice finding the area of a triangle?");
String answerFirstQuestion = "Yes";
switch (answerFirstQuestion) {
case "Yes":
System.out.println("Thanks kid!\nJust give me a random base and height for our imaginary triangle.\nI'll then use the equation b*h/2 to see if my program can find the area.");
break;
default:
System.out.println("Oh ok, that's fine.\nThanks anyways.");
return;
// Return from the main function. The code below this will NOT be executed
}

Related

How Do I Add Charges Based On Yes or No?

Working on a coding assignment and we have to incorporate methods and returning it. However, that is the beside the point. I am struggling on the price calculation portion. Okay, here's the gist of what I am stuck with. When the program asks, is your car and import. If the answer is yes, you will be charge with a 7% import tax, if no, the charge is negated. Now, the program asks for four services, depending on yes or no, the charge will be added based on their answer.
So, if the user wants an Oil Change and Tune Up and if their car is an import the services price will be added along with the import tax or if the car is not an import but want all services then the charges will be displayed without the import tax added, etc... The final outcome would display "Before taxes, it would be $# and after taxes, your total is..." An if statement is required but I do not know where to start because I am mainly struggling with the yes or no... Any help? My professor advised me to use an accumulator so I added one.. No avail, I am lost, any help would be greatly appreciated.
import java.util.Scanner;
public class CarMaintenance
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String car;
//capture car
System.out.println("What is the make of your car");
car = keyboard.nextLine();
String answer;
boolean yn;
System.out.println("Is your car an import?");
while (true)
{
answer = keyboard.nextLine();
if (answer.equalsIgnoreCase("yes"))
{
yn = true;
break;
}
else if (answer.equalsIgnoreCase("no"))
{
yn = false;
break;
}
else
{
System.out.println("Sorry, I didn't catch that. Please answer yes or no");
}
}
String[] services = {"Oil Change", "Coolant Flush", "Brake Job", "Tune Up"};
for(int i=0; i<services.length; i++)
{
System.out.println("Do you want a " +services[i]);
answer = keyboard.next();
}
double amount = 0;
double[] price = {39.99, 59.99, 119.99, 109.99};
for(int i =0; i<price.length; i++)
{
amount = (price[i] + price [i]);
}
// double total = 0;
double c = 0;
c = car(amount);
// c = car(total);
System.out.println("The price for your services for your" + " " + car + " " + "is" + " "+ c + ".");
}
public static double car(double amount)
{
return (double) ((amount-32)* 5/9);
}
}
Before I talk about your code, I recommend you read this article.
Rubber Duck Debugging.
It is a bit flippant, but there is a deep truth to it. And it maps neatly with a debugging technique that I was taught .... umm ... 40 years ago.
First I draw your attention to this:
System.out.println("Is your car an import?");
while (true)
{
answer = keyboard.nextLine();
if (answer.equalsIgnoreCase("yes"))
{
yn = true;
break;
}
else if (answer.equalsIgnoreCase("no"))
{
yn = false;
break;
}
else
{
System.out.println("Sorry, I didn't catch that. Please answer yes or no");
}
}
That code does a pretty good job of asking a "yes or no" question and getting the answer. It includes code to retry if the user responds with something that isn't recognizable as "yes" or "no".
So I'm guessing that someone wrote that code for you ... or you found it somewhere. You need to read that code carefully, and make sure you understand exactly how it works.
Next, I draw your attention to this:
for(int i=0; i<services.length; i++)
{
System.out.println("Do you want a " +services[i]);
answer = keyboard.next();
}
This code does not make sense. (Explain it to your rubber duck!)
Your earlier code is a good example of how to ask a yes / no question. But this is nothing like that:
You are not testing for "yes" or "no".
You are not repeating if the user gives an unrecognizable answer
You are not even storing the answer .... for each distinct service.
Then this:
double amount = 0;
double[] price = {39.99, 59.99, 119.99, 109.99};
for(int i =0; i<price.length; i++)
{
amount = (price[i] + price [i]);
}
It looks like this is trying to add the price of service items to the overall amount. But:
You are doing it for every service item, not just the items that the customer asked for.
You are actually doing the calculation incorrectly anyway. (Talk to your Rubber Duck about it!)
How to fix this?
Some things should be obvious from what I said above.
The problem of remembering the answers from the first for loop and using them in the second for loop ... is actually a problem you can / should avoid. Instead, combine the two loops into one. Here's some pseudo code:
for each service:
ask the user if he wants this service
if the user wants this service:
add the service cost to the total.
Understand the logic of that, and translate it into Java code. (Yes I could write it for you, but that defeats the purpose!)
If you would want the charges to be added to the value of answer you should do it in only one for loop and because the index of services and prices line up just use and if statement to check if the response is yes
String[] services = {"Oil Change", "Coolant Flush", "Brake Job", "Tune Up"};
for(int i=0; i<services.length; i++)
{
System.out.println("Do you want a " +services[i]);
answer = keyboard.next();
if(answer.equalsIgnoresCase()){
answer += prices[i]
}
}
double amount = 0;
double[] price = {39.99, 59.99, 119.99, 109.99};
for(int i =0; i<price.length; i++)
{
amount = (price[i] + price [i]);
}

(Java) Text Adventure: Closing the game if input is entered

I am trying to modify my friend's simple Text Adventure in Java. I am trying to add a ‘quit’ option to the menu, and modify the loop in the main() method so that it exits if the user enters this command.
But this code stops the game from the very beginning no matter what the input is:
while (playerLocation==10);
System.out.println("You won the game");
break;
And this produces an error:
while(scanner.equals("9")) {
System.out.println("You have quit the game.");
break;
}
Full code below:
package practice;
import java.util.Scanner;
public class practice
{
private final int NO_EXIT = 99999; // indicates that there is no exit in that direction
private int map[][] = {{NO_EXIT,1,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT}, // 1
{2,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT}, // 2
{NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,3,NO_EXIT,NO_EXIT}, // 3
{NO_EXIT,NO_EXIT,4,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT}, // 4
{NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,5}, // 5
{NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,6,NO_EXIT,NO_EXIT}, // 6
{NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,7}, // 7
{NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,8,NO_EXIT}, // 8
{9,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT}, // 9
{NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT}}; // 10
private String description[] = {"(Stage 1) stranded in dead-looking forest. You are trying to find your way out of here. You then see a pathway heading east.",
"(Stage 2) now in a large abandoned military base. There is no other way around and you see another pathway heading north.",
"(Stage 3) now brought by the path you took here in an abandoned farmhouse. You now see a cave up ahead on the northwest",
"(Stage 4) now inside a cave. It has an entrance to the west.",
"(Stage 5) currently in the left wing of the cave. On the southwest there is an entrance leading somwhere.",
"(Stage 6) halfway through to your escape. You are in the bottom of the cave. \nThere is a small hole ahead on your northeast, but you can fit right through",
"(Stage 7) inside a section where there is a body of water. On your southwest direction, you see a ladder leading somewhere.",
"(Stage 8) inside a slightly dark hallway. You see a light ahead in the southeast direction.",
"(Stage 9) now outside the cave. You see a path heading north connecting towards an establishment.",
"(Stage 10) finally on your destination!"};
private String objectName[] = {"An adult magazine", "A fully loaded .45 calibre gun", "A motherboard from the 90s", "A syringe",
"A skull", "A worn out pair of shoes", "A Matchbox", "A Wooden Plank", "Illegal Drugs", "A bottle of whiskey" };
private int objectLocation[] = {0, 1, 3, 1, 2, 0, 7, 6, 2, 1};
private int playerLocation = 0;
// Prints out a description of the location the player is currently in, including a list of any objects at that location
private void describeLocation()
{
System.out.println();
System.out.println("You are " + description[playerLocation]);
System.out.println("\n\t\tIn this area, you found: ");
int numObjects = 0;
for (int i=0; i<objectLocation.length; i++)
{
if (objectLocation[i]==playerLocation)
{
System.out.println("\n\t\t" + objectName[i]);
numObjects++;
}
}
if (numObjects==0)
{
System.out.println("\n\t\tNo Item(s)");
}
System.out.println();
}
// implements a simple text-driven menu
private int getMenuSelection(Scanner s)
{
// display menu
System.out.println("What do you want to do?");
System.out.println("1. Go North");
System.out.println("2. Go East");
System.out.println("3. Go West");
System.out.println("4. Go South");
System.out.println("5. Go North East");
System.out.println("6. Go North West");
System.out.println("7. Go South East");
System.out.println("8. Go South West");
System.out.println("9. Quit Game");
System.out.print("Enter command (1-9): ");
// get and return the user's selection
return s.nextInt();
}
// try to move in the specified direction
private void move(int direction)
{
int nextLocation = map[playerLocation][direction];
if (nextLocation==NO_EXIT)
{
System.out.println("\nThere is no way there. Try again!");
}
else
{
playerLocation = nextLocation;
}
}
public void startGame()
{
Scanner scanner = new Scanner(System.in);
do
{
describeLocation();
int selection = getMenuSelection(scanner);
move(selection-1);
while (playerLocation==10);
System.out.println("You won the game");
break;
} while (true);
}
public static void main(String[] args)
{
practice adv = new practice();
adv.startGame();
}
}
So basically what I need to know is how to put an inventory after those things are corrected.
Thank you.
One of your many problems is this code snippet:
while (playerLocation==10); // < Semi colon here means loop forever if playerLocation==10
System.out.println("You won the game");
break; // This will always happen.
You probably wanted this instead:
playerLocation = scanner.nextInt();
if (playerLocation==10){
System.out.println("You won the game");
break; // this will break the outer loop
}
First of all, the playerLocation can never be 10, your array size is only 9. You have to start counting from 0 not from 1. The first value in an array is 0.
while (playerLocation == 9) {
System.out.println("You won the game");
break;
}
The next one is, that your moving to the direction, not to the new position. If the player want to go east, you change the position to the location 2 in the array.
So think about it and work with a switch case. It could be like this.
// try to move in the specified direction
private void move(int direction) {
int x = 0;//this should be the actual position of the player
int y = 0;//this should be the actual position of the player
switch (direction) {
case 0:
System.out.println("up");
y++;
break;
case 1:
System.out.println("down");
y--;
break;
case 2:
System.out.println("left");
x--;
break;
case 3:
System.out.println("right");
x++;
break;
default:
break;
}
}

Calling local variables in other static methods?

I am supposed to write a program that selects a random number between user given constraints, and asks the user to input guesses as to what this number is. The program gives feedback to the user as to whether or not the number is higher or lower than the user's guesses. The number of guesses, the number of games, the total guesses used throughout all of the games, and the lowest number of guesses used in one game are recorded.
These results are printed. The functions that responsible for running the game (playGame()) and the functions responsible for printing these results (getGameResults()) must be in two separate methods.
My problem is, I am not sure how to get the local variables that are modified throughout the course of the method playGame() to the getGameResults() method.
getGameResults() is intended to be called in another method, continuePlayTest(), which tests the user's input to determine whether or not they wish to continue playing the game, so I don't think that calling getGameResults() will work, otherwise this test will not work either. Unless I call continuePlayTest() in playGame(), but continuePlayTest() calls playGame() in its code so that would complicate things.
We can use ONLY the concepts that we've learned. We cannot use any concepts ahead.
So far, we've learned how to use static methods, for loops, while loops, if/else statements and variables. Global variables are bad style, so they cannot be used.
CODE:
public class Guess {
public static int MAXIMUM = 100;
public static void main(String[] args) {
boolean whileTest = false;
gameIntroduction();
Scanner console = new Scanner(System.in);
playGame(console);
}
// Prints the instructions for the game.
public static void gameIntroduction() {
System.out.println("This process allows you to play a guessing game.");
System.out.println("I will think of a number between 1 and");
System.out.println(MAXIMUM + " and will allow you to guess until");
System.out.println("you get it. For each guess, I will tell you");
System.out.println("whether the right answer is higher or lower");
System.out.println("than your guess.");
System.out.println();
}
//Takes the user's input and compares it to a randomly selected number.
public static void playGame(Scanner console) {
int guesses = 0;
boolean playTest = false;
boolean gameTest = false;
int lastGameGuesses = guesses;
int numberGuess = 0;
int totalGuesses = 0;
int bestGame = 0;
int games = 0;
guesses = 0;
games++;
System.out.println("I'm thinking of a number between 1 and " + MAXIMUM + "...");
Random number = new Random();
int randomNumber = number.nextInt(MAXIMUM) + 1;
while (!(gameTest)){
System.out.print("Your guess? ");
numberGuess = console.nextInt();
guesses++;
if (randomNumber < numberGuess){
System.out.println("It's lower.");
} else if (randomNumber > numberGuess){
System.out.println("It's higher.");
} else {
gameTest = true;
}
bestGame = guesses;
if (guesses < lastGameGuesses) {
bestGame = guesses;
}
}
System.out.println("You got it right in " + guesses + " guesses");
totalGuesses += guesses;
continueTest(playTest, console, games, totalGuesses, guesses, bestGame);
}
public static void continueTest(boolean test, Scanner console, int games, int totalGuesses, int guesses, int bestGame) {
while (!(test)){
System.out.print("Do you want to play again? ");
String inputTest = (console.next()).toUpperCase();
if (inputTest.contains("Y")){
playGame(console);
} else if (inputTest.contains("N")){
test = true;
}
}
getGameResults(games, totalGuesses, guesses, bestGame);
}
// Prints the results of the game, in terms of the total number
// of games, total guesses, average guesses per game and best game.
public static void getGameResults(int games, int totalGuesses, int guesses, int bestGame) {
System.out.println("Overall results:");
System.out.println("\ttotal games = " + games);
System.out.println("\ttotal guesses = " + totalGuesses);
System.out.println("\tguesses/games = " + ((double)Math.round(guesses/games) * 100)/100);
System.out.println("\tbest game = " + bestGame);
}
}
If you cannot use "global" variables, I guess your only option is passing parameters when calling the method. If you don't know how to declare and use methods with parameters, I don't know another answer.
EDIT/ADD
After you specified your question, circumstances and posted your code I got a working solution including comments.
public class Guess {
public static int MAXIMUM = 100;
public static void main(String[] args) {
boolean play = true; // true while we want to play, gets false when we quit
int totalGuesses = 0; // how many guesses at all
int bestGame = Integer.MAX_VALUE; // the best games gets the maximum value. so every game would be better than this
int totalGames = 0; // how many games played in total
Scanner console = new Scanner(System.in); // our scanner which we pass
gameIntroduction(); // show the instructions
while (play) { // while we want to play
int lastGame = playGame(console); // run playGame(console) which returns the guesses needed in that round
totalGames++; // We played a game, so we increase our counter
if (lastGame < bestGame) bestGame = lastGame; // if we needed less guesses last round than in our best game we have a new bestgame
totalGuesses += lastGame; // our last guesses are added to totalGuesses (totalGuesses += lastGame equals totalGuesses + totalGuesses + lastGame)
play = checkPlayNextGame(console); // play saves if we want to play another round or not, whats "calculated" and returned by checkPlayNextGame(console)
}
getGameResults(totalGames, totalGuesses, bestGame); // print our final results when we are done
}
// Prints the instructions for the game.
public static void gameIntroduction() {
System.out.println("This process allows you to play a guessing game.");
System.out.println("I will think of a number between 1 and");
System.out.println(MAXIMUM + " and will allow you to guess until");
System.out.println("you get it. For each guess, I will tell you");
System.out.println("whether the right answer is higher or lower");
System.out.println("than your guess.");
System.out.println();
}
// Takes the user's input and compares it to a randomly selected number.
public static int playGame(Scanner console) {
int guesses = 0; // how many guesses we needed
int guess = 0; // make it zero, so it cant be automatic correct
System.out.println("I'm thinking of a number between 1 and " + MAXIMUM + "...");
int randomNumber = (int) (Math.random() * MAXIMUM + 1); // make our random number. we don't need the Random class with its object for that task
while (guess != randomNumber) { // while the guess isnt the random number we ask for new guesses
System.out.print("Your guess? ");
guess = console.nextInt(); // read the guess
guesses++; // increase guesses
// check if the guess is lower or higher than the number
if (randomNumber < guess)
System.out.println("It's lower.");
else if (randomNumber > guess)
System.out.println("It's higher.");
}
System.out.println("You got it right in " + guesses + " guesses"); // Say how much guesses we needed
return guesses; // this round is over, we return the number of guesses needed
}
public static boolean checkPlayNextGame(Scanner console) {
// check if we want to play another round
System.out.print("Do you want to play again? ");
String input = (console.next()).toUpperCase(); // read the input
if (input.contains("Y")) return true; // if the input contains Y return true: we want play another round (hint: don't use contains. use equals("yes") for example)
else return false; // otherwise return false: we finished and dont want to play another round
}
// Prints the results of the game, in terms of the total number
// of games, total guesses, average guesses per game and best game.
public static void getGameResults(int totalGames, int totalGuesses, int bestGame) {
// here you passed the total guesses twice. that isnt necessary.
System.out.println("Overall results:");
System.out.println("\ttotal games = " + totalGames);
System.out.println("\ttotal guesses = " + totalGuesses);
System.out.println("\tguesses/games = " + ((double) (totalGuesses) / (double) (totalGames))); // cast the numbers to double to get a double result. not the best way, but it works :D
System.out.println("\tbest game = " + bestGame);
}
}
Hope I could help.
Is it a problem passing the variables between functions? ex:
public static void getGameResults(int games, int totalGuesses, int guesses, int bestGame) {
// implementation
}
Another option, assuming this is all in one class, is using private static memeber variables. They aren't global. Then again, they might be considered 'global' by your teacher for this assignment.
Given that you've only learnt how to use static methods, your only option is to pass the information from function to function via its arguments.

Slot Machine in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am supposed to build a slot machine that has 3 display windows, each window has 6 options that could display.
I am confused what "test expression" to use after the term switch? and then how to get the program to compare the 6 cases or options (cherry, orange, plum, bell, melon, bar) to see if they match and offer a return of what they won.
import java.util.Random;
import java.util.Scanner;
public class SlotMachine
{
//This is the main method
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
Random random = new Random();
String cont = "y" or "Y";
char answer;
int money = 0;
int totalEntered = 0;
int a;
int n;
int amountWon = 0;
int dbl = money * 2;
int trpl = money * 3;
while (cont.equals("y"))OR (cont.equals("Y"))
{
a = random.nextInt(6);
n = random.nextInt(991) +10;
totalEntered += money;
System.out.println("How much money would you like to bet? ");
money = keyboard.nextInt();
switch (TestExpression????)
{
case 0:
System.out.println("Cherry");
break;
case 1:
System.out.println("Orange");
break;
case 2:
System.out.println("Plum");
break;
case 3:
System.out.println("Bell");
break;
case 4:
System.out.println("Melon");
break;
default:
System.out.println("Bar");
}
if ()
{
System.out.println("You have won $0");
}
else if ()
{
System.out.println("Congratulations, you have won $" + dbl);
amountWon += dbl;
}
else if ()
{
System.out.println("Congratulations, you have won $" + trpl);
amountWon += trpl;
}
System.out.println("Continue? Enter y = yes");
cont = keyboard.nextLine();
}
}
}
Put a there. Whatever a is it jumps to that case in the switch statement. Ex: if a is 2 it jumps to case 2 so would print "Plum"
Could I also recommend using an Enum in this case?
enum SlotOptions {
CHERRY,
ORANGE,
PLUM,
BELL,
MELON,
BAR;
}
It looks like the swithc expression is the "actual" slow machine, so you want to put a random int there. Something along the lines of switch(a).
Why? Think about how a slot machine works and then look at your code. The slot machine randomly picks a symbol for each spot (ie 1 fruit). In your code you have a case for each fruit. What is happening is you are representing each case with a number. So to determine which case, you need to pick a number. What number do you pick? Since its a slot machine you pick a random number. That is why you have a=random.nextInt();.
You don't put a test expression in a switch-statement. You put an integer value. In this case, it seems like you want a there.

How can I turn my Java app that runs in a terminal into a desktop app with a UI?

I know that their are a bunch of different ways to do this, but I am getting pretty good at working with the terminal and want to move on to creating user interfaces and desktop apps! I have written an extremely cheesy and messy "number game" and would like to make it run in an applet with buttons and all of that jazz!
public static void gameStart() {
System.out.println("Welcome to the Game of Awesome V1.2");
System.out.println("-------------------------------------");
spin();
}
public static void spin() {
int spinNum = (int)(10*Math.random());
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
System.out.println("type a number between 1 and 10:");
int guessNum = input.nextInt();
if(guessNum > 10) {
System.out.println(guessNum + " is greater than 10 you fool!");
spin();
}else if(guessNum < 1) {
System.out.println(guessNum + " is less than 1 you fool!");
spin();
}else {
System.out.println("\nSweet, looks like you chose [" + guessNum + "], good luck...");
System.out.println("\ntype \"s\" to spin and \"quit\" to, quit...");
String run = input2.nextLine();
switch (run.toLowerCase()) {
case "s":
System.out.println("\nyou spun [" + spinNum + "] and guessed [" + guessNum + "]!");
if(guessNum == spinNum) {
win();
askPlay();
}else {
lose();
askPlay();
}
break;
case "quit":
System.out.println("See ya later!");
System.out.println();
System.exit(0);
break;
default:
System.out.println("shit, you broke it! Luckily, I can fix this.");
askPlay();
System.out.println();
break;
}
}
}
public static void askPlay() {
Scanner input = new Scanner(System.in);
System.out.println("\nWanna play again? (Type \"yes\" or \"no\")");
String decideSpin = input.nextLine();
switch (decideSpin.toLowerCase()) {
case "yes":
spin();
break;
case "no":
System.out.println("\nI know it's a crappy game, thanks for playing though!");
System.out.println();
System.exit(0);
break;
default:
System.out.println("\nI'm sorry,\nI was made by a lazy "
+ "programmer and can only understand \"yes\" or \"no\"..");
askPlay();
break;
}
}
public static void win() {
System.out.println("\nWINNER: you won this insanley stupid game, of awesome! Be proud, winner. (:");
}
public static void lose() {
System.out.println("\nLOOOOOSSSSSEEEERRRR: yep, you stand with the majority with this loss.");
}
How can I do that?
I'd suggest you start by having a read through Swing Trail
The other thing you need to understand is Swing is event driven, unlike a console program where it tends to run sequentially
Well, its simple: Start learning some of Java's GUI, I would recommend one of these two:
Swing
SWT
Definitely start learning Swing. This is the standard Java GUI framework.
Consider running it in Processing (a Java Library/Applet). That's perhaps the fastest most simplest way to get something up and going. You can even export it as an app if you like. Cheers!

Categories

Resources