2017-06-21 9 views
-3

これを学校の課題に取り組んで、スイッチやケースの仕組みについて混乱させます。私は最終的には動作すると思っていましたが、まだフォーマットエラーが発生していますが、私はなぜそれがわかりません。私が正しくあなたの意図を理解していればスイッチとケースの書式設定は?

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

フォーマットエラーとは何ですか? – shmosel

+0

スレッド "main"の例外java.lang.RuntimeException:Uncompilableソースコード - 誤ったツリータイプ: \t、PartyAffiliation.main。私は確信していません、私はJavaについてはあまりよく分かりません。 – user8191661

+0

'do'ループに対応する' while'条件がないので、コンパイルエラーです。無限にループさせたい場合は、 'do'を' while(true) 'に置き換えてください。 – shmosel

答えて

-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)を入力するまで、ユーザーからの入力を要求し続けたいとします。

+1

素晴らしい、それはまさに私がやろうとしていたことだ、ありがとう – user8191661

+0

あなたは大歓迎です!これがあなたの質問に答えるなら、答えの横にあるチェックマークをクリックしてください。これにより、人々は質問に答えられたことを知らせ、それを書いた人に信用を与えます。スタックオーバーフローへようこそ! –

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."); 
    } 
} 
+0

+1ですが、個人的には 'String party = in.nextLine();' ' –

+0

があります@ScaryWombat私はOPがループの後に変数を望んでいると想定しました。しかし、はい、私は必要最小限に範囲を制限することが最善であることに同意します。 –