2015-10-20

posted Oct 20, 2015, 4:08 PM by Samuel Konstantinovich   [ updated Oct 20, 2015, 4:08 PM ]
Reminder: Quiz Thursday on Loops and Inheritance. 

//sample input checking loop using Scanner
//example of a do/while loop

public class Driver{
    public static boolean isValid(String s){
        return s.equalsIgnoreCase("W") || s.equalsIgnoreCase("B");
    }

    public static void main(String[]args){
        Scanner in = new Scanner(System.in);
        Actor p1;
        String result;
        String message = "Enter your class Warrior or Barbarian: [W/B] ";
        do{
            System.out.print(message);
            result = in.next();
            message = "Stop being wrong... enter W or B only: [W/B]";
        }while(!isValid(result));

        //result is always W or B, so if/else is ok
        if(result.equals("W")){
            p1 = new Wizard();
        }else{
            p1 = new Barbarian();
        }

        System.out.println(p1);
    }
} 
Comments