【问题标题】:Asking multiplication questions and computing the total of answers correct in Java问乘法问题并计算 Java 中正确答案的总数
【发布时间】:2017-08-27 02:34:47
【问题描述】:

代码应该输出华氏温度和摄氏温度的表格

public static void main(String[] args) {
    System.out.println("Fahrenheit\tCelsius");
    System.out.println("=======================");
     for(int temp = -45; temp <= 120; temp += 5) //for(int i = 0; i <= 100; i+= 10)
        {
            System.out.printf("%5d       |",          temp);
            double sum = (temp + (9.0/5.0)) * 32;   
            System.out.printf("%5d", (int)sum );
            System.out.printl

n();

【问题讨论】:

  • 一个主要问题:不要在 while 循环中不断地重新创建 Scanner。您将 Scanner 基于 System.in,并且您不希望它在程序完全完成并且不再需要此输入之前结束。因此,只在 before 您的 while 循环中设置一次 Scanner。
  • 我有似曾相识的漏洞吗?前几天我不是几乎同样的问题吗?是不是碰巧被关闭和删除了,现在你再试一次?
  • 感谢您的回答。我确实有这种倾向。我看看能不能修改。 @气垫船。
  • 我不知道你在说什么,我只是想学习/理解。如果你不想帮忙,那很好。 @鬼猫

标签: java if-statement while-loop do-while


【解决方案1】:

您应该在一个包含 while 循环中完成所有这些操作。

while循环伪代码是这样的:

string answer = "";
While (answer != quit)
{
answer = (ask question to user)
if (answer != quit)
   compute/display response
}

【讨论】:

    【解决方案2】:

    错误是您为 'if' 语句打开了两次 '{' -

    if (question.equals("no")||question.equals("n")&& (number1 * number2 == answer)){ 
    {
    

    忘记在最后一行关闭一个括号。

    以下代码解决了由于括号引起的错误:

    import java.util.Scanner;
    
    public class Program3_3 {
    
    private static String question;
    private static Scanner input;
    private static Scanner scanner;
    private static int totalScore;
    private static int answer1;
    private static int userScore;
    
    public static void main(String[] args) {
    do {
        int number1 = (int)(Math.random() * 10);
        int number2 = (int)(Math.random() * 10);    
        totalScore = 0;
        userScore = 0;
        input = new Scanner(System.in);
        System.out.print("What is " + number1 + " * " + number2 + "? ");
        int answer = input.nextInt();
        while (number1 * number2 != answer) {
            int answer1 = number1 * number2;
            System.out.println("Incorrect. The answer is " + answer1+".");
            System.out.print("Want more questions yes or no? ");
            scanner = new Scanner (System.in);
            question = scanner.next(); 
            break;
         } 
    
        if (number1 * number2 == answer){ 
            System.out.println("Correct. Nice work!");
            System.out.print("Want more questions yes or no? ");
            scanner = new Scanner (System.in);
            question = scanner.next(); 
            }
        if (question.equals("no")||question.equals("n")&& (number1 * number2 == answer))
        {
            userScore++;
            totalScore++;
              System.out.println("You have this many questions correct:" + totalScore);
           }
    } 
       while(question.equals("yes") || question.equals("y"));
        }
     }
    

    【讨论】:

      【解决方案3】:

      您的牙套似乎没有正确打开和关闭。 userScore 和 totalScore 也在每次迭代中被重置。所以你不会有正确的分数。将其移出循环。

      import java.util.Scanner;
      
      public class Multiplication {
      
          private static String question;
          private static Scanner input;
          private static Scanner scanner;
          private static int totalScore;
          private static int answer1;
          private static int userScore;
      
          public static void main(String[] args) {
              int number1;
              int number2;
              totalScore = 0;
              userScore = 0;
              input = new Scanner(System.in);
              do {
                  number1 = (int)(Math.random() * 10);
                  number2 = (int)(Math.random() * 10);    
      
                  System.out.print("What is " + number1 + " * " + number2 + "? ");
                  int answer = input.nextInt();
                  while (number1 * number2 != answer) 
                  {
                      int answer1 = number1 * number2;
                      System.out.println("Incorrect. The answer is " + answer1+".");
                      System.out.print("Want more questions yes or no? ");
                      scanner = new Scanner (System.in);
                      question = scanner.next();
                      totalScore++;
                      break;
                  } 
      
                  if (number1 * number2 == answer)
                  { 
                      System.out.println("Correct. Nice work!");
                      System.out.print("Want more questions yes or no? ");
                      scanner = new Scanner (System.in);
                      question = scanner.next(); 
                      userScore++;
                      totalScore++;
                  }
      
              }while(question.equals("yes") || question.equals("y"));
              System.out.println("You have this many questions correct:" + userScore +" out of " + totalScore);
          }
      }
      

      更好的实现

      这里的代码看起来更具可读性,并且删除了一些不必要的代码行。

      import java.util.Scanner;
      
      public class Multiplication {
      
          private static final Scanner scanner = new Scanner (System.in);
          private static int totalScore = 0;
          private static int userScore = 0;
          private static int number1;
          private static int number2;
          private static int answer;
      
          public static boolean continueExe(){
              String question;
              System.out.print("Want more questions yes or no? ");
              question = scanner.next();
              return (question.equals("yes") || question.equals("y"));        
          }
      
          public static void printOutput(){
              System.out.println("You have this many questions correct:" + userScore +" out of " + totalScore);
          }
      
          public static void askQuestion(){
              number1 = (int)(Math.random() * 10);
              number2 = (int)(Math.random() * 10);
              System.out.print("What is " + number1 + " * " + number2 + "? ");
          }
      
          public static void readAnswer(){
              answer = scanner.nextInt();
          }
      
          public static void isAnswerCorrect(){
              if (number1 * number2 != answer){
                  System.out.println("Incorrect. The answer is " + number1 * number2 +".");
              }else{
                  userScore++;
                  System.out.println("Correct. Nice work!");
              }
              totalScore++;
          }
      
          public static void main(String[] args) {
              do{
                  askQuestion();
                  readAnswer();
                  isAnswerCorrect();
              }while(continueExe());
              printOutput();
          }
      }
      

      【讨论】:

      • 非常感谢。第二个代码看起来很高级。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多