java - 尝试用 Java 创建计算器并出现这些错误 : How Do I Fix Them?

标签 java exception

我用 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 初学者,我不太清楚错误消息的含义。有人可以向我解释这意味着什么以及如何解决它吗?

最佳答案

您的代码中存在一些问题。

首先,您不需要为每个方法创建一个单独的类。只需将所有方法放在同一个类中即可。这样,您在调用每个方法时就不需要指定类名。

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

第三,你的乘法和除法实际上似乎是在做加法。使用 * 来乘两个数字,使用 / 来除它们。

第四,丢失对 nextLine() 的一些调用,只打印您已经检索到的值。因此,例如,System.out.println("第一个数字:"+ numberOne.nextLine()); 应该是 System.out.println("第一个数字:"+ x) ; 并且在代码中多次出现类似的情况。

关于java - 尝试用 Java 创建计算器并出现这些错误 : How Do I Fix Them?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43827455/

相关文章:

java - JXL 尝试在设置背景颜色时修改引用格式

java - 在 Java 中为自定义检查异常编写 JUnit 测试用例?

java - Android - Firebase - 无法将 java.util.HashMap 类型的值转换为字符串

java - android :windowLightStatusBar and Navigation Drawer 之间的问题

java - 在字段中存储各种类型的模式

javascript - Angularjs 如何显示列表框中所选行的隐藏数据?

java - 即使在 catch block 中捕获异常对象后,我们是否可以将异常对象扔给调用方法?

java - 如果文件不存在,如何使输出流创建文件?

java - Firebase : Can't convert object of type java. lang.String 键入 com.example.positivethinkers.Members

java - java - 如何将xml Element及其子节点转换为Java中的String?