gpt4 book ai didi

java - 制作计算器但我收到编译错误

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

我正在自学 Java 并开始制作计算器,但当我尝试运行代码时遇到两个错误。

我正在考虑用字符实现一个 WHILE 循环来创建循环,但它似乎不起作用。真的精疲力竭,我认为这是一个提问的好地方。

import java.lang.Math;
import java.util.Scanner;

public class Calculator
{

public static void main(String[] args)
{
double numberOne = 0;
double numberTwo = 0;
String operatorInput;
Scanner myInput = new Scanner(System.in);

System.out.print("Enter first number: ");
numberOne = myInput.nextDouble();

System.out.print("Enter second number: ");
numberTwo = myInput.nextDouble();

System.out.print("Enter the operation you wish to perform (/, *, -, +): ");
operatorInput = myInput.nextLine();

double result = calculate(numberOne, numberTwo, operatorInput, myInput);

System.out.printf("The result is: %.2f%n", result);

myInput.close();
}

public static double calculate(double numberOne, double numberTwo, String operatorInput, Scanner myInput)
{
while(true)
{
switch (operatorInput)
{
case("/"):
{
myInput.close();
return divideNumbers(numberOne, numberTwo);
}
case("*"):
{
myInput.close();
return multiplyNumbers(numberOne, numberTwo);
}
case("+"):
{
myInput.close();
return addNumbers(numberOne, numberTwo);
}
case("-"):
{
myInput.close();
return subtractNumbers(numberOne, numberTwo);
}
default:
{
System.out.println("Invalid Input");
System.out.print("Enter the operation you wish to perform (/, *, -, +): ");
operatorInput = myInput.nextLine();
}
}
}
}

public static double divideNumbers(double numberOne, double numberTwo)
{
return numberOne / numberTwo;
}
public static double multiplyNumbers(double numberOne, double numberTwo)
{
return numberOne * numberTwo;
}
public static double addNumbers(double numberOne, double numberTwo)
{
return numberOne + numberTwo;
}
public static double subtractNumbers(double numberOne, double numberTwo)
{
return numberOne - numberTwo;
}
}
Calculator.java:27: error: cannot find symbol
operatorInput = input.nextChar();
^
symbol: method nextChar()
location: variable input of type Scanner
Calculator.java:55: error: incompatible types: <null> cannot be converted to double
return null;
^

最佳答案

您可以将 double 基本类型更改为 Double 对象,以便能够返回 null 值。您也可以将operatorInput转换为字符串并将其读取为input.next()以下是我建议的更改示例:

import java.util.Scanner;

public class Calculator
{
public static void main(String[] args)
{
double numberOne = 0;
double numberTwo = 0;
String operatorInput;

Scanner input = new Scanner(System.in);

System.out.print("Enter first number: ");
numberOne = input.nextDouble();

System.out.print("Enter second number: ");
numberTwo = input.nextDouble();

System.out.print("Enter the operation you wish to perform (/, *, -, +): ");
operatorInput = input.next();
System.out.println(calculate(numberOne, numberTwo,operatorInput));

}



public static Double calculate(double numberOne, double numberTwo, String operatorInput)
{
switch (operatorInput)
{
case("/"):
{
return divideNumbers(numberOne, numberTwo);
}
case("*"):
{
return multiplyNumbers(numberOne, numberTwo);
}
case("+"):
{
return addNumbers(numberOne, numberTwo);
}
case("-"):
{
return subtractNumbers(numberOne, numberTwo);
}
default:
{
System.out.println("Invalid Input");
return null;
}
}

}

public static double divideNumbers(double numberOne, double numberTwo)
{
return numberOne / numberTwo;
}
public static double multiplyNumbers(double numberOne, double numberTwo)
{
return numberOne * numberTwo;
}
public static double addNumbers(double numberOne, double numberTwo)
{
return numberOne + numberTwo;
}
public static double subtractNumbers(double numberOne, double numberTwo)
{
return numberOne - numberTwo;
}

}

关于java - 制作计算器但我收到编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56330704/

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