我对Java相当陌生,以前从未做过编程。在尝试为每个操作符运行代码时,我遇到以下错误。我希望它将用户输入作为字符串接收,但返回一个double作为结果。我知道可能会有几个错误,但我们非常感谢您的帮助。
线程“main”java中出现异常。lang.错误:未解决的编译问题:类型不匹配:无法从字符串转换为双精度运算符-参数类型java未定义。lang.字符串,java。lang.String参数类型java的运算符*未定义。lang.字符串,java。参数类型java的运算符/未定义。lang.字符串,java。lang.String参数类型java的运算符%未定义。lang.字符串,java。lang.String无法对非静态字段数学进行静态引用无法对非静态字段数学进行静态引用命令无法解析为运算符类型
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);
}
}
以下是我第一眼能看到的几个问题。
>
myScanner.toString()不读取用户输入。要读取用户输入,可以使用myScanner.next()。
里面if条件if(command.equals("HELP"))...
,这里你还没有从用户输入中读取命令,看来你想使用if(firstCommand.equals("HELP"))
。
代码中从未使用名为“firstNumber”和“secondNumber”的变量。
****代码如下所示****
在react native中遇到问题“无法安装应用程序。请确保您拥有Android开发环境”
我的合并排序算法无法正常工作。代码在下面,但是我简单总结一下我试过的,我知道的代码没有错的地方。 函数将指向数组的指针和数组的大小作为参数。如果数组的大小小于2,它会立即返回。我确信这是有效的,因为我调试了这部分几次。它返回了8次,这是我期望它做的。接下来,创建一个变量作为索引来拆分数组。我测试了它,我很有信心在所有递归中都是正确的。然后,创建两个数组,第一个数组包含索引…中的元素,第二个数组包含
包计算器; 导入静态com.sun.javafx.animation.TickCalculation.sub;导入java.util.扫描仪; 公共类计算器{ }v包装计算器; 导入静态com.sun.javafx.animation.TickCalculation.sub;导入java.util.扫描仪; 公共类计算器{ 包计算器; 导入静态com.sun.javafx.animation.Ti
这是添加时导致错误的代码: 如果message.content==“id start”:
编辑:我刚刚意识到,即使是一个带有应用程序条的简单屏幕,也会发生这种情况 错误:任务“:app:checkdebugaarmadata”的执行失败 无法解析配置“:app:debugRuntimeClasspath”的所有文件。无法解析com。谷歌。firebase:firebase firestore:22.1.2。所需人员:项目:应用程序 无法解析com。谷歌。firebase:firebas