开关和案例格式?

时间:2017-06-21 02:34:16

标签: java switch-statement case

为学校作业工作,并对交换机和案例的工作方式感到困惑。我以为我终于得到了它的工作,但我仍然遇到格式错误,我不知道为什么。

import java.util.Scanner;

public class PartyAffiliation
{
    public static void main(String[] args)
    {
        do
        {
            String Party = null;
            boolean running = true;
            Scanner in = new Scanner(System.in);
            loopParty: while(running)
            {
                System.out.println("What is your politcal party? (D for Democrat - R for Republican - I for Independent");
                Party = in.nextLine();

                switch (Party)
                {
                    case ("D"):
                        System.out.println("You get a Democratic Donkey!");
                        running=false;
                        break;
                    case ("R"):
                        System.out.println("You get a Republican Elephant!");
                        running=false;
                        break;
                    case ("I"):
                        System.out.println("You get an Independent Man!");
                        running=false;
                        break;
                    default:
                        System.out.println("I guess you aren't any of the three.");
                        break;       
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您不需要像boolean这样的running哨兵。并且您不能拥有原始do块(没有while,您真的不需要)。使用无限循环,标记它,然后您可以在该标签上break。此外,允许混合大小写输入更好(IMO)(所以我会在输入上调用toUpperCase())。像,

String party = null; // <-- follow Java naming conventions. Party looks like a Class.
Scanner in = new Scanner(System.in);
loop: while (true) {
    System.out.println("What is your politcal party? (" + 
             "D for Democrat - R for Republican - I for Independent");
    party = in.nextLine();

    switch (party.toUpperCase()) {
    case "D": // <-- you don't need () around your case values
        System.out.println("You get a Democratic Donkey!");
        break loop;
    case "R":
        System.out.println("You get a Republican Elephant!");
        break loop;
    case "I":
        System.out.println("You get an Independent Man!");
        break loop;
    default:
        System.out.println("I guess you aren't any of the three.");
    }
}

答案 1 :(得分:-1)

如果我理解你的意图,也许你想这样做:

public static void main(String[] args) {

    String Party = null;
    boolean running = true;
    Scanner in = new Scanner(System.in);

    do {
        System.out.println("What is your politcal party? (D for Democrat - R for Republican - I for Independent");
        Party = in.nextLine();

        switch (Party) {
            case ("D"):
                System.out.println("You get a Democratic Donkey!");
                running = false;
                break;
            case ("R"):
                System.out.println("You get a Republican Elephant!");
                running = false;
                break;
            case ("I"):
                System.out.println("You get an Independent Man!");
                running = false;
                break;
            default:
                System.out.println("I guess you aren't any of the three.");
                break;
        }
    } while (running);

}

do应附带匹配的while。您希望继续询问用户输入,直到他们输入有效输入(即D,R或I)。