mercredi 8 juillet 2015

Forcing a valid If/Else Scanner input from Console

Basically, I want the user to pick either a Dwarf or Human. The problem is, if another choice is made, such as "Cyclops" the process ends.

Below is my code.

//Race.java
public class Race{
    private String race;
    private int might;
    private int reflex;
    private int durability;
    private int smarts;
    private int bravado;

    public Race(){
    }
    public String getRace(){
        return race;
    }
    public void setRace(String race){
        this.race = race;
    }
    public int getMight(){
        return might;
    }
    public void setMight(int might){
        this.might = might;
    }
    public int getReflex(){
        return reflex;
    }
    public void setReflex(int reflex){
        this.reflex = reflex;
    }
    public int getDurability(){
        return durability;
    }
    public void setDurability(int durability){
        this.durability = durability;
    }
    public int getSmarts(){
        return smarts;
    }
    public void setSmarts(int smarts){
        this.smarts = smarts;
    }
    public int getBravado(){
        return bravado;
    }
    public void setBravado(int bravado){
        this.bravado = bravado;
    }
}

//RaceCreator.java
import java.util.Scanner;

public class RaceCreator{
    public static void main(String[] args) {
        Race dwarf = new Race();
        dwarf.setRace("Dwarf");
        dwarf.setMight(4);
        dwarf.setReflex(2);
        dwarf.setDurability(4);
        dwarf.setSmarts(3);
        dwarf.setBravado(2);

        Race human = new Race();
        human.setRace("Human");
        human.setMight(3);
        human.setReflex(3);
        human.setDurability(3);
        human.setSmarts(3);
        human.setBravado(3);

        System.out.println("Are you Human or Dwarf?");
        Scanner scanRace = new Scanner(System.in);
        String inputRace = scanRace.next();
        //System.out.println("Hello down there " + inputRace + "!");
        if (inputRace.equalsIgnoreCase("Dwarf")) {
            System.out.println("Greetings " + dwarf.getRace() + "!");
            System.out.println("Your Might is " + dwarf.getMight() + " ... Impressive!");
            System.out.println("Your Reflex is " + dwarf.getReflex() + " ... Below Average.");
            System.out.println("Your Durability is " + dwarf.getDurability() + " ... Impressive!");
            System.out.println("Your Smarts are " + dwarf.getSmarts() + " ... Average.");
            System.out.println("Your Bravado is " + dwarf.getBravado() + " ... Below Average.");
        } else if (inputRace.equalsIgnoreCase("Human")) {
            System.out.println("Greetings " + human.getRace() + "!");
            System.out.println("Your Might is " + human.getMight() + " ... Average.");
            System.out.println("Your Reflex is " + human.getReflex() + " ... Average.");
            System.out.println("Your Durability is " + human.getDurability() + " ... Average.");
            System.out.println("Your Smarts are " + human.getSmarts() + " ... Average.");
            System.out.println("Your Bravado is " + human.getBravado() + " ... Average.");
        }
    }
}

I have looked up numerous tutorials on "while" "do" "break" but they are all using integers, or just 1 option, instead of 2 (Dwarf or Human).

Aucun commentaire:

Enregistrer un commentaire