Java 将我的大部分代码放在另一个类中如何让它在Eclipse中运行?

Java 将我的大部分代码放在另一个类中如何让它在Eclipse中运行?,java,eclipse,class,Java,Eclipse,Class,我在一个类文件中制作了一个计算器,它工作得很好。 现在我决定只让我的inputstring和scanner在主类中,其余的代码进入另一个类。如何让它工作?请注意,我是一个初学者。所以main类还需要运行/执行calculator类 我在计算器课上收到的错误有: inputString无法解析为变量 重复字段计算器.i(我的for循环) 还有很多关于这些符号的语法错误(,) 主类 计算器类 package com.haynespro.calculator; 导入java.util.ArrayL

我在一个类文件中制作了一个计算器,它工作得很好。 现在我决定只让我的inputstring和scanner在主类中,其余的代码进入另一个类。如何让它工作?请注意,我是一个初学者。所以main类还需要运行/执行calculator类

我在计算器课上收到的错误有:

  • inputString无法解析为变量
  • 重复字段计算器.i(我的for循环)
  • 还有很多关于这些符号的语法错误(,)
主类

计算器类

package com.haynespro.calculator;
导入java.util.ArrayList;
公共类计算器{
//将字符串“res”的ArrayList指定给splitExpression
ArrayList res=拆分表达式(inputString);
//创建一个包含res的ObjectList
ArrayList objectList=新的ArrayList(res);
System.out.print(“\n让我的算法处理它:\n\n”);
//循环遍历objectList并将字符串转换为双精度字符串
对于(int i=0;i
您需要将代码放入类中的方法中。例如:

public static void doStuff(String inputString) {
    // Assign ArrayList of Strings "res" to splitExpression
    ArrayList<String> res = splitExpression(inputString);
    // Create an ObjectList that holds res
    ArrayList<Object> objectList = new ArrayList<Object>(res);
    System.out.print("\n Let my algorithm take care of it: \n\n");

    // (...)  REST OF YOUR CODE

    for (int i = 0; i < maxi; i += 2) {
        String operator = (String) objectList.get(i + 1);
        double a = (Double) objectList.get(i);
        double b = (Double) objectList.get(i + 2);
        double sum;

        if (i == 0) {
            sum = sum(a, b, operator);
        } else {
            sum = sum(lastSum, b, operator);
        }
        lastSum = sum;
        System.out.println(lastSum);
    }
}
另一个提示:扫描器可能会抛出异常-所以您需要
尝试。。抓住他们。由于您在最后“关闭”了扫描仪,因此您可以使用较短的资源,如下所示:

public static void main(String[] args) {
    String inputString = "0";

    // the code with the scanner comes here...

    doStuff(inputString);
}
try (Scanner user_input = new Scanner(System.in)) {         // the scanner is only available inside the try block - and in each case (exception or not) it will be closed.
    System.out.print("please insert your calculations: ");
    inputString = user_input.next();
}
最后一个提示:在循环中,您有一个
try…catch
,它捕获一个
NumberFormatException
。最好是处理异常。例如,为用户打印一条消息,以便他知道发生了什么或设置默认数字


希望这有助于

在类计算器中为您的代码使用一种方法。我这样做了,但仍然不起作用。我将显示它作为答案。好的,我的第一个问题解决了,我仍然有很多问题,大多数编译错误仍然存在。他现在开始抱怨我的splitexpression方法没有为类型计算器定义。该方法本身表示重复的局部变量inputString和参数splitExpression的非法修饰符;只允许使用final。它正在工作:-)我必须将新方法的}放在正确的位置!如果我选择了你的位置,我会让新方法返回结果(而不仅仅是打印结果)。这也可以帮助你保持对你的程序的全面了解。通常算法有一些输入和一些输出-所以把这些东西放在不同的方法中,让主方法一个接一个地进行调用。如果我从公共静态void main(String[]args)中给出args,我还有一件事要做。如何将其作为我的inputString,以便它自动计算这些参数?当没有给出args时,他要求输入一些东西……
args
是一个
String[]
因此,当您从控制台调用
javamyprogram hello
时,在主方法中,
args[0]
将包含字符串
“hello”
。但是请注意,
args
可以是
null
empty
或有多个参数:
javamyprogrammhelloworld
(其中
args[0]
将是
“hello”
,而
args[1]
将是
“world”
public static void main(String[] args) {
    String inputString = "0";

    // the code with the scanner comes here...

    doStuff(inputString);
}
try (Scanner user_input = new Scanner(System.in)) {         // the scanner is only available inside the try block - and in each case (exception or not) it will be closed.
    System.out.print("please insert your calculations: ");
    inputString = user_input.next();
}