如何在Java中返回两个值

如何在Java中返回两个值,java,variables,return,limit,return-value,Java,Variables,Return,Limit,Return Value,我知道java通常不能返回两个值,但我尝试将该值作为一个方法返回。。从这里开始,我想在main方法的字符串中使用score1和maxScore1的值。getHwGrades是我遇到问题的方法。我得到“错误:找不到符号”。我不允许在这个程序中使用数组。另一方面,我也不应该使用任何if/else语句,但我找不到任何其他方法将discussionGrade的值限制为20。谢谢你的帮助 import java.util.Scanner; public class Grades { public st

我知道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;
}



}
import java.util.Scanner;
公营班级职系
{
公共静态void main(字符串[]args)
{
扫描仪getG=新扫描仪(System.in);
int权重1;
int权重2;
int权重3;
int评分2=0;
int maxScore2=0;
int段;
国际讨论级;
System.out.println(“硬件和考试1的权重是多少?”);
weight1=getG.nextInt();
weight2=getG.nextInt();
权重3=获取权重(权重1,权重2);
System.out.println(“使用“+weight1+”“+weight2+”“+weight3”的权重);
getHwGrades();
sections=numberSections();
discussionGrade=calcDGrade(截面);
System.out.println(“你的考试成绩是多少?”);//移动到hw总分和最终成绩下,添加曲线
score2=getG.nextInt();
maxScore2=getG.nextInt();
System.out.println(“总分=”+(分数1+讨论等级)+“/”(最高分数1+20));
}
public static int getHwGrades()//用于请求和存储hw等级的方法
{
int评分1分;
int-maxScore1;
int nOfA;
扫描仪getG=新扫描仪(System.in);
System.out.println(“有多少硬件分配?”);
nOfA=getG.nextInt();
for(inti=1;i20)//将总分限制为20
{
返回20;
} 
其他的
{ 
返回maxDGrade;
}
}
publicstaticintgetweight(intweight1,intweight2)//返回将用于weight3的权重
{
收益率(100-(权重1+权重2));
}
公共静态双循环2(双倍数字)
{
返回数学四舍五入(数字*100.0)/100.0;
}
}

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

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

  • 让方法填充传递给它的类的实例(除非有充分的理由,否则通常不理想)

  • 例如:

    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...
    }
    
    然后从
    getHwGrades
    返回
    HwGrades
    的实例:

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


    如果需要,您可以通过使
    HwGrades
    成为一个接口来进一步解耦,然后使用私有类实现该接口,这样API就不会绑定到特定的类。几乎可以肯定的是,对于一个小型学校项目来说,这是一种过火的行为。

    getHwGrade预计将成为一个班级。 有这样的课吗

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

    请在格式化您的代码方面投入更多的精力。您的代码没有一致地缩进,在同一行的其他语句后面有
    if
    语句,多个空行表示不明显,并且代码比我猜想的要多。如果一个方法只能返回一个值,因此,我最好的建议是用分隔符将两个值合并,然后再拆分它们。顺便说一句,您希望
    返回新的getHwGrade
    做什么?据我所知,您没有一个名为
    getHwGrade
    的类……对不起,我在试图将其全部显示在代码块中时遇到了问题,因此我不得不继续与它混淆。我使用的是jGraspAre,你允许使用其他类吗?我认为您使用
    返回新的getHwGrade(score1,maxScore1)是正确的,但当然不能创建方法的实例。所以我需要使用此信息创建getHwGrades类?那么返回新的getHwGrades行是否正确?@dlnhnh:不要调用它
    getHwGrades
    ,它看起来像一个方法名。但是,您可以创建一个类,可能是
    HwGrades
    ,然后
    返回新的HwGrades(score1,maxScore1)我已经更新了答案以显示这样做。为什么要在类中声明私有int?@dlnhnh:默认为封装,除非有很好的理由执行其他操作。:-)在任何一个像样的IDE中,这不是更多的工作(例如,您可以让IDE生成访问器),这意味着您可以做一些事情,比如使
    HwGrades
    不可变。