gpt4 book ai didi

java - 我正在使用 Java 构建计算器,但在执行运算符时遇到问题

转载 作者:行者123 更新时间:2023-12-02 09:06:41 25 4
gpt4 key购买 nike

我对 Java 还很陌生,以前从未编程过。当尝试为每个运算符运行代码时,我收到以下错误。我希望它以字符串形式接收用户输入,但返回 double 值作为结果。我知道可能会有几个错误,但我们将不胜感激。

线程“main”java.lang.Error中出现异常: Unresolved 编译问题: 类型不匹配:无法从 String 转换为 double 对于参数类型 java.lang.String、java.lang.String ,运算符 - 未定义 对于参数类型 java.lang.String、java.lang.String ,运算符 * 未定义 对于参数类型 java.lang.String、java.lang.String ,运算符/未定义 对于参数类型 java.lang.String、java.lang.String 未定义运算符 % 无法对非静态字段 Math 进行静态引用 无法对非静态字段 Math 进行静态引用 命令无法解析为类型 对于参数类型 Class、int ,运算符 >= 未定义 标记“.”上的语法错误,此标记后应有类 对于参数类型 String, int ,运算符 <= 未定义

at Calculator.main(Calculator.java:72)
import java.util.Scanner;
import java.lang.Math;

/*@public class for calculator*/
public class Calculator {
String ADD = "+";
String SUB = "-";
String DIV = "/";
String MUL = "*";
String MOD = "%";
String SQRT = "sqrt";
String POW = "^";
String Math.PI = "pi";

public static void main(String[] args) {


/*@Scanner to read user input */
Scanner myScanner = new Scanner(System.in);
boolean continueLoop = true;

/*@try statement to begin program */
try {



String x,y,command;
double result = 0;
double myDouble = result;

/*@print line welcoming user to calculator */
System.out.print("Welcome to my Calculator! Lets Begin!" + "\n");
/*@print line asking user to either type HELP or START to begin */
System.out.print("Type HELP or START to begin: ");
/*@string to read user input for HELP or START command */
String firstCommand = myScanner.nextLine();
if (firstCommand.equals("HELP")) {
System.out.println("Welcome to my calculator. Make sure to enter only numbers into the first two fields. \nDo NOT divide by 0. \nBe sure to enter the number matching your desired operation.");
System.out.print("Type HELP or START to begin: ");
command = myScanner.nextLine();
}




Double firstNumber = Double.parseDouble(myScanner.nextLine());
/*@print statement asking user for first number input */
System.out.println("Please Enter your First number: ");
/*@variable x to be read by scanner */

x = myScanner.nextLine();

Double secondNumber = Double.parseDouble(myScanner.nextLine());
/*@print statement asking user for second number input *
System.out.println("Please Enter your Second Number: ");

/*@variable y to be read by scanner */
y = myScanner.nextLine();

/*@param converts string into double*/
Double converted = Double.parseDouble(myScanner.nextLine());
/*Print statements with instructions on what commands to put in and where to put in.*/
System.out.print("\n1: Add. \n2: Sub. \n3: Mult. \n4: Divide. \n5: Modulo. \n6: Pi \n7: Pow.");
System.out.print("\nEnter your operator: ");

command = myScanner.nextLine();

switch(command) {
/*@returns addition of firstNumber and secondNumber */
case "1":
result = (x+y); break;
/*@returns subtraction of firstNumber and secondNumber */
case "2":
result = (x-y); break;
/*@returns multiplication of firstNumber and secondNumber */
case "3":
result = (x*y); break;
/*@returns division of firstNumber and secondNumber */
case "4":
result = (x/y); break;
/*@returns modulo of firstNumber and secondNumber */
case "5":
result = (x%y); break;
/*@returns Math.PI */
case "6":
result = (Math.PI);
/*@returns power of firstNumber to the second number secondNumber*/
case "7":
result = (Math.pow(x, y));
}
if(command. >= 1 && command <= 5)
System.out.println("Your answer is: " + result);

/*@if statements for all mathematical operators */
// cannot get sqrt, pow, pie to work


/*@throws exception if anything other than a number is typed in by user */
/*catch exception if anything other than number is keyed in*/
} catch (InputMismatchException inputMismatchException) {
/*@error statement printed*/
System.err.printf("%nException: %s%n", inputMismatchException);
/*@statement for scanner to read the next line*/
myScanner.nextLine(); // discard input, so user can try again
/*@print statement letting user know to try again*/
System.out.printf("You must enter integers. Please try again %n%n");

}
/*@throws exception if zero is keyed in as denominator
/*exception if zero is keyed in as the denominator in division since you cannot divide by zero*/
catch (ArithmeticException arithmeticException) {
/*error statement printed*/
System.err.printf("%nException: %s%n", arithmeticException);
/*print statement letting user that 0 is not valid and to please try again
not able to get the loop to go back and restart*/
System.out.printf("Zero is an invalid denominator.Please try again. %n %n");
}
/*@param finally statement closes scanner*/
finally {
myScanner.close();
} while(continueLoop);

}



}

最佳答案

以下是我第一眼看到的一些问题。

  1. 使用 try 时缺少 catch 或 finally block 。
  2. 比较字符串与整数(比较之前需要进行一些类型转换)
  3. myScanner.toString() 不读取用户输入。要读取用户输入,您可以使用 myScanner.next()。

  4. 里面的if条件if (command.equals("HELP"))...,这里你还没有从用户输入中读取命令,看来你想使用if (firstCommand.equals("HELP")).

5.代码中从未使用过名为“firstNumber”和“secondNumber”的变量。

  • 在提示用户提供输入之前使用 myScanner.nextLine()
  • case语句中使用的变量是String类型。但是,应将其视为整数。正确的方法应该是这样case "1"://命令的数据类型是字符串,所以应该用引号 ""括起来。
  • 关于java - 我正在使用 Java 构建计算器,但在执行运算符时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59763270/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com