Guessing Game

/** * The one and only guessing game. * * @author Sebastian Garrubbo * @version 09/12/17 */ import java.util.*; public class guessingGame{ public static void main(String args[]){ String playAgain; int rng; //random int guess; boolean proceed = true; Scanner scan = new Scanner(System.in); while (proceed){ int tries = 3; rng = (int) (Math.random( ) * (10)) + 1; guess = 0; int win = 0; System.out.println("Play the one and only guessing game!."); System.out.println("Please guess a number between 1 and 10."); while (win ==0 && tries > 0){ guess = scan.nextInt(); System.out.println(); if (rng == guess){ win = 1; } else if (tries >= 0){ tries--; System.out.println("Sorry, wrong!"); if (tries > 1){ System.out.println(tries + " tries remaining."); } if (tries == 1){ System.out.println("1 try remaining."); } } } if (win == 1){ System.out.println("Lucky guess! The correct number was " + rng + "."); } else{ System.out.println("You lose!"); } System.out.println("To play again, press 1. Press any other key to exit."); System.out.println(); int cont = scan.nextInt(); if(cont != 1){ proceed = false; //System.out.println("Thanks for playing the one and only guessing game! Please play again another time."); } } } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.