1

可能重复:
如何在 dr java 程序中创建计数器

这是我正在处理的代码...

我想知道在询问每个问题后如何重置交互窗口,以便窗口清晰,以便向用户显示下一个问题,但我也不希望重置分数,以便我可以显示分数在最后。我正在寻找正确的语法和代码来做到这一点。

//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

清除控制台的一种蛮力(并且与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 回答