(admit.java)我的小程序没有在方法之间传递整数



我有点困惑,我有一个为学校作业编写的小程序,我需要使用的约束是任何方法都不能包含超过 15 行代码,我们不能在下一个循环中使用任何代码。

我似乎无法弄清楚的是为什么我能够将整数传递给我的舍入方法,它甚至返回舍入值,但随后它会跳回到我的 if/then 语句(它不应该这样做(并且不传递变量。

我知道这是一个非常基本的程序...我的编码技能不是很好,但任何帮助将不胜感激。

我需要传回的变量是 testscore 和 GPA,它们需要返回到 main 方法,以便它们可以存储到新变量中,最后推送到 result 方法中。

我对编码和社区以及事情的运作方式仍然很陌生......

import java.util.*;
public class Admit {
public static void main(String[] Args) {
Scanner console = new Scanner(System.in);
Introduction();
double testscore = 0;
double GPA = 0;
int Student = 1;
ACTSATScores(1,0,console);
double StudentOneTestScore = testscore;
GPAInfo(0,console);
double StudentOneGPAScore = GPA;
Student = 2;
ACTSATScores(2,0,console);
double StudentTwoTestScore = testscore;
GPAInfo(0,console);
double StudentTwoGPAScore = GPA;
DisplayResults(StudentOneTestScore,StudentOneGPAScore,StudentTwoTestScore,StudentTwoGPAScore);
}
public static void Introduction() {
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();   
}
public static double ACTSATScores(int Student,double testscore,Scanner console) {
System.out.println("Information for applicant #" + Student + ":");
System.out.print("do you have 1) SAT scores or 2) ACT scores? ");
int ACTSAT = console.nextInt();
if ( ACTSAT == 1) {
SAT(Student,testscore,console);
}
if ( ACTSAT == 2) {
ACT(Student,testscore,console);
}
return testscore;
}
public static double SAT(int Student,double testscore,Scanner console) {
System.out.print("SAT math? ");
int SATMath = console.nextInt();
System.out.print("SAT critical reading? ");
int SATReading = console.nextInt();
System.out.print("SAT writing? ");
int SATWriting = console.nextInt();
testscore = ( ( 2 * SATMath + SATReading + SATWriting ) / 32);
System.out.println("exam score = " +  roundNumber(testscore));
return ( ( 2 * SATMath + SATReading + SATWriting ) / 32);
}
public static double ACT(int Student,double testscore,Scanner console) {
System.out.print("ACT English? ");
int ACTEnglish = console.nextInt();
System.out.print("ACT math? ");
int ACTMath = console.nextInt();
System.out.print("ACT reading? ");
int ACTReading = console.nextInt();
System.out.print("ACT science? ");
int ACTScience = console.nextInt();
testscore = ( ( 2 * ACTMath + ACTEnglish + ACTReading + ACTScience ) / 1.8 );
System.out.println("exam score = " + roundNumber(testscore));
return testscore;
}
public static double GPAInfo(double GPA,Scanner console) {
System.out.print("overall GPA? ");
double OverallGPA = console.nextDouble();
System.out.print("max GPA? ");
double MaxGPA = console.nextDouble();
System.out.print("Transcript Multiplier? ");
double TranscriptMultiplier = console.nextDouble();
GPA = ( OverallGPA / MaxGPA * 100 * TranscriptMultiplier );
System.out.println("GPA score = " + roundNumber(GPA));
return GPA;
}
public static double roundNumber(double number) {
return (Math.round(number * 10)) / 10.0;
}
public static double DisplayResults(double StudentOneTestScore, double StudentOneGPAScore, double StudentTwoTestScore, double StudentTwoGPAScore) {
System.out.println("First applicant overall score = " + StudentOneTestScore + StudentOneGPAScore);
System.out.println("Second applicant overall score = " + StudentTwoTestScore + StudentTwoGPAScore);
if ( StudentOneTestScore + StudentOneGPAScore > StudentTwoTestScore + StudentTwoGPAScore ) {
System.out.println("The first applicant seems to be better");
}
else if ( StudentOneTestScore + StudentOneGPAScore < StudentTwoTestScore + StudentTwoGPAScore ) {
System.out.println("The second applicant seems to be better");
}
else {
System.out.println("The two applicants seem to be equal");
}
return StudentOneTestScore;
}
}

预期产出:

This program compares two applicants to
determine which one seems like the stronger
applicant.  For each candidate I will need
either SAT or ACT scores plus a weighted GPA.
 
Information for applicant #1:
    do you have 1) SAT scores or 2) ACT scores? 1
    SAT math? 450
    SAT critical reading? 530
    SAT writing? 490
    exam score = 60.0
    overall GPA? 3.4
    max GPA? 4.0
    Transcript Multiplier? 0.9
    GPA score = 76.5
 
Information for applicant #2:
    do you have 1) SAT scores or 2) ACT scores? 2
    ACT English? 25
    ACT math? 20
    ACT reading? 18
    ACT science? 15
    exam score = 54.4
    overall GPA? 3.3
    max GPA? 4.0
    Transcript Multiplier? 0.95
    GPA score = 78.4
 
First applicant overall score  = 136.5
Second applicant overall score = 132.8
The first applicant seems to be better

我的输出

This program compares two applicants to
determine which one seems like the stronger
applicant.  For each candidate I will need
either SAT or ACT scores plus a weighted GPA.
Information for applicant #1:
do you have 1) SAT scores or 2) ACT scores? 1
SAT math? 450
SAT critical reading? 530
SAT writing? 490
exam score = 60.0
overall GPA? 3.4
max GPA? 4.0
Transcript Multiplier? 0.9
GPA score = 76.5
Information for applicant #2:
do you have 1) SAT scores or 2) ACT scores? 2
ACT English? 25
ACT math? 20
ACT reading? 18
ACT science? 15
exam score = 54.4
overall GPA? 3.3
max GPA? 4.0
Transcript Multiplier? 0.95
GPA score = 78.4
First applicant overall score = 0.00.0
Second applicant overall score = 0.00.0
The two applicants seem to be equal

为任何需要的人更正了代码

import java.util.*;
public class Admit {
public static void main(String[] Args) {
Scanner console = new Scanner(System.in);
Introduction();
int Student = 1;
double StudentOneTestScore = ACTSATScores(1,console);
double StudentOneGPAScore = GPAInfo(0,console);
Student = 2;
double StudentTwoTestScore = ACTSATScores(1,console);
double StudentTwoGPAScore = GPAInfo(0,console);
DisplayResults(StudentOneTestScore,StudentOneGPAScore,StudentTwoTestScore,StudentTwoGPAScore);
}
public static void Introduction() {
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();   
}
public static double ACTSATScores(int Student,Scanner console) {
double testscore = 0;
System.out.println("Information for applicant #" + Student + ":");
System.out.print("    do you have 1) SAT scores or 2) ACT scores? ");
int ACTSAT = console.nextInt();
if ( ACTSAT == 1) {
testscore = SAT(console);
}
if ( ACTSAT == 2) {
testscore = ACT(console);
}
return testscore;
}
public static double SAT(Scanner console) {
System.out.print("    SAT math? ");
int SATMath = console.nextInt();
System.out.print("    SAT critical reading? ");
int SATReading = console.nextInt();
System.out.print("    SAT writing? ");
int SATWriting = console.nextInt();       
double testscore = ( ( 2 * SATMath + SATReading + SATWriting ) / 32);
System.out.println("    exam score = " +  roundNumber(testscore));
return testscore;
}
public static double ACT(Scanner console) {
System.out.print("    ACT English? ");
int ACTEnglish = console.nextInt();
System.out.print("    ACT math? ");
int ACTMath = console.nextInt();
System.out.print("    ACT reading? ");
int ACTReading = console.nextInt();
System.out.print("    ACT science? ");
int ACTScience = console.nextInt();
double testscore = ( ( 2 * ACTMath + ACTEnglish + ACTReading + ACTScience ) / 1.8 );
System.out.println("    exam score = " + roundNumber(testscore));
return testscore;
}
public static double GPAInfo(double GPA,Scanner console) {
System.out.print("    overall GPA? ");
double OverallGPA = console.nextDouble();
System.out.print("    max GPA? ");
double MaxGPA = console.nextDouble();
System.out.print("    Transcript Multiplier? ");
double TranscriptMultiplier = console.nextDouble();
GPA = ( OverallGPA / MaxGPA * 100 * TranscriptMultiplier );
System.out.println("    GPA score = " + roundNumber(GPA));
System.out.println();
return GPA;
}
public static double roundNumber(double number) {
return (Math.round(number * 10)) / 10.0;
}
public static double finalScore(double TestScore, double GPAScore) {
return TestScore + GPAScore;
}
public static double DisplayResults(double StudentOneTestScore, double StudentOneGPAScore, double StudentTwoTestScore, double StudentTwoGPAScore) {
double StudentOneScore = finalScore(StudentOneTestScore,StudentOneGPAScore);
double StudentTwoScore = finalScore(StudentTwoTestScore,StudentTwoGPAScore);
System.out.println("First applicant overall score = " + roundNumber(StudentOneScore)); //StudentOneTestScore + StudentOneGPAScore);
System.out.println("Second applicant overall score = " + roundNumber(StudentTwoScore)); //StudentTwoTestScore + StudentTwoGPAScore);
if ( StudentOneTestScore + StudentOneGPAScore > StudentTwoTestScore + StudentTwoGPAScore ) {
System.out.println("The first applicant seems to be better");
} else if ( StudentOneTestScore + StudentOneGPAScore < StudentTwoTestScore + StudentTwoGPAScore ) {
System.out.println("The second applicant seems to be better");
} else {
System.out.println("The two applicants seem to be equal");
}
return StudentOneTestScore;
}
}

使用方法、返回类型和不可变类型时似乎遇到了问题。 在主要部分:

double testscore = 0;
double GPA = 0;
int Student = 1;
ACTSATScores(1,0,console);
double StudentOneTestScore = testscore;
GPAInfo(0,console);
double StudentOneGPAScore = GPA;
Student = 2;
ACTSATScores(2,0,console);
double StudentTwoTestScore = testscore;

您正在为这些变量分配值"testscore"。但是你真的永远不会改变这个值,你编写方法的方式让我相信你期待 testscore 在方法内部改变。

尝试在主服务器中打印 testscore 的值,以查看它不会更改其值(在该主服务器中应始终为 0(。这可能是您要查找的内容:

double StudentOneTestScore = ACTSATScores(1,0,console);

最新更新