2017-03-07 10 views
0

こんにちは、私はクイズアプリを作りたいと思っています。g.間違った位置での一致?プログラムは毎回終了します

import java.util.Scanner; 
public class Brotcrunsher { 
    public static void main(String args[]){ 
    Scanner scan = new Scanner(System.in); 

    System.out.println ("Hallo"); 
    System.out.println ("Welcome to the test"); 
    System.out.println ("If you are the admin of the test and would like to edit it please type the password now"); 
    System.out.println ("Otherwhise please say 'begin' to start"); 
    String c = scan.nextLine(); 
    if (c.equals ("edit")) { 
     System.out.println ("Editor mode coming soon, please edit via code"); 
    } 
    else { 
     if (c.equals ("begin")) { 
     } // end of if 
     System.out.println ("A flag has more then 1 color right?"); 
     String a = scan.nextLine(); 
     if (a.equals("ja")) { 
     System.out.println ("You arent dumb, nice."); 
     System.out.println ("What is 352 + 223"); 
     int b = scan.nextInt(); 
     if (b == 575) { 
      System.out.println ("That is correct nice"); 
      System.out.println ("You passed the basic questions, going to be tricky now"); 
      System.out.println ("What was the date when Donald Trump became president"); 
      String g = scan.nextLine(); 
      System.out.println ("A = 19.01 B = 20.01 C = 21.01 D= 22.01"); 
      if (g.equals ("B")){ 
      System.out.println ("You are right, lets move on."); 
      } // end of if 
      else { 
      System.out.println ("That is wrong. Better luck next time"); 
      } // end of if-else 
     } 
     else { 
      System.out.println("That isn't right, you failed."); 
      System.out.println ("If you'd like to repeat the test, go ahead and do so"); 
     } 
     } 
     else { 
     System.out.println ("You arentn a genie"); 
     } 
    } 
    } 
} 

私の質問は、どこに文字列g = scan.NextLineを配置する必要があるのですか?私はコンソールの出口だけのコンパイルエラーは表示されません。 私はそれをIF文と他のいくつかのポジションに置いてみましたが、ただ止めました。助けてください。

答えて

1

アプリケーションを実行しているときにコンソールを閉じると、おそらくランタイムエラーが発生しています。すでに開いているコマンドシェルからプログラムを実行して、エラーを確認してください。

は、これがコメントする必要がありますが、私はこの問題は、コードの重要な部分に到着したときにスキャナが空ではないということです0 :)

+0

閉じるのではなく、一時停止するように...キーを押すと閉じます。 –

+0

それはあなたの入力を待っている可能性があります – PimJ

+0

あなたの入力が与えられた(キーを押した)とき、プログラムはランタイムエラーのために終了することがあります。コマンドラインからアプリを実行してみてください: http://stackoverflow.com/questions/16137713/how-to-run-a-java-program-from-the-command-line – PimJ

1

十分な担当者を持っていません。あなたは、バッファEXをクリアする必要があります。

public class Brotcrunsher { 
     public static void main(String args[]){ 
     Scanner scan = new Scanner(System.in); 

     System.out.println ("Hallo"); 
     System.out.println ("Welcome to the test"); 
     System.out.println ("If you are the admin of the test and would like to edit it please type the password now"); 
     System.out.println ("Otherwhise please say 'begin' to start"); 
     String c = scan.nextLine(); 
     if (c.equals ("edit")) { 
      System.out.println ("Editor mode coming soon, please edit via code"); 
     } 
     else { 
      if (c.equals ("begin")) { 
      } // end of if 
      System.out.println ("A flag has more then 1 color right?"); 
      String a = scan.nextLine(); 
      if (a.equals("ja")) { 
      System.out.println ("You arent dumb, nice."); 
      System.out.println ("What is 352 + 223"); 
      int b = scan.nextInt(); 
      if (b == 575) { 
       System.out.println ("That is correct nice"); 
       System.out.println ("You passed the basic questions, going to be tricky now"); 
       System.out.println ("What was the date when Donald Trump became president"); 
       System.out.println ("A = 19.01 B = 20.01 C = 21.01 D= 22.01"); 
       scan.nextLine();  // FEED the new-line character 
       String g = scan.nextLine(); 
       if (g.equals ("B")){ 
       System.out.println ("You are right, lets move on."); 
       } // end of if 
       else { 
       System.out.println ("That is wrong. Better luck next time"); 
       } // end of if-else 
      } 
      else { 
       System.out.println("That isn't right, you failed."); 
       System.out.println ("If you'd like to repeat the test, go ahead and do so"); 
      } 
      } 
      else { 
      System.out.println ("You arentn a genie"); 
      } 
     } 
     } 
    } 

詳細を:私はあなたのコードと最後scan.nextLineを()しようとした How can I clear the Scanner buffer in Java?

+0

すべてを修正しました。ありがとう多く:) –

+0

答えを受け入れることは同じことを意味します:) – betontalpfa

0

をスキップしているようです。投稿はthisをご覧ください。 あなたの電話の位置は問題ではありません。

0

私はコードSystem.out.println( "A = 19.01 B = 20.01 C = 21.01 D = 22.01")と思う。 String g = scan.nextLine()の前になければなりません。最初にユーザーにオプションを与えてから、ユーザーが選択する必要があるからです。

関連する問題