连续限制重复的用户输入选择If / Else

时间:2015-07-14 21:31:44

标签: java if-statement

如何限制用户在每个inputFirstTag,inputSecondTag,inputThirdTag上的不同“技能”上花费“标记”

一切正常,直到第二轮才会出现丑陋的头脑。 inputSecondTag可以成为inputFirstTag的副本,inputThirdTag可以成为inputSecondTag的副本。

System.out.println("You have 3 Skills to Tag.");
        System.out.println("What skill would you like to Tag? (+25)");
        System.out.print("Small Guns, Big Guns, Energy Weapons, Unarmed, Melee Weapons, Throwing, ");
        System.out.print("First Aid, Doctor, Sneak, Lockpick, Steal, Traps, Science, Repair, ");
        System.out.println("Speech, Barter, Gambling or Outdoors?");
        Scanner scanFirstTag = new Scanner(System.in);
        String inputFirstTag = null;
        while (scanFirstTag.hasNextLine()) {
            inputFirstTag = scanFirstTag.nextLine();
            if (inputFirstTag.equalsIgnoreCase("Small Guns") || inputFirstTag.equalsIgnoreCase("Big Guns") ||
                    inputFirstTag.equalsIgnoreCase("Energy Weapons") || inputFirstTag.equalsIgnoreCase("Unarmed") ||
                    inputFirstTag.equalsIgnoreCase("Melee Weapons") || inputFirstTag.equalsIgnoreCase("Throwing") ||
                    inputFirstTag.equalsIgnoreCase("First Aid") || inputFirstTag.equalsIgnoreCase("Doctor") ||
                    inputFirstTag.equalsIgnoreCase("Sneak") || inputFirstTag.equalsIgnoreCase("Lockpick") ||
                    inputFirstTag.equalsIgnoreCase("Steal") || inputFirstTag.equalsIgnoreCase("Traps") ||
                    inputFirstTag.equalsIgnoreCase("Science") || inputFirstTag.equalsIgnoreCase("Repair") ||
                    inputFirstTag.equalsIgnoreCase("Speech") || inputFirstTag.equalsIgnoreCase("Barter") ||
                    inputFirstTag.equalsIgnoreCase("Gambling") || inputFirstTag.equalsIgnoreCase("Outdoors"))
                break;
            else System.out.println("Please choose Small Guns, Big Guns, Energy Weapons, " +
                    "Unarmed, Melee Weapons, Throwing, First Aid, Doctor, " +
                    "Sneak, Lockpick, Steal, Traps, Science, Repair, " +
                    "Speech, Barter, Gambling or Outdoors?");
        }
        if (inputFirstTag.equalsIgnoreCase("Small Guns")) {
            System.out.println("Small Guns Increased by 25!");
            smallGuns = smallGuns + 25;
            System.out.println("Small Guns: " + smallGuns);
        } else if (inputFirstTag.equalsIgnoreCase("Big Guns")) {
            System.out.println("Big Guns Increased by 25!");
            bigGuns = bigGuns + 25;
            System.out.println("Big Guns: " + bigGuns);
        } else if (inputFirstTag.equalsIgnoreCase("Energy Weapons")) {
            System.out.println("Energy Weapons Increased by 25!");
            energyWeapons = energyWeapons + 25;
            System.out.println("Energy Weapons: " + energyWeapons);
        } else if (inputFirstTag.equalsIgnoreCase("Unarmed")) {
            System.out.println("Unarmed Increased by 25!");
            unarmed = unarmed + 25;
            System.out.println("Unarmed: " + unarmed);
        } else if (inputFirstTag.equalsIgnoreCase("Melee Weapons")) {
            System.out.println("Melee Weapons Increased by 25!");
            meleeWeapons = meleeWeapons+ 25;
            System.out.println("Melee Weapons: " + meleeWeapons);
        } else if (inputFirstTag.equalsIgnoreCase("Throwing")) {
            System.out.println("Throwing Increased by 25!");
            throwing = throwing + 25;
            System.out.println("Throwing: " + throwing);
        } else if (inputFirstTag.equalsIgnoreCase("First Aid")) {
            System.out.println("First Aid Increased by 25!");
            firstAid = firstAid+ 25;
            System.out.println("First Aid: " + firstAid);
        } else if (inputFirstTag.equalsIgnoreCase("Doctor")) {
            System.out.println("Doctor Increased by 25!");
            doctor = doctor + 25;
            System.out.println("Doctor: " + doctor);
        } else if (inputFirstTag.equalsIgnoreCase("Sneak")) {
            System.out.println("Sneak Increased by 25!");
            sneak = sneak + 25;
            System.out.println("Sneak: " + sneak);
        } else if (inputFirstTag.equalsIgnoreCase("Lockpick")) {
            System.out.println("Lockpick Increased by 25!");
            lockpick = lockpick + 25;
            System.out.println("Lockpick: " + lockpick);
        } else if (inputFirstTag.equalsIgnoreCase("Steal")) {
            System.out.println("Steal Increased by 25!");
            steal = steal + 25;
            System.out.println("Steal: " + steal);
        } else if (inputFirstTag.equalsIgnoreCase("Traps")) {
            System.out.println("Traps Increased by 25!");
            traps = traps + 25;
            System.out.println("Traps: " + traps);
        } else if (inputFirstTag.equalsIgnoreCase("Science")) {
            System.out.println("Science Increased by 25!");
            science = science + 25;
            System.out.println("Science: " + science);
        } else if (inputFirstTag.equalsIgnoreCase("Repair")) {
            System.out.println("Repair Increased by 25!");
            repair = repair + 25;
            System.out.println("Repair: " + repair);
        } else if (inputFirstTag.equalsIgnoreCase("Speech")) {
            System.out.println("Speech Increased by 25!");
            speech = speech + 25;
            System.out.println("Speech: " + speech);
        } else if (inputFirstTag.equalsIgnoreCase("Barter")) {
            System.out.println("Barter Increased by 25!");
            barter = barter + 25;
            System.out.println("Barter: " + barter);
        } else if (inputFirstTag.equalsIgnoreCase("Gambling")) {
            System.out.println("Gambling Increased by 25!");
            gambling = gambling + 25;
            System.out.println("Gambling: " + gambling);
        } else if (inputFirstTag.equalsIgnoreCase("Outdoors")) {
            System.out.println("Outdoors Increased by 25!");
            outdoors = outdoors + 25;
            System.out.println("Outdoors: " + outdoors);
        }
        System.out.println();
        System.out.println("You have 2 Skills to Tag.");
        System.out.println("What skill would you like to Tag? (+20)");
        System.out.print("Small Guns, Big Guns, Energy Weapons, Unarmed, Melee Weapons, Throwing, ");
        System.out.print("First Aid, Doctor, Sneak, Lockpick, Steal, Traps, Science, Repair, ");
        System.out.println("Speech, Barter, Gambling or Outdoors?");
        Scanner scanSecondTag = new Scanner(System.in);
        String inputSecondTag = null;
        while (scanSecondTag.hasNextLine()) {
            inputSecondTag = scanSecondTag.nextLine();
            if (inputSecondTag.equalsIgnoreCase("Small Guns") || inputSecondTag.equalsIgnoreCase("Big Guns") ||  inputSecondTag.equalsIgnoreCase("Energy Weapons") || inputSecondTag.equalsIgnoreCase("Unarmed") ||  inputSecondTag.equalsIgnoreCase("Melee Weapons") || inputSecondTag.equalsIgnoreCase("Throwing") ||  inputSecondTag.equalsIgnoreCase("First Aid") || inputSecondTag.equalsIgnoreCase("Doctor") ||
                    inputSecondTag.equalsIgnoreCase("Sneak") || inputSecondTag.equalsIgnoreCase("Lockpick") ||
                    inputSecondTag.equalsIgnoreCase("Steal") || inputSecondTag.equalsIgnoreCase("Traps") ||
                    inputSecondTag.equalsIgnoreCase("Science") || inputSecondTag.equalsIgnoreCase("Repair") ||
                    inputSecondTag.equalsIgnoreCase("Speech") || inputSecondTag.equalsIgnoreCase("Barter") ||
                    inputSecondTag.equalsIgnoreCase("Gambling") || inputSecondTag.equalsIgnoreCase("Outdoors"))
                break;
            else System.out.println("Please choose Small Guns, Big Guns, Energy Weapons, " +
                    "Unarmed, Melee Weapons, Throwing, First Aid, Doctor, " +
                    "Sneak, Lockpick, Steal, Traps, Science, Repair, " +
                    "Speech, Barter, Gambling or Outdoors?");
        }
        if (inputSecondTag.equalsIgnoreCase("Small Guns")) {
            System.out.println("Small Guns Increased by 20!");
            smallGuns = smallGuns + 20;
            System.out.println("Small Guns: " + smallGuns);
        } else if (inputSecondTag.equalsIgnoreCase("Big Guns")) {
            System.out.println("Big Guns Increased by 20!");
            bigGuns = bigGuns + 20;
            System.out.println("Big Guns: " + bigGuns);
        } else if (inputSecondTag.equalsIgnoreCase("Energy Weapons")) {
            System.out.println("Energy Weapons Increased by 20!");
            energyWeapons = energyWeapons + 20;
            System.out.println("Energy Weapons: " + energyWeapons);
        } else if (inputSecondTag.equalsIgnoreCase("Unarmed")) {
            System.out.println("Unarmed Increased by 20!");
            unarmed = unarmed + 20;
            System.out.println("Unarmed: " + unarmed);
        } else if (inputSecondTag.equalsIgnoreCase("Melee Weapons")) {
            System.out.println("Melee Weapons Increased by 20!");
            meleeWeapons = meleeWeapons+ 20;
            System.out.println("Melee Weapons: " + meleeWeapons);
        } else if (inputSecondTag.equalsIgnoreCase("Throwing")) {
            System.out.println("Throwing Increased by 20!");
            throwing = throwing + 20;
            System.out.println("Throwing: " + throwing);
        } else if (inputSecondTag.equalsIgnoreCase("First Aid")) {
            System.out.println("First Aid Increased by 20!");
            firstAid = firstAid+ 20;
            System.out.println("First Aid: " + firstAid);
        } else if (inputSecondTag.equalsIgnoreCase("Doctor")) {
            System.out.println("Doctor Increased by 20!");
            doctor = doctor + 20;
            System.out.println("Doctor: " + doctor);
        } else if (inputSecondTag.equalsIgnoreCase("Sneak")) {
            System.out.println("Sneak Increased by 20!");
            sneak = sneak + 20;
            System.out.println("Sneak: " + sneak);
        } else if (inputSecondTag.equalsIgnoreCase("Lockpick")) {
            System.out.println("Lockpick Increased by 20!");
            lockpick = lockpick + 20;
            System.out.println("Lockpick: " + lockpick);
        } else if (inputSecondTag.equalsIgnoreCase("Steal")) {
            System.out.println("Steal Increased by 20!");
            steal = steal + 20;
            System.out.println("Steal: " + steal);
        } else if (inputSecondTag.equalsIgnoreCase("Traps")) {
            System.out.println("Traps Increased by 20!");
            traps = traps + 20;
            System.out.println("Traps: " + traps);
        } else if (inputSecondTag.equalsIgnoreCase("Science")) {
            System.out.println("Science Increased by 20!");
            science = science + 20;
            System.out.println("Science: " + science);
        } else if (inputSecondTag.equalsIgnoreCase("Repair")) {
            System.out.println("Repair Increased by 20!");
            repair = repair + 20;
            System.out.println("Repair: " + repair);
        } else if (inputSecondTag.equalsIgnoreCase("Speech")) {
            System.out.println("Speech Increased by 20!");
            speech = speech + 20;
            System.out.println("Speech: " + speech);
        } else if (inputSecondTag.equalsIgnoreCase("Barter")) {
            System.out.println("Barter Increased by 20!");
            barter = barter + 20;
            System.out.println("Barter: " + barter);
        } else if (inputSecondTag.equalsIgnoreCase("Gambling")) {
            System.out.println("Gambling Increased by 20!");
            gambling = gambling + 20;
            System.out.println("Gambling: " + gambling);
        } else if (inputSecondTag.equalsIgnoreCase("Outdoors")) {
            System.out.println("Outdoors Increased by 20!");
            outdoors = outdoors + 20;
            System.out.println("Outdoors: " + outdoors);
        }
        System.out.println();
        System.out.println("You have 1 Skill to Tag.");
        System.out.println("What skill would you like to Tag? (+15)");
        System.out.print("Small Guns, Big Guns, Energy Weapons, Unarmed, Melee Weapons, Throwing, ");
        System.out.print("First Aid, Doctor, Sneak, Lockpick, Steal, Traps, Science, Repair, ");
        System.out.println("Speech, Barter, Gambling or Outdoors?");
        Scanner scanThirdTag = new Scanner(System.in);
        String inputThirdTag = null;
        while (scanThirdTag.hasNextLine()) {
            inputThirdTag = scanThirdTag.nextLine();
            if (inputThirdTag.equalsIgnoreCase("Small Guns") || inputThirdTag.equalsIgnoreCase("Big Guns") ||  inputThirdTag.equalsIgnoreCase("Energy Weapons") || inputThirdTag.equalsIgnoreCase("Unarmed") ||  inputThirdTag.equalsIgnoreCase("Melee Weapons") || inputThirdTag.equalsIgnoreCase("Throwing") ||  inputThirdTag.equalsIgnoreCase("First Aid") || inputThirdTag.equalsIgnoreCase("Doctor") ||
                    inputThirdTag.equalsIgnoreCase("Sneak") || inputThirdTag.equalsIgnoreCase("Lockpick") ||
                    inputThirdTag.equalsIgnoreCase("Steal") || inputThirdTag.equalsIgnoreCase("Traps") ||
                    inputThirdTag.equalsIgnoreCase("Science") || inputThirdTag.equalsIgnoreCase("Repair") ||
                    inputThirdTag.equalsIgnoreCase("Speech") || inputThirdTag.equalsIgnoreCase("Barter") ||
                    inputThirdTag.equalsIgnoreCase("Gambling") || inputThirdTag.equalsIgnoreCase("Outdoors"))
                break;
            else System.out.println("Please choose Small Guns, Big Guns, Energy Weapons, " +
                    "Unarmed, Melee Weapons, Throwing, First Aid, Doctor, " +
                    "Sneak, Lockpick, Steal, Traps, Science, Repair, " +
                    "Speech, Barter, Gambling or Outdoors?");
        }
        if (inputThirdTag.equalsIgnoreCase("Small Guns")) {
            System.out.println("Small Guns Increased by 25!");
            smallGuns = smallGuns + 25;
            System.out.println("Small Guns: " + smallGuns);
        } else if (inputThirdTag.equalsIgnoreCase("Big Guns")) {
            System.out.println("Big Guns Increased by 25!");
            bigGuns = bigGuns + 25;
            System.out.println("Big Guns: " + bigGuns);
        } else if (inputThirdTag.equalsIgnoreCase("Energy Weapons")) {
            System.out.println("Energy Weapons Increased by 25!");
            energyWeapons = energyWeapons + 25;
            System.out.println("Energy Weapons: " + energyWeapons);
        } else if (inputThirdTag.equalsIgnoreCase("Unarmed")) {
            System.out.println("Unarmed Increased by 25!");
            unarmed = unarmed + 25;
            System.out.println("Unarmed: " + unarmed);
        } else if (inputThirdTag.equalsIgnoreCase("Melee Weapons")) {
            System.out.println("Melee Weapons Increased by 25!");
            meleeWeapons = meleeWeapons+ 25;
            System.out.println("Melee Weapons: " + meleeWeapons);
        } else if (inputThirdTag.equalsIgnoreCase("Throwing")) {
            System.out.println("Throwing Increased by 25!");
            throwing = throwing + 25;
            System.out.println("Throwing: " + throwing);
        } else if (inputThirdTag.equalsIgnoreCase("First Aid")) {
            System.out.println("First Aid Increased by 25!");
            firstAid = firstAid+ 25;
            System.out.println("First Aid: " + firstAid);
        } else if (inputThirdTag.equalsIgnoreCase("Doctor")) {
            System.out.println("Doctor Increased by 25!");
            doctor = doctor + 25;
            System.out.println("Doctor: " + doctor);
        } else if (inputThirdTag.equalsIgnoreCase("Sneak")) {
            System.out.println("Sneak Increased by 25!");
            sneak = sneak + 25;
            System.out.println("Sneak: " + sneak);
        } else if (inputThirdTag.equalsIgnoreCase("Lockpick")) {
            System.out.println("Lockpick Increased by 25!");
            lockpick = lockpick + 25;
            System.out.println("Lockpick: " + lockpick);
        } else if (inputThirdTag.equalsIgnoreCase("Steal")) {
            System.out.println("Steal Increased by 25!");
            steal = steal + 25;
            System.out.println("Steal: " + steal);
        } else if (inputThirdTag.equalsIgnoreCase("Traps")) {
            System.out.println("Traps Increased by 25!");
            traps = traps + 25;
            System.out.println("Traps: " + traps);
        } else if (inputThirdTag.equalsIgnoreCase("Science")) {
            System.out.println("Science Increased by 25!");
            science = science + 25;
            System.out.println("Science: " + science);
        } else if (inputThirdTag.equalsIgnoreCase("Repair")) {
            System.out.println("Repair Increased by 25!");
            repair = repair + 25;
            System.out.println("Repair: " + repair);
        } else if (inputThirdTag.equalsIgnoreCase("Speech")) {
            System.out.println("Speech Increased by 25!");
            speech = speech + 25;
            System.out.println("Speech: " + speech);
        } else if (inputThirdTag.equalsIgnoreCase("Barter")) {
            System.out.println("Barter Increased by 25!");
            barter = barter + 25;
            System.out.println("Barter: " + barter);
        } else if (inputThirdTag.equalsIgnoreCase("Gambling")) {
            System.out.println("Gambling Increased by 25!");
            gambling = gambling + 25;
            System.out.println("Gambling: " + gambling);
        } else if (inputThirdTag.equalsIgnoreCase("Outdoors")) {
            System.out.println("Outdoors Increased by 25!");
            outdoors = outdoors + 25;
            System.out.println("Outdoors: " + outdoors);
        }
        System.out.println();
        System.out.println(enter);
        pressEnter.nextLine();
        System.out.println("Your new Skills are:");
        System.out.println("Small Guns: " + smallGuns);
        System.out.println("Big Guns: " + bigGuns);
        System.out.println("Energy Weapons: " + energyWeapons);
        System.out.println("Unarmed: " + unarmed);
        System.out.println("Melee Weapons: " + meleeWeapons);
        System.out.println("Throwing: " + throwing);
        System.out.println("First Aid: " + firstAid);
        System.out.println("Doctor: " + doctor);
        System.out.println("Sneak: " + sneak);
        System.out.println("Lockpick: " + lockpick);
        System.out.println("Steal: " + steal);
        System.out.println("Traps: " + traps);
        System.out.println("Science: " + science);
        System.out.println("Repair: " + repair);
        System.out.println("Speech: " + speech);
        System.out.println("Barter: " + barter);
        System.out.println("Gambling: " + gambling);
        System.out.println("Outdoors: " + outdoors);
        System.out.println();
        System.out.println(enter);
        pressEnter.nextLine();

    }
}

3 个答案:

答案 0 :(得分:2)

没有用一长串代码压倒你:

您可以创建“taggedSkills”的Set数据结构。最初,此Set为空。如果用户标记了技能,则该技能会添加到该组中。

然后,如果用户的第二个标记与第一个标记的技能相同,则程序可以检查“设置”并要求用户使用新技能再次尝试,而不是添加重复标记。与第三项技能相同。

答案 1 :(得分:1)

我的版本。

import java.util.*;

public class SOFQuestion {

    private static Skill getSkillFromUser() {
        final Scanner scanFirstTag = new Scanner(System.in);

        while (scanFirstTag.hasNextLine()) {
            final String inputFirstTag = scanFirstTag.nextLine();
            for (Skill skill : Skill.values()) {
                if (skill.getSkillName().equalsIgnoreCase(inputFirstTag)) {
                    if (Skill.alreadyAddedSkills.contains(skill)) {
                        System.out.println("Sorry, but you have are already increased this skill.");
                        break;
                    }
                    return skill;
                }
            }
            System.out.println("Please choose " + Skill.getSkillsList() + "?");
        }
        return null;
    }

    enum Skill {
        SMALL_GUNS("Small Guns"),
        THROWING("Throwing"),
        FIRST_AID("First Aid"),
        DOCTOR("Doctor");

        private final String skillName;
        private int skillPower;
        private static List<Skill> alreadyAddedSkills = new ArrayList<>();

        Skill(String skillName) {
            this.skillName = skillName;
        }

        public String getSkillName() {
            return skillName;
        }

        public int getSkillPower() {
            return skillPower;
        }

        public void increaseSkillPower(int skillPower) {
            this.skillPower += skillPower;
            alreadyAddedSkills.add(this);
            System.out.println("You increase " + getSkillName() + " skill to " + getSkillPower());
        }

        public static String getSkillsList() {
            final StringBuffer sb = new StringBuffer();
            for (Skill s : Skill.values()) {
                if (alreadyAddedSkills.contains(s)) continue;

                sb.append(s.getSkillName()).append(", ");
            }
            if (sb.length() > 2) {
                return sb.toString().substring(0, sb.length() - 2);
            }
            return "EMPTY SKILL LIST";
        }
    }

    public static void main(String... args) {
        int skillLimit = 3;
        int defPower = 10;
        while (skillLimit != 0) {
            final int powerToAdd = skillLimit * 5 + defPower;
            System.out.println("You have " + skillLimit-- + " Skills to Tag.");
            System.out.println("What skill would you like to Tag? " + "(+" + powerToAdd + ")");
            System.out.println(Skill.getSkillsList() + "?");

            final Skill skill = getSkillFromUser();
            if (skill == null) return;

            skill.increaseSkillPower(powerToAdd);
        }
        System.out.println("Your new Skills are:");
        for (Skill skill : Skill.values()) {
            System.out.println(skill.getSkillName() + ": " + skill.getSkillPower());
        }
    }
}

答案 2 :(得分:0)

这并不完美,我并不想输入所有选择,但这可能对你有帮助。

import java.util.ArrayList;
import java.util.Scanner;

public class Meow {

public static void main(String[] args){

        ArrayList<String> choices = new ArrayList<String>();
        choices.add("Barter");
        choices.add("Repair");
        choices.add("Speech");
        choices.add("Potato");
        int[] numbers = new int[choices.size()];

    System.out.println("You have 3 Skills to Tag.");
    System.out.println("What skill would you like to Tag? (+25)");
    System.out.println("Barter, Repair, Speech, Potato?");
    Scanner scanner = new Scanner(System.in);
    String inputFirstTag = "";
    while (scanner.hasNextLine()) {
        inputFirstTag = scanner.nextLine();
        if ((choices.contains(inputFirstTag))){
            if(numbers[choices.indexOf(inputFirstTag)] == 0){
                numbers[choices.indexOf(inputFirstTag)] += 25;
                break;
            }
            else {System.out.println("Please don't choose duplicates");}
        }
        else {System.out.println("Please choose Repair, Speech, Barter.");}
    }

    System.out.println(inputFirstTag + " + 25");
    System.out.println();

    System.out.println("You have 2 Skills to Tag.");
    System.out.println("What skill would you like to Tag? (+25)");
    System.out.println("Barter, Repair, Speech, Potato?");
    String inputSecondTag = "";
    while (scanner.hasNextLine()) {
        inputSecondTag = scanner.nextLine();
        if ((choices.contains(inputSecondTag))){
            if(numbers[choices.indexOf(inputSecondTag)] == 0){
                numbers[choices.indexOf(inputSecondTag)] += 25;
                break;
            }
            else {System.out.println("Please don't choose duplicates");}
        }
        else {System.out.println("Please choose Repair, Speech, Barter.");}
    }
    System.out.println(inputSecondTag + " + 25");
    System.out.println();


    System.out.println();
    System.out.println("You have 1 Skill to Tag.");
    System.out.println("What skill would you like to Tag? (+25)");
    System.out.println("Barter, Repair, Speech, Potato?");
    String inputThirdTag = "";
    while (scanner.hasNextLine()) {
        inputThirdTag = scanner.nextLine();
        if ((choices.contains(inputThirdTag))){
            if(numbers[choices.indexOf(inputThirdTag)] == 0){
                numbers[choices.indexOf(inputThirdTag)] += 25;
                break;
            }
            else {System.out.println("Please don't choose duplicates");}
        }
        else {System.out.println("Please choose Repair, Speech, Barter.");}
    }

    System.out.println(inputThirdTag + " + 25");
    System.out.println();

    for (int i = 0; i < choices.size(); i++){
        System.out.println(choices.get(i) + " : " + numbers[i]);
    }

}

}