Java启动错误选择不包含主类型

发布于 2024-10-03 17:29:47 字数 7713 浏览 6 评论 0原文

我是 Java 和 Eclipse 的新手。我用谷歌搜索了很多帮助,但仍然很困惑。在 Eclipse 中,我单击“运行”,然后选择“Java 应用程序”,我立即收到此错误。这是我的源代码:

import java.util.Arrays;
import java.util.Scanner;

public class SliceandDice {
    void main(String args[]) {
        System.out.println("This is the BIGGEST program console");
        System.out.println("Each input line will be a pair of numbers separated by a COMMA.");
        System.out.println("First number must be an unsigned number less than 10000. Max example: 9999");
        System.out.println("Second number is the 'target' number.");
        System.out.println("Example of an input line to be typed:");
        System.out.println(" 4721 , 75");
        for (int i = 1; i < 6; i++) // each time in this loop, we have a new
                                    // set.
        {
            System.out.println("Type a pair of numbers according to the syntax rules above:");
            String iLine; // Declare a variable to hold the name.
            Scanner in = new Scanner(System.in);
            iLine = in.nextLine(); // Read one line from the console.
            in.close(); // Note 2
            NumberSet set = ParseRawInput(i, iLine);
            if (set.IsValid()) {
                System.out.println("Valid inputs. Card Number to tear apart:" + set.getCardNumber()
                        + "; Target Number: " + set.getTargetNumber());
                String cardNumber = set.getCardNumber();
                int target = set.getTargetNumber();
                AnalyzeNumber(cardNumber, target); // solve for this set of
                                                   // numbers
            } else {
                System.out.println("Invalid set of numbers. Enter in format: nnnn,tttt");
            }
        }
    }

    private void AnalyzeNumber(String cn, int t) {
        int n = cn.length();
        int m = n;
        int sums = 4 + 3 + 2 + 1;
        int[] possibleAnswers = new int[sums];
        int answer = 0;
        for (int digits = 1; digits < m; digits++) {
            possibleAnswers[answer] = PossibleAnswer(0, digits, cn);
            answer++;
        }
        System.out.println("-----------");
        possibleAnswers[answer] = PossibleAnswer(1, 2, cn);
        answer++;
        possibleAnswers[answer] = PossibleAnswer(1, 3, cn);
        answer++;
        System.out.println("-----------");
        possibleAnswers[answer] = PossibleAnswer(2, 2, cn);
        answer++;
        System.out.println("-----------");
        int finalAnswer = FindBiggestNearTarget(possibleAnswers, t);
        System.out.println("Best sum (closet to target) = " + String.valueOf(finalAnswer));
    }

    private int PossibleAnswer(int extender, int lengthDigits, String cn) {
        // extender => which digit position gets multilength adjustment
        int n = cn.length();
        int[] number = new int[n]; // holds individual addends
        int sum = 0;
        int LEN = 1; // default length when we need it
        String addends = "";
        int i;
        if (extender == 0) {
            i = 0;
            while (i < n) {
                addends += cn.substring(i, lengthDigits) + " + ";
                number[i] = Integer.parseInt(cn.substring(i, lengthDigits));
                sum += number[i];
                i = i + lengthDigits; // always increment at least 1 position
                if (i + lengthDigits > n)
                    lengthDigits = 1;
            }
            System.out.println(addends + " = " + String.valueOf(sum));
            return sum;
        }
        if (extender == 1) {
            i = 0;
            while (i < n) {
                addends += cn.substring(i, LEN) + " + ";
                number[i] = Integer.parseInt(cn.substring(i, LEN));
                sum += number[i];
                if (i == 0) {
                    i++;
                    LEN = lengthDigits;
                } else if (i == 1) {
                    i = i + lengthDigits; // i = 3 (last number)
                    LEN = 1;
                } else if (i == 2) {
                    i = 1;
                    LEN = 3;
                } else {
                    i = n; // force exit of while loop
                }
                if (i + LEN > n)
                    LEN = 1;
            }
            System.out.println(addends + " = " + String.valueOf(sum));
            return sum;
        }
        if (extender == 2) {
            i = 0;
            while (i < n) {
                addends += cn.substring(i, LEN) + " + ";
                number[i] = Integer.parseInt(cn.substring(i, LEN));
                sum += number[i];
                i = i + LEN; // always increment at least 1 position
                if (i == extender) {
                    LEN = lengthDigits;
                }
                if (i + LEN > n)
                    i = n; // force out of loop
            }
            System.out.println(addends + " = " + String.valueOf(sum));
            return sum;
        }
        return 0;
    }

    private int FindBiggestNearTarget(int[] possibles, int target) {
        int[] sumArray = possibles;
        Arrays.sort(sumArray);
        // temporary variable for swapping values
        int temp;
        // reverse the array
        for (int i = 0; i < sumArray.length / 2; ++i) {
            temp = sumArray[i];
            sumArray[i] = sumArray[sumArray.length - i - 1];
            sumArray[sumArray.length - i - 1] = temp;
        }

        for (int i = 0; i < sumArray.length; i++) {
            if (sumArray[i] < target) {
                return sumArray[i];
            }
        }
        return -1; // should not occur
    }

    public static NumberSet ParseRawInput(int i, String rawInput) {
        NumberSet errorSet = new NumberSet(-1, "", 0);
        // string[] stringArray = rawInput.Split( new char[] { ','});
        String[] stringArray = rawInput.toString().split(rawInput, ',');
        if (stringArray.length != 2)
            return errorSet; // ensure 2 tokens separated by comma
        // work on 1st token:
        String cardNumberString = stringArray[0].trim(); // trim any whitespace
                                                         // from this token
        if (cardNumberString.length() > 4)
            return errorSet; // ensure token is 4 bytes or less
        // declare token as integer
        int cardNumber = Integer.parseInt(cardNumberString);

        // work on 2nd token:
        String targetNumberString = stringArray[1].trim(); // trim any
                                                           // whitespace from
                                                           // token
        if (targetNumberString.length() > 4)
            return errorSet; // ensure token is 4 bytes or less

        int targetNumber = Integer.parseInt(targetNumberString); // convert into
                                                                 // int

        NumberSet validSet = new NumberSet(i, cardNumberString, targetNumber);
        return validSet;
    }
}

class NumberSet {
    // Java getter & setter
    private String CardNumber;
    private int TargetNumber;
    private int SetNumber;

    public int getSetNumber() {
        return this.SetNumber;
    }

    public String getCardNumber() {
        return this.CardNumber;
    }

    public int getTargetNumber() {
        return this.TargetNumber;
    }

    public NumberSet(int sn, String cn, int t) {
        this.SetNumber = sn;
        this.CardNumber = cn;
        this.TargetNumber = t;
    }

    public Boolean IsValid() {
        if (this.SetNumber < 0)
            return false;
        return true;
    }
}

I am a total newbie in Java and Eclipse. I googled for lots of help but still confused. From Eclipse I click run then choose Java application and I get this error immediately. Here is my source code:

import java.util.Arrays;
import java.util.Scanner;

public class SliceandDice {
    void main(String args[]) {
        System.out.println("This is the BIGGEST program console");
        System.out.println("Each input line will be a pair of numbers separated by a COMMA.");
        System.out.println("First number must be an unsigned number less than 10000. Max example: 9999");
        System.out.println("Second number is the 'target' number.");
        System.out.println("Example of an input line to be typed:");
        System.out.println(" 4721 , 75");
        for (int i = 1; i < 6; i++) // each time in this loop, we have a new
                                    // set.
        {
            System.out.println("Type a pair of numbers according to the syntax rules above:");
            String iLine; // Declare a variable to hold the name.
            Scanner in = new Scanner(System.in);
            iLine = in.nextLine(); // Read one line from the console.
            in.close(); // Note 2
            NumberSet set = ParseRawInput(i, iLine);
            if (set.IsValid()) {
                System.out.println("Valid inputs. Card Number to tear apart:" + set.getCardNumber()
                        + "; Target Number: " + set.getTargetNumber());
                String cardNumber = set.getCardNumber();
                int target = set.getTargetNumber();
                AnalyzeNumber(cardNumber, target); // solve for this set of
                                                   // numbers
            } else {
                System.out.println("Invalid set of numbers. Enter in format: nnnn,tttt");
            }
        }
    }

    private void AnalyzeNumber(String cn, int t) {
        int n = cn.length();
        int m = n;
        int sums = 4 + 3 + 2 + 1;
        int[] possibleAnswers = new int[sums];
        int answer = 0;
        for (int digits = 1; digits < m; digits++) {
            possibleAnswers[answer] = PossibleAnswer(0, digits, cn);
            answer++;
        }
        System.out.println("-----------");
        possibleAnswers[answer] = PossibleAnswer(1, 2, cn);
        answer++;
        possibleAnswers[answer] = PossibleAnswer(1, 3, cn);
        answer++;
        System.out.println("-----------");
        possibleAnswers[answer] = PossibleAnswer(2, 2, cn);
        answer++;
        System.out.println("-----------");
        int finalAnswer = FindBiggestNearTarget(possibleAnswers, t);
        System.out.println("Best sum (closet to target) = " + String.valueOf(finalAnswer));
    }

    private int PossibleAnswer(int extender, int lengthDigits, String cn) {
        // extender => which digit position gets multilength adjustment
        int n = cn.length();
        int[] number = new int[n]; // holds individual addends
        int sum = 0;
        int LEN = 1; // default length when we need it
        String addends = "";
        int i;
        if (extender == 0) {
            i = 0;
            while (i < n) {
                addends += cn.substring(i, lengthDigits) + " + ";
                number[i] = Integer.parseInt(cn.substring(i, lengthDigits));
                sum += number[i];
                i = i + lengthDigits; // always increment at least 1 position
                if (i + lengthDigits > n)
                    lengthDigits = 1;
            }
            System.out.println(addends + " = " + String.valueOf(sum));
            return sum;
        }
        if (extender == 1) {
            i = 0;
            while (i < n) {
                addends += cn.substring(i, LEN) + " + ";
                number[i] = Integer.parseInt(cn.substring(i, LEN));
                sum += number[i];
                if (i == 0) {
                    i++;
                    LEN = lengthDigits;
                } else if (i == 1) {
                    i = i + lengthDigits; // i = 3 (last number)
                    LEN = 1;
                } else if (i == 2) {
                    i = 1;
                    LEN = 3;
                } else {
                    i = n; // force exit of while loop
                }
                if (i + LEN > n)
                    LEN = 1;
            }
            System.out.println(addends + " = " + String.valueOf(sum));
            return sum;
        }
        if (extender == 2) {
            i = 0;
            while (i < n) {
                addends += cn.substring(i, LEN) + " + ";
                number[i] = Integer.parseInt(cn.substring(i, LEN));
                sum += number[i];
                i = i + LEN; // always increment at least 1 position
                if (i == extender) {
                    LEN = lengthDigits;
                }
                if (i + LEN > n)
                    i = n; // force out of loop
            }
            System.out.println(addends + " = " + String.valueOf(sum));
            return sum;
        }
        return 0;
    }

    private int FindBiggestNearTarget(int[] possibles, int target) {
        int[] sumArray = possibles;
        Arrays.sort(sumArray);
        // temporary variable for swapping values
        int temp;
        // reverse the array
        for (int i = 0; i < sumArray.length / 2; ++i) {
            temp = sumArray[i];
            sumArray[i] = sumArray[sumArray.length - i - 1];
            sumArray[sumArray.length - i - 1] = temp;
        }

        for (int i = 0; i < sumArray.length; i++) {
            if (sumArray[i] < target) {
                return sumArray[i];
            }
        }
        return -1; // should not occur
    }

    public static NumberSet ParseRawInput(int i, String rawInput) {
        NumberSet errorSet = new NumberSet(-1, "", 0);
        // string[] stringArray = rawInput.Split( new char[] { ','});
        String[] stringArray = rawInput.toString().split(rawInput, ',');
        if (stringArray.length != 2)
            return errorSet; // ensure 2 tokens separated by comma
        // work on 1st token:
        String cardNumberString = stringArray[0].trim(); // trim any whitespace
                                                         // from this token
        if (cardNumberString.length() > 4)
            return errorSet; // ensure token is 4 bytes or less
        // declare token as integer
        int cardNumber = Integer.parseInt(cardNumberString);

        // work on 2nd token:
        String targetNumberString = stringArray[1].trim(); // trim any
                                                           // whitespace from
                                                           // token
        if (targetNumberString.length() > 4)
            return errorSet; // ensure token is 4 bytes or less

        int targetNumber = Integer.parseInt(targetNumberString); // convert into
                                                                 // int

        NumberSet validSet = new NumberSet(i, cardNumberString, targetNumber);
        return validSet;
    }
}

class NumberSet {
    // Java getter & setter
    private String CardNumber;
    private int TargetNumber;
    private int SetNumber;

    public int getSetNumber() {
        return this.SetNumber;
    }

    public String getCardNumber() {
        return this.CardNumber;
    }

    public int getTargetNumber() {
        return this.TargetNumber;
    }

    public NumberSet(int sn, String cn, int t) {
        this.SetNumber = sn;
        this.CardNumber = cn;
        this.TargetNumber = t;
    }

    public Boolean IsValid() {
        if (this.SetNumber < 0)
            return false;
        return true;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如果没有 2024-10-10 17:29:47

您无法像在 C++ 中那样定义 main 方法。

对于 Java 解释器,main 方法必须始终是公共且静态的。因此,您需要将 main 方法签名更改为

public static void main(String args[])

Try this,并随时进一步询问。 :-)

You cannot define the main method as you do in C++.

For the Java interpreter, the main method must always be public and static. So you need to change your main method signature to

public static void main(String args[])

Try this, and feel free to ask further. :-)

放赐 2024-10-10 17:29:47

创建一个新项目并使构建路径指向现有的源文件夹。这将解决“选择不包含主类型”错误。

Create a new project and make the build path to point to the existing source folders. This will resolve the "Selection does not contain a main type" error.

下雨或天晴 2024-10-10 17:29:47

我也遇到过这个问题并且可以解决。目前还没有提到解决方案。

就我而言,我使用 Xtend 在 Eclipse 中工作,并将项目转换为 Maven。之后,您的类路径文件中有一个源排除条目:

<classpathentry excluding="**" kind="src" output="target/classes" path="xtend-gen">

然后您只需删除此行并执行干净的构建。

I also experienced this problem and could solve it. The solution was not mentioned yet.

In my case I'm working in Eclipse with Xtend and I converted the project to maven. After that there was a source-exclusion entry in your classpath file:

<classpathentry excluding="**" kind="src" output="target/classes" path="xtend-gen">

Then you just need to delete this line and do a clean build.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文