1

重複の可能性:
drjavaプログラムでカウンターを作成する方法

これは私が取り組んでいるコードです...

次の質問がユーザーに表示されるようにウィンドウがクリアになるように、すべての質問が行われた後にインタラクションウィンドウをリセットする方法を知りたいのですが、スコアを表示できるようにスコアもリセットしたくありません。最後に。これを行うための正しい構文とコードを探しています。

//Neel Patel
//Friday October 9th, 2009
/*This is a quiz program that will ask the user 10 questions. the user will answer
 * these questions and will be scored out of 10.*/

class Quiz
{
  public static void main (String args[])
  {
    //Instructions
    System.out.println("instructions");
    System.out.println(" ");
    System.out.println("1. You wll be asked ten questions through out the quiz.");
    System.out.println("2. The first question will appear, you will have to answer that    question for the next question to appear.");
    System.out.println("3. When you answer the last question you will be told your score.");
    System.out.println(" ");

System.out.println("welcome to the basketball quiz.");
int Score=0;

  // question 1
  System.out.println(" ");
  System.out.println("Question 1. ");
  System.out.println("How tall is a basketball hoop? ");
  System.out.println("Type in Answer here:");
  String Question1= In.getString();
  if (Question1.equalsIgnoreCase("10 Feet"))
  {
    Score++;
    System.out.println("Correct!");

  }
  else
  {
    System.out.println("you got this questions wrong");
  }

   // question 2
  System.out.println(" ");
  System.out.println("Question 2. ");
  System.out.println("Who invented basketball? ");
  System.out.println("Type in Answer here:");
  String Question2= In.getString();
  if (Question2.equalsIgnoreCase("James Naismith"))
  {

    Score++;
    System.out.println("Correct!");

  }
  else
  {
    System.out.println("you got this questions wrong");
  }
  // question 3
  System.out.println(" ");
  System.out.println("Question 3. ");
  System.out.println("Who is the only person in the history of the NBA to average a triple double for an entier season?");
  System.out.println("Type in Answer here:");
  String Question3= In.getString();
  if (Question3.equalsIgnoreCase("Oscar Robertson"))
  {
    Score++;
    System.out.println("Correct!");

  }
  else
  {
    System.out.println("you got this questions wrong");
  }
   // question 4
  System.out.println(" ");
  System.out.println("Question 4. ");
  System.out.println("how many players was the first basketball game played with?");
  System.out.println("Type in Answer here:");
  String Question4= In.getString();
  if (Question4.equalsIgnoreCase("9 on 9||18"))
  {
    Score++;
    System.out.println("Correct!");

  }
  else
  {
    System.out.println("you got this questions wrong");
  }
  }
}
4

2 に答える 2

0

コンソールをクリアするための1 つのブルート フォース (および、clsプラットフォームに依存しない) 方法は、コンソールに大量の新しさを出力することです。このアプローチには、11 年生の CS プロジェクトに適していると見なされるという追加の利点があります。

于 2009-10-12T01:42:23.927 に答える
0

ある種のループを使用する必要があります。したがって、次のように質問と回答を格納する配列を作成することから始めることができます。

String[] questions = {" \nQuestion 1. \nHow tall is a basketball hoop? \nType in Answer here:",
                  " \nQuestion 2. \nWho invented basketball? \nType in Answer here: "};
String[] answers = {"10 Feet", "James Naismith"};
int score = 0;
String ans = "";

次に、次のようなループを記述できます。

for(int i = 0;i < questions.length; i++){
   System.out.println(questions[i]);
   ans= In.getString();
   if (ans.equalsIgnoreCase(answers[i]))
   {

    System.out.println("Correct!");
    score++;
    }
    else
    {
    System.out.println("you got this questions wrong");
    }
}
System.out.println(score);

最後に、画面自体をクリアするには、Javaで直接行うことはできませんが、clsコマンドを実行できる場合があります(Windowsを実行していると仮定)が、コードプラットフォーム固有になります。

Runtime.getRuntime().exec("cls");
于 2009-10-12T01:05:41.227 に答える