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

时间:2018-10-12 21:57:28

标签: java

我有点困惑,我有一个小程序是为学校作业编写的,我需要处理的约束是没有方法可以包含超过15行代码,并且下一个循环不能使用任何代码

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

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

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

对于编程和社区以及事物的工作方式,我还是很陌生...

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;
    }
}

1 个答案:

答案 0 :(得分:0)

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

  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”。但是您实际上永远都不会更改该值,并且编写方法的方式使我相信您期望testcore在方法内部进行更改。

尝试在主目录中打印testscore的值,以查看它没有改变其值(该主目录中的值应始终为0)。这可能是您要寻找的:

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