【问题标题】:Switches and cases formatting?开关和案例格式?
【发布时间】:2017-06-21 02:34:15
【问题描述】:

为学校作业做这件事,但对开关和案例的工作方式感到困惑。我以为我终于让它工作了,但我仍然收到格式错误,我不知道为什么。

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;       
                }
            }
        }
    }
}

【问题讨论】:

  • 什么是格式错误?
  • 线程“main”java.lang.RuntimeException 中的异常:无法编译的源代码 - 错误的树类型: 在 PartyAffiliation.main。我不确定,我对 Java 不太了解。
  • 这是一个编译错误,因为您的 do 循环没有相应的 while 条件。如果您希望它无限循环,请将do 替换为while(true)
  • 可能根本不想要do
  • 谢谢,看起来确实解决了问题。它确实会一直循环,这是我不想要的,但我会尝试解决这个问题。

标签: java switch-statement case


【解决方案1】:

您不需要像running 这样的boolean 哨兵。而且你不能有一个原始的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 但个人会拥有String party = in.nextLine(); `
  • @ScaryWombat 我假设 OP 想要循环后的变量。但是,是的,我同意最好将范围限制在必要的最小范围内。
【解决方案2】:

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

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)。

【讨论】:

  • 太棒了,这正是我想要做的,谢谢
  • 不客气!如果这回答了您的问题,请单击答案旁边的复选标记。这让人们知道问题已得到解答,并归功于编写它的人。欢迎使用 Stack Overflow!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多