Я довольно новичок в Java, никогда раньше не занимался программированием. Я получаю следующие ошибки при попытке запустить код для каждого из операторов. Я хочу, чтобы он принимал пользовательский ввод в виде строки, но в качестве результата возвращал double. Я знаю, что, вероятно, будет несколько ошибок, но любая помощь приветствуется.
Исключение в потоке "main" java .lang.Error: Неразрешенные проблемы компиляции: Несоответствие типов: невозможно преобразовать из 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 Невозможно сделать ссылку * stati c на поле non-stati c Math Невозможно сделать ссылку * stati c на поле non-stati c. Математическая команда не может быть преобразована в тип. operator> = не определен для типа (ов) аргумента. Класс, int Синтаксическая ошибка в токене ".", класс, ожидаемый после этого токена. Оператор <= не определен для типа (ов) аргумента String, int </p>
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);
}
}