//This is the source code of java programming that talk about GuessingGame. This tutorial mostly concentrate //on Condition. Let's see
import java.util.Scanner;
class GuessingGame {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
boolean keepPlaying=true;
System.out.println("Let's play a guessing game!");
while(keepPlaying){
boolean validInput;
int num, guess;
String answer;
//Pick a random number
num=(int) (Math.random()*10)+1;
System.out.println("\nI'm thinking of number "+ "between 1 and 10.");
System.out.print("What do you think it is?");
do{
guess=sc.nextInt();
validInput=true;
if((guess<1)||(guess>10)){
System.out.println("I said, between 1 and 10." + "Try again: ");
validInput=false;
}
}
while(!validInput);
if (guess==num)
System.out.println("You are right!");
else
System.out.println("You are wrong! " + " The number was " +num);
do{
System.out.print("\n Play again? (Y or N)");
answer=sc.next();
validInput=true;
if(answer.equalsIgnoreCase(("Y")));
else if (answer.equalsIgnoreCase("N"))
keepPlaying=false;
else
validInput=false;
} while (!validInput);
}
System.out.println("Thanks for playing");
}
}
//Hope you enjoy it. :)
import java.util.Scanner;
class GuessingGame {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
boolean keepPlaying=true;
System.out.println("Let's play a guessing game!");
while(keepPlaying){
boolean validInput;
int num, guess;
String answer;
//Pick a random number
num=(int) (Math.random()*10)+1;
System.out.println("\nI'm thinking of number "+ "between 1 and 10.");
System.out.print("What do you think it is?");
do{
guess=sc.nextInt();
validInput=true;
if((guess<1)||(guess>10)){
System.out.println("I said, between 1 and 10." + "Try again: ");
validInput=false;
}
}
while(!validInput);
if (guess==num)
System.out.println("You are right!");
else
System.out.println("You are wrong! " + " The number was " +num);
do{
System.out.print("\n Play again? (Y or N)");
answer=sc.next();
validInput=true;
if(answer.equalsIgnoreCase(("Y")));
else if (answer.equalsIgnoreCase("N"))
keepPlaying=false;
else
validInput=false;
} while (!validInput);
}
System.out.println("Thanks for playing");
}
}
//Hope you enjoy it. :)
Blogger Comment
Facebook Comment