import java.util.Scanner;
import java.util.Random;
public class Driver{
public static void main(String[]args){
TheGame game = new TheGame();
Scanner s= new Scanner(System.in);
String input = new String();
do{
game.setTheNumber((int)(Math.random()*10));
do{
try{
System.out.print("Tebak 1 Angka antara 0 s/d 10:");
game.guess(s.nextInt());
}
catch(GameException e){
e.printStackTrace();
System.out.println(e.getMessage());
}
catch(InvalidAnswerException e){
e.printStackTrace();
System.out.println(e.getMessage());
}
}while(game.isStatus());
System.out.println("Maen lagi?(y/n)");
game.setStatus(true);
input = s.next();
}while(input.equals("y") || input.equals("Y"));
}
}
public class GameException extends Exception{
public GameException(){
super("Jawaban Salah");
}
}
public class InvalidAnswerException extends Exception{
public InvalidAnswerException(){
super("Jawaban tidak valid");
}
}
public class TheGame{
private int theNumber;
private boolean status=true;
public void setTheNumber(int num){
theNumber =num;
}
public void guess(int num)throws InvalidAnswerException,GameException{
if(num==theNumber){
System.out.println("Jawaban Anda Benar");
status = false;
}
else if(num>10){
throw new InvalidAnswerException();
}
else{
throw new GameException();
}
}
public void setStatus(boolean status){
this.status = status;
}
public int getNumber(){
return theNumber;
}
public boolean isStatus(){
return status;
}
}
0 komentar:
Post a Comment