java - 为什么我的代码在 ideone.com 中不起作用?

标签 java

import java.util.Scanner;
public class Admit {

    // the main method has minimal dialogue and just calls the other methods with the appropriate parameters.
    public static void main(String[] args) {
        promptUser();
        System.out.println("Information for the first applicant:");
        double person1Score = getAllResults();
        System.out.println();
        System.out.println("Information for the second applicant");
        double person2Score = getAllResults();
        System.out.println("First applicant's overall score  = " + person1Score);
        System.out.println("Second applicant's overall score = " + person2Score);
        compareScores (person1Score, person2Score);

    }

    // promptUser() introduces the program and what it's used for.
    public static void promptUser() {
        System.out.println("This program compares two applicants to");
        System.out.println("determine which one seems like the stronger");
        System.out.println("applicant. For each candidate I will need");
        System.out.println("either SAT or ACT scores plus a weighted GPA.");
        System.out.println();
    }

    // getAllResults() calculates the applicant's overall score to be compared,
    // depending on whether the user submitted SAT or ACT scores.
    public static double getAllResults() {
        Scanner userInput = new Scanner(System.in);
        System.out.print("\tDo you have 1) SAT scores, or 2) ACT scores? ");
        int testType = userInput.nextInt();
        // @var vinalScore is just storing an arbitrary value for now.
        double finalScore = 0;
        if (testType == 1) {
            System.out.print("\tSAT Math? ");
            int SATMath = userInput.nextInt();
            System.out.print("\tSAT Verbal? ");
            int SATVerbal = userInput.nextInt();
            System.out.print("\tActual GPA? ");
            double yourGPA = userInput.nextDouble();
            System.out.print("\tMax GPA? ");
            double maxGPA = userInput.nextDouble();
            double testScore = calculateSATScore(SATVerbal, SATMath);
            double finalGPA = calculateGPA(yourGPA, maxGPA);
            finalScore = calculateOverallScore(testScore, finalGPA);
        }
        else if (testType == 2) {
            System.out.print("\tACT English? ");
            int ACTEng = userInput.nextInt();
            System.out.print("\tACT Math? ");
            int ACTMath = userInput.nextInt();
            System.out.print("\tACT Reading? ");
            int ACTRead = userInput.nextInt();
            System.out.print("\tACT Science? ");
            int ACTSci = userInput.nextInt();
            System.out.print("\tActual GPA? ");
            double yourGPA = userInput.nextDouble();
            System.out.print("\tMax GPA? ");
            double maxGPA = userInput.nextDouble();
            double testScore = calculateACTScore(ACTEng, ACTMath, ACTRead, ACTSci);
            double finalGPA = calculateGPA(yourGPA, maxGPA); 
            finalScore = calculateOverallScore(testScore, finalGPA);
        }
        return finalScore;
    }

    // calculateSATScore() calculates the overall SAT score taken from getAllResults().
    public static double calculateSATScore(int verbal, int math) {
        double SATScore = (2 * verbal + math) / 24;
        return SATScore;
    }

    // calculateACTScore() calculates the overall ACT score taken from getAllResults().
    public static double calculateACTScore(int eng, int math, int read, int sci) {
        double ACTScore = ((2 * read) + eng + math + sci) / 1.8;
        return ACTScore;
    }

    // calculateGPA() calculates the final GPA taken from getAllResults().
    public static double calculateGPA(double uGPA, double mGPA) {
        double finalGPA = uGPA / mGPA * 100;
        return finalGPA;
    }

    // calculateOverallScore() computes the final scores to be compared, using the
    // overall test scores and the final GPA.
    public static double calculateOverallScore(double testScore, double GPA) {
        double overallScore = testScore + GPA;
        return overallScore;
    }

    // compareScores() compares the overall scores for each applicant and then prints out
    // which applicant appears to be the better choice.
    public static void compareScores (double person1, double person2) {
        if (person1 > person2) {
            System.out.println("The first applicant seems to be better.");
        }
        else {
            System.out.println("The second applicant seems to be better.");
        }
    }
}

我一直收到一个错误提示“Main.java:11: error: class Admit is public, should be declared in a file named Admit.java 公开课承认{'

最佳答案

只需遵循示例 Java 代码:http://ideone.com/samples#sample_lang_10

将“public class Admit {”更改为“public class Main {”

您的应用可能无法运行,因为看起来您使用了

    Scanner userInput = new Scanner(System.in);

接收键盘输入,ideone.com 只接受 STDIN,Scanner 可能不支持。

例如,当我运行修改后的代码时,出现以下错误:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at Main.getAllResults(Main.java:32)
at Main.main(Main.java:8)

关于java - 为什么我的代码在 ideone.com 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11789057/

相关文章:

java - App Engine 和 Commons FileUpload

java - ArrayList元素拒绝被移除

java - 将 DFDL 转换为 XML

java - 如何将本地 html 文件(不在我的类路径中)加载到 WebView?

java - 类路径中的版本控制信息

java - 如何查明 Windows 上的 Eclipse 安装正在使用哪个 JDK(而非 JRE)

java - 有没有好的方法来判断String中是否包含日期信息

java - 在 webview Android 中打开新 url 时保持 Session 属性

java - 如何在 Java 中设置 JButton 的背景颜色?

java - 遗传算法的数组帮助