(admit.java)小程序没有在方法之间传递一个整数?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (54)

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

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

这是一个非常基本的程序,我知道......我的编码技巧不是很好,但任何帮助都会受到赞赏。

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

我对编码和社区以及工作原理仍然很陌生......

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”。但是你真的永远不会改变这个值,而你编写方法的方式让我相信你期望testcore在方法中改变。

尝试在main中打印testscore的值,看它不会改变它的值(在那个main中它应该总是0)。这可能是您正在寻找的:

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

所属标签

可能回答问题的人

  • 找虫虫

    0 粉丝0 提问6 回答
  • 优惠活动秘书

    0 粉丝2 提问6 回答
  • 天使的炫翼

    17 粉丝531 提问5 回答
  • 爸爸

    腾讯 · 客户端安全 (已认证)

    4 粉丝4 提问5 回答

扫码关注云+社区

领取腾讯云代金券