我用Java編寫了這個代碼來創建一個非常簡單的計算器。試圖在Java中創建計算器並且出現這些錯誤:我如何修復它們?
import java.util.Scanner;
public class Addition {
static void Addition() {
Scanner numberOne = new Scanner(System.in);
float x = numberOne.nextFloat();
System.out.println("First Number: " + numberOne.nextLine());
Scanner numberTwo = new Scanner(System.in);
float y = numberTwo.nextFloat();
System.out.println("Second Number: " + numberTwo.nextLine());
float sum = x + y;
System.out.println(sum);
}
}
public class Subtraction {
static void Subtraction() {
Scanner numberOne = new Scanner(System.in);
float x = numberOne.nextFloat();
System.out.println("First Number: " + numberOne.nextLine());
Scanner numberTwo = new Scanner(System.in);
float y = numberTwo.nextFloat();
System.out.println("Second Number: " + numberTwo.nextLine());
float difference = x - y;
System.out.println(difference);
}
}
public class Multiplication {
static void Multiplication() {
Scanner numberOne = new Scanner(System.in);
float x = numberOne.nextFloat();
System.out.println("First Number: " + numberOne.nextLine());
Scanner numberTwo = new Scanner(System.in);
float y = numberTwo.nextFloat();
System.out.println("Second Number: " + numberTwo.nextLine());
float product = x + y;
System.out.println(product);
}
}
public class Division {
static void Addition() {
Scanner numberOne = new Scanner(System.in);
float x = numberOne.nextFloat();
System.out.println("First Number: " + numberOne.nextLine());
Scanner numberTwo = new Scanner(System.in);
float y = numberTwo.nextFloat();
System.out.println("Second Number: " + numberTwo.nextLine());
float quotient = x + y;
System.out.println(quotient);
}
}
public class Calculate {
public static void main(String[] args) {
System.out.println("Calculator");
System.out.println("Choose an operation:");
System.out.println("Addition");
System.out.println("Subtraction");
System.out.println("Multiplication");
System.out.println("Division");
Scanner input = new Scanner(System.in);
String choice = input.nextLine();
if(choice.equals("Addition") {
Addition();
}
else if(choice.equals("Subtraction") {
Subtraction();
}
else if(choice.equals("Mutliplication") {
Mutliplication();
}
else if(choice.equals("Division"){
Division();
}
else {
System.out.println("That wasn't a valid input. Please try again.");
}
}
}
然而,當我試圖運行它,我得到這個錯誤信息:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token ")",) expected after this token
The method Addition() is undefined for the type Calculate
Syntax error on token ")",) expected after this token
The method Subtraction() is undefined for the type Calculate
Syntax error on token ")",) expected after this token
The method Mutliplication() is undefined for the type Calculate
Syntax error on token ")",) expected after this token
The method Division() is undefined for the type Calculate
at Calculate.main(Calculate.java:14)
我在Java初學者,我不太清楚錯誤消息意味着什麼。有人可以向我解釋它的含義以及我如何解決它?
謝謝大家的幫助。我做了更改,現在程序正常運行。還有一件事我想做。如果用戶提供了無效的輸入,我希望他們能夠再次嘗試,但不必重複整個問題。就像他們給出的東西不是這四種選擇一樣,我希望他們只是爲了能夠再次回答,並重復這些,直到他們回答某些有效的輸入。 –