2017-05-07 18 views
2

我用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初學者,我不太清楚錯誤消息意味着什麼。有人可以向我解釋它的含義以及我如何解決它?

+0

謝謝大家的幫助。我做了更改,現在程序正常運行。還有一件事我想做。如果用戶提供了無效的輸入,我希望他們能夠再次嘗試,但不必重複整個問題。就像他們給出的東西不是這四種選擇一樣,我希望他們只是爲了能夠再次回答,並重復這些,直到他們回答某些有效的輸入。 –

回答

1

你已經在你的代碼有幾個問題。

首先,你不需要每個方法都有一個單獨的類。把所有的方法放在同一個班上。這樣,當你調用每個方法時,你不需要指定一個類名。

其次,您在if語句中缺少一些)字符。確保每個(字符都有匹配的)。例如,if (choice.equals("Addition")) {

第三,你的乘法和除法方法實際上似乎在做加法。使用*將兩個數字相乘,並使用/將它們分開。

第四,將其中一些電話撥打nextLine(),然後輸出您已檢索的值。所以,例如,System.out.println("First Number: " + numberOne.nextLine());應該是System.out.println("First Number: " + x);,並且在您的代碼中多次使用。

1

您已在名爲Addition的課程中定義了您的Addition方法。

  1. import static Addition.Addtion();

  2. 變化Addition()Addition.Addition()

然後您需要爲其他方法執行相同的操作。您還在choise測試中錯過了)

if(choice.equals("Addition")) { // <-- count the open and close parens. 
0

在您的Calculate類體中,您沒有正確地關閉if語句標題。

if(choice.equals("Addition") { 

應該

if(choice.equals("Addition")) { // notice the second closing parenthesee 

你必須把關閉parenthesee爲每一個開放你。

0

您有一個語法錯誤,您沒有爲if語句添加必要的右括號。

if(choice.equals("Addition") <---- missing parenthesis { 
    Addition(); 
} 

解決方案

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."); 
} 
0

您正在將像加法和減法這樣的方法放在自己的類中,因此Main不知道如何調用它們。

我會說每個數學方法之前的公共類。同時改變你的

System.out.println("Second Number: " + numberTwo.nextLine()); 

喜歡的東西:

System.out.println("Second Number: " + y); 

如果你想讓它實際上吐出相應的數字。

相關問題