Guessing Game

/** * The one and only guessing game. * * @author Josh Wang * @version 09/11/17 */ import java.util.*; public class guessingGame{ public static void main(String args[]){ 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("\nThis is the one and only guessing game."); System.out.println("Please guess a number between 1 and 10."); while (win ==0 && tries > 0){ //System.out.println(rng + " YOU ARE CHEATING!"); //for debugging purposes guess = scan.nextInt(); System.out.println(); if (rng == guess){ win = 1; } else { 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("You're winner! 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.\n"); int cont = scan.nextInt(); if(cont != 1){ proceed = false; } } } }

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.