如何重置dr java中的交互窗口?

发布于 2024-08-07 15:20:42 字数 2875 浏览 2 评论 0原文

可能的重复:
如何在 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");
  }
  }
}

Possible Duplicate:
how to create a counter in a dr java program

this is the code that I am working on...

I would like to know how to reset the interactions window after every question is asked so that the window is clear for the next question to be shown to the user but I do not want the Score to be reset either so that I can display the score at the end. I am looking for the correct syntax and code to do this.

//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");
  }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丿*梦醉红颜 2024-08-14 15:20:42

您需要使用某种循环。因此,您可以首先创建数组来存储问题和答案,如下所示:

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");

You need to use a loop of some sort. So you can start by creating arrays to store the questions and answers like this:

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 = "";

Then you can write a loop like this:

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);

And finally to clear the screen itself you cannot do that directly in Java but you may be able to run the cls command (assuming you run Windows) but that makes your code platform specific.

Runtime.getRuntime().exec("cls");
蓝天白云 2024-08-14 15:20:42

清除控制台的一种强力方法(与 cls 不同,与平台无关)是向控制台打印一大堆新内容。这种方法可能还有一个额外的好处,那就是被认为适合 11 年级的计算机科学项目。

One brute-force (and, unlike cls, platform-independent) method for clearing the console would be to print a whole bunch of newliness to the console. This approach could have the added benefit of being seen as befitting an 11th grade CS project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文