1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
package Textprg;
import java.util.Scanner;
public class Guessingame1
{
public static void main(String[]args)
{
System.out.println("Let's play a game,I'll pick a nb between");
System.out.println("1 and 100,and you try to guess it");
boolean playagain;
do{
playgame();
String saisieuser="";
while
(!saisieuser.equals("y")&& !saisieuser.equals("n"))
{
System.out.println("Would you like to play agin(y/n)");
Scanner sc=new Scanner(System.in);
saisieuser=sc.nextLine();
}
playagain=(saisieuser.equals("y"));
}
while(playagain);
System.out.println("Thanks for playing.Goodbye.");
}
static void playgame()
{
int computersnb;
int usersguess=-1;
int guesscount=0;
computersnb=(int)(100*Math.random())+1;
System.out.println("");
System.out.println("What is your first guess?");
while(true)
{
Scanner sc1=new Scanner(System.in);
try
{
usersguess=sc1.nextInt();
}
catch(Exception e)
{
System.out.println("Only use an integer,please!");
usersguess=-1;
}
guesscount++;
if(usersguess==computersnb)
{
System.out.println("You get in "+guesscount+" Guesses!My nb was "+computersnb);
break;
}
if (guesscount==6)
{
System.out.println("You didn't get the nb in 6 guesses");
System.out.println("You lose.My nb was "+computersnb);
break;
}
if(usersguess<computersnb && usersguess !=-1)
System.out.println("That is too bas, try again");
else if(usersguess>computersnb)
System.out.println("That's too high Try again");
}
System.out.println("");
}
} |