如何在Java中返回两个值

时间:2015-08-03 17:31:57

标签: java variables return limit return-value

我知道java通常不会返回两个值,但我试图将值作为方法返回..从那里我想在main方法的字符串中使用score1和maxScore1的值。 getHwGrades是我遇到问题的方法。我得到“错误:找不到符号”。我不允许在这个程序中使用数组。另外请注意,我也不应该使用任何if / else语句,但我找不到任何其他方法将discussionGrade的值限制为20.任何帮助表示赞赏

import java.util.Scanner;

public class Grades
{

public static void main(String[] args)
{
Scanner getG = new Scanner(System.in);

int weight1;
int weight2;
int weight3;
int score2 = 0;
int maxScore2 = 0;
int sections;
int discussionGrade;






System.out.println("What is the weight for the HW and Exam 1?");
weight1 = getG.nextInt();
weight2 = getG.nextInt();
weight3 = getWeight(weight1, weight2);
System.out.println("Using weights of " + weight1 + " " + weight2 + " " + weight3);


 getHwGrades();
 sections = numberSections();
 discussionGrade = calcDGrade(sections);

System.out.println("What is your grade for exam1?"); //move under hw total points and final grade, add curve
score2 = getG.nextInt();
maxScore2 = getG.nextInt();


System.out.println("Total Points =" + (score1+ discussionGrade) + "/ "(maxScore1 + 20));

}



public static int getHwGrades()//method for asking and storing hw grades
{
int score1;
int maxScore1;
int nOfA;
Scanner getG = new Scanner(System.in);

System.out.println("How many HW assignments are there?");
nOfA = getG.nextInt();

 for (int i = 1; i <= nOfA; i++) //loop that asks for the grades corresponding to the amount of hw assignments there were
 {
 System.out.println("What is your grade and then the max grade for assignment " + i + "?");
 score1 += getG.nextInt();
 maxScore1 += getG.nextInt();
 }

 return new getHwGrade(score1, maxScore1); //returns results of inputs into method holding the 2 variables
}



public static int numberSections() //finds out how many sections the student attended
{
Scanner getG = new Scanner(System.in);
System.out.println("How many sections did you attend?");
return getG.nextInt();
}

public static int calcDGrade(int sections) //method to calculate the grade for the students sections
{
int maxDGrade = ((sections*4)); if (maxDGrade > 20) //limits total score to 20

{
   return 20;
} 
else 
{ 
return maxDGrade;
}

}



public static int getWeight(int weight1, int weight2)//returns the weight that will be used for weight3
{
   return (100-(weight1 + weight2));

}

public static double round2(double number) 
{
return Math.round(number * 100.0) / 100.0;
}



}

2 个答案:

答案 0 :(得分:2)

由于您只能返回单个值,因此需要返回单个值。 :-)通常,如果一个方法需要返回复杂的信息,你可以这样做:

  1. 返回一个新创建的类实例,该类包含各个信息的字段

  2. 让方法填写一个传递给它的类的实例(通常不理想,除非有充分理由这样做)

  3. 例如:

    class HwGrades {
        private int score1;
    
        private int maxScore1;
    
        ScoreInfo(int _score1, int _maxScore1) {
            this.score1 = _score1;
            this.maxScore1 = _maxScore1;
        }
    
        // ...accessors as appropriate, presumably at least getters...
    }
    

    然后从HwGrades返回getHwGrades的实例:

    public static int getHwGrades()//method for asking and storing hw grades
    {
        int score1;
        int maxScore1;
        // ...
        return new HwGrades(score1, maxScore1);
    }
    

    如果您需要,可以通过制作HwGrades 接口进一步解耦,然后使用私有类实现,这样API就不会与具体课程。对于一个小型学校项目来说几乎肯定是过度杀伤。

答案 1 :(得分:-3)

getHwGrade应该是一个班级。 有一个像这样的课程

class getHwGrade{
int a;
int b;
public getHwGrade(int a,b){
this.a=a;this.b=b;
}