当前位置: 首页 > 知识库问答 >
问题:

当我在不使用系统的情况下键入“退出”时,我如何使用一段时间来连续请求用户输入并退出程序。退出()?

姜楷
2023-03-14

我目前正在学校参加AP计算机科学课程,我的一个项目遇到了一点麻烦!该项目需要我创建一个计算器,可以计算一个表达式,然后求解它。我已经记下了大部分内容,但我遇到了一点小麻烦,因为我的老师让我使用while循环来不断地请求输入并显示答案,而我对此一窍不通。要结束程序,用户必须输入“退出”,而我不能使用系统。exit()或任何类似的作弊行为,程序必须耗尽代码。有人有什么建议吗?

import java.util.*;

public class Calculator {
   public static void main(String[] args) {
      System.out.println("Welcome to the AP Computer Science calculator!!");
      System.out.println();
      System.out.println("Please use the following format in your expressions: (double)(space)(+,-,*,/...)(space)(double)");
      System.out.println("or: (symbol)(space)(double)");
      System.out.println();
      next();
   }

   public static void next() {
      Scanner kb = new Scanner(System.in);
      System.out.print("Enter an expression, or quit to exit: ");
      String expression = kb.nextLine();
      next3(expression);
   }

   public static void next3(String expression) {   
      while (!expression.equals("quit")) {
         next2(expression);
         next();
      }  
   }

   public static void next2(String expression) {
      if (OperatorFor2OperandExpressions(expression).equals("+")) {
         System.out.println(FirstOperandFor2OperandExpressions(expression) + " " + OperatorFor2OperandExpressions(expression) + " " + SecondOperandFor2OperandExpressions(expression) + " = " + (FirstOperandFor2OperandExpressions(expression) + SecondOperandFor2OperandExpressions(expression)));          
      }
      else if (OperatorFor2OperandExpressions(expression).equals("*")) {
         System.out.println(FirstOperandFor2OperandExpressions(expression) + " " + OperatorFor2OperandExpressions(expression) + " "  + SecondOperandFor2OperandExpressions(expression) + " = " + (FirstOperandFor2OperandExpressions(expression) * SecondOperandFor2OperandExpressions(expression)));
      }
      else if (OperatorFor2OperandExpressions(expression).equals("-")) {
         System.out.println(FirstOperandFor2OperandExpressions(expression) + " " + OperatorFor2OperandExpressions(expression) + " " + SecondOperandFor2OperandExpressions(expression) + " = " + (FirstOperandFor2OperandExpressions(expression) - SecondOperandFor2OperandExpressions(expression)));       
      }
      else if (OperatorFor2OperandExpressions(expression).equals("/")) {
         System.out.println(FirstOperandFor2OperandExpressions(expression) + " " + OperatorFor2OperandExpressions(expression) + " "  + SecondOperandFor2OperandExpressions(expression) + " = " + (FirstOperandFor2OperandExpressions(expression) / SecondOperandFor2OperandExpressions(expression)));
      }
      else if (OperatorFor2OperandExpressions(expression).equals("^")) {
         System.out.println(FirstOperandFor2OperandExpressions(expression) + " " + OperatorFor2OperandExpressions(expression) + " " + SecondOperandFor2OperandExpressions(expression) + " = " + Math.pow(FirstOperandFor2OperandExpressions(expression),SecondOperandFor2OperandExpressions(expression)));
      }
      else if (OperatorFor1OperandExpressions(expression).equals("|")) {
         System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + Math.abs(OperandFor1OperatorExpressions(expression)));
      }
      else if (OperatorFor1OperandExpressions(expression).equals("v")) {
         System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + Math.sqrt(OperandFor1OperatorExpressions(expression)));
      }
      else if (OperatorFor1OperandExpressions(expression).equals("~")) {
         double x = 0.0;
         System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + (Math.round(OperandFor1OperatorExpressions(expression))+ x));
      }
      else if (OperatorFor1OperandExpressions(expression).equals("s")) {
         System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + Math.sin(OperandFor1OperatorExpressions(expression)));
      }
      else if (OperatorFor1OperandExpressions(expression).equals("c")) {
         System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + Math.cos(OperandFor1OperatorExpressions(expression)));
      }
      else if (OperatorFor1OperandExpressions(expression).equals("t")) {
         System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + Math.tan(OperandFor1OperatorExpressions(expression))); 
      } 
   }

      public static double FirstOperandFor2OperandExpressions(String expression) {
         String[] tokens = expression.split(" ");
         String OperandOrOperator = tokens[0];
         double y = Double.parseDouble(OperandOrOperator);
         return y;         
   }
      public static double SecondOperandFor2OperandExpressions(String expression) {
         String[] tokens = expression.split(" ");
         String OperandOrOperator = tokens[2];
         double y = Double.parseDouble(OperandOrOperator);
         return y;
   }
       public static String OperatorFor2OperandExpressions(String expression) {
         String[] tokens = expression.split(" ");
         String OperandOrOperator = tokens[1];
         return OperandOrOperator;
   }
      public static String OperatorFor1OperandExpressions(String expression) {
         String[] tokens = expression.split(" ");
         String OperandOrOperator = tokens[0];
         return OperandOrOperator; 
   }
      public static double OperandFor1OperatorExpressions(String expression) {
         String[] tokens = expression.split(" ");
         String OperandOrOperator = tokens[1];
         double y = Double.parseDouble(OperandOrOperator);
         return y;  
   }
      public static boolean QuitFunction(String expression) {
         if (expression.equalsIgnoreCase("quit")) {
            System.out.println("Goodbye!");
            return false;

         }
         else {
            return true;
         }
      }           
}

共有1个答案

华烈
2023-03-14

看看这个代码。我想这可能会帮你找到正确的方向。它与您已经编写的类似,只是它消除了在while循环中调用方法的需要。

Scanner input = new Scanner(System.in);

while (!input.hasNext("quit")) {

    String expression = input.nextLine(); // gets the next line from the Scanner
    next2(expression); // process the input

}
// once the value "quit" has been entered, the while loop terminates
System.out.println("Goodbye");

以这种方式编写它会彻底清理代码,并防止每次处理输入时都出现Scanner kb=new Scanner(System.in);的新声明。

 类似资料:
  • 问题内容: 我“d成的运行Django的服务器,我不能简单地和出来。 是否有其他摆脱困境的方法? 目前,我手动关闭本地PC上的选项卡,然后再重新打开,但这变得很烦人。 问题答案: 或 。请参阅屏幕手册#分离。

  • 我正在尝试编写一个程序,允许在无限循环中生成随机数。如果用户按下'c',它将脱离循环。但是循环只在我按下c时开始,它应该在之前开始,当我按下'c'时它应该停止。 另外,我想知道如何才能只使用while循环而不是do-while循环来完成这个程序。当我使用while时,我从开始,然后使用来自input的if语句中断循环,但这也不起作用。内部仍然有两个for循环。 多谢

  • 问题内容: 这是我的代码。我不确定如何使用返回值退出程序。有任何想法吗?这是我完成任务的最后一步。重要区域标有////////我听到了返回的消息,但是当我将main中的void更改为int时,程序会说main必须为void。 问题答案: 只要您不需要返回自定义退出代码(由所返回的0 除外)并且不启动新线程,就可以通过执行以下操作来终止程序 用你的方法。请注意,没有返回任何值,因此您无需将方法从更改

  • 问题内容: 我正在使用ProcessBuilder启动子进程,并且如果父进程确实需要退出子进程。在正常情况下,我的代码可以正确阻止孩子。但是,如果我导致操作系统杀死父进程,则子进程将继续运行。 有什么方法可以将子进程“绑定”到父进程,以便在父进程被杀死时退出? 问题答案: 子进程与其父进程之间没有联系。他们可能彼此知道进程ID,但是它们之间没有硬连接。您在谈论孤立过程。这是操作系统级别的问题。意味

  • 你好,所以我正在使一个java工作,出于某种原因,当我到达if else时,当我在对话框中单击no时,它没有退出,而是继续询问学生名`public static void main(string[]args){ }`

  • 我试图建立一个视频播放Android应用程序使用Kotlin。我遵循了以下教程https://www.raywenderlich.com/14273655-mediaplayer-simplifed-video-playback-on-Android 我的视频可以播放,但当我退出应用程序并重新进入它,或关闭屏幕并再次打开时,视频图像会变成黑色,尽管音频仍在播放。 我尝试使用onRestart()、