2017-09-28 7 views
-1

まず、私はこのウェブサイトの初心者ですので、私のルーキーの間違いを許してください。私の問題の第2は、私はJavaコーディング(明らかに)。しかし、私はそれを実行するたびに、正しい答えを入力しても、間違った答えをタイプしたように反応します。私の間違いを指摘していただけますか?私のifとelseコーディングがうまくいきません

import java.util.*; 

public class Quiz { 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 


    @SuppressWarnings("resource") 
    Scanner in = new Scanner(System.in); 
    System.out.println("Hi! Welcome to the Quiz! Please insert your username."); 
    String userName = in.nextLine(); 
    System.out.println("Welcome " + userName + "! Let's start! Your first question is what is the capital of Norway?"); 
    String answer = in.nextLine(); 
    if (answer == ("Oslo")) 
    System.out.println("Good Job! Your next question is how many states are there in U.S.A.?"); 

else   
{ 
    System.out.println("You've failed!"); 
} 
    { 

     @SuppressWarnings("unused") 
    String answer1 = in.nextLine(); 
    if (answer1 == ("50")) 
    System.out.println("Well done! Your next question is where did the first football world cup happen?"); 
    else   
    { 
     System.out.println("You've failed!"); 
    } 
    String answer2 = in.nextLine(); 
    if (answer2 == ("Uruguay")) 
    System.out.println("Well done! Your next question is"); 
    else   
    { 
     System.out.println("You've failed!"); 
    } 
    } 
    } 
    } 
+0

また、コーディングでループを作成する方法を学習したいと思います。たとえば、間違った答えを与えた後、質問1から自動的に再起動します。ありがとうございました –

+0

私はEclipseはあなたの質問とは関係ないと思います。それはあなたが使っているIDEだと思われます。 –

+1

Stack Overflowへようこそ[ツアー]、[ヘルプ]、[ask]を読んでこのサイトの仕組みを知ってください。また、あなたのコードを正しくインデントする方法を読んで、何が起こっているのかを理解することは不可能に近づいています。リンクされた/重複した質問を読んで、何が間違っているかを知る。再起動するには、 'do-while'を読んでください。 – Frakcool

答えて

0

文字列の比較に関係します。

あなたの状況は、あなたが変更されたときに解決されなければならないあなたの場合の条件に:

if (answer.equals("Oslo")) { // Do something } 

あなたが比較してみてください何の内容を比較()equalsを使って。 ==を使用すると、実際に比較しようとしているものの参照が比較されます。したがって、オブジェクトは、指定された文字列 "Oslo"に格納(返答)されます。これは等しくないため、falseを返します。ネストされた場合-else文を使用することができます迅速な解決策を得るために、コメント

であなたの質問に加えて

質問ごとに新しいif文を使用することもできます。これにより、コードの書き換えがもう少し必要になります。例を見てください:

しかし、あなたが見ることができるように、コードがかなり繰り返されるように、関数を使うことは次の良いステップになるでしょう。次の例を参照してください。

public class AppWithFunction { 

    public static void main(String[] args) { 

     Scanner in = new Scanner(System.in); 
     System.out.println("Hi! Welcome to the Quiz! Please insert your username."); 
     String userName = in.nextLine(); 
     System.out.println("Welcome " + userName + "! Let's start!"); 

     handleQuestion(in, "Your first question is what is the capital of Norway?", "Oslo"); 
     handleQuestion(in, "Your next question is how many states are there in U.S.A.?", "50"); 
} 

private static void handleQuestion(Scanner in, String questions, String answer) { 
    System.out.println(questions); 
     if (!in.nextLine().equals(answer)) { 
      System.out.println("You've failed!"); 
     } 
    } 
} 

それは一瞬のためにあなたの範囲外のビットかもしれませんが、あなたはマップ(質問=キー、答え=値)でこれらを格納し、質問の束を持っているならば、それらを反復し、それに応じてそれを処理することは、良い次のステップになるでしょう(後で素晴らしい自己割り当ての新しい練習)。

+0

if(answer .equals( "Oslo")){// System.out.println( "Good Job!次の質問はアメリカにはいくつの州があるの?"); \t} 他\t \t \t \t \t { \t \tのSystem.out.println( "あなたが失敗しました!"); \t}私はまだそれを正しく得ることができませんでした。どうしたんだ?私のための実例を書いてください。事前に感謝@クリティエ! –

+0

答えのセクションで詳細を教えてください:)。 – Crittje

関連する問題