gpt4 book ai didi

java - Java 中的 Getter 类接受用户输入

转载 作者:行者123 更新时间:2023-11-30 06:36:11 24 4
gpt4 key购买 nike

这里是 Java 新手,我有一项作业,需要编写一个类来创建学生对象,然后编写一个驱动程序。

我已经完成了所有要求,但我在这方面遇到了麻烦 - “编写接受测试编号并返回适当分数的 getTestScore 方法。”

我相信我已经正确编写了该类,但我在为驱动程序编写代码时遇到了困难。

在提示用户输入测试编号后,它没有返回任何内容,并且程序终止而不是返回适当的分数。

非常感谢您的帮助!

这是类(class):

public class Student
{
private String firstName, lastName;
private String homeAddress, schoolAddress;
private int testScore1, testScore2, testScore3;

//Constructors
public Student()
{
firstName = "None";
lastName = "None";
homeAddress = "None";
schoolAddress = "None";
testScore1 = 0;
testScore2 = 0;
testScore3 = 0;
}

public Student(String first, String last, String home, String school, int score1, int score2, int score3)
{
firstName = first;
lastName = last;
homeAddress = home;
schoolAddress = school;
testScore1 = score1;
testScore2 = score2;
testScore3 = score3;
}

//Setter for test scores
public void setTestScore(int testNum, int score)
{
if (testNum == 1)
testScore1 = score;
else
if (testNum == 2)
testScore2 = score;
else
if (testNum == 3)
testScore3 = score;
else
throw new IllegalArgumentException(testNum + " is out of range");
}
//Getter for test scores
public int getTestScore(int testNum2)
{
if (testNum2 == 1)
return testScore1;
else
if (testNum2 == 2)
return testScore2;
else
if (testNum2 == 3)
return testScore3;
else
throw new IllegalArgumentException(testNum2 + " is out of range");
}
//Calculates average for each student
public int getAverage()
{
int average = (testScore1 + testScore2 + testScore3)/3;
return average;
}
//Returns a description of this Student object
public String toString()
{
String result;

result = firstName + " " + lastName + "\n";
result += "Home Address:\n" + homeAddress + "\n";
result += "School Address:\n" + schoolAddress + "\n";
result += "Test Score 1:\n" + testScore1 + "\n";
result += "Test Score 2:\n" + testScore2 + "\n";
result += "Test Score 3:\n" + testScore3 + "\n";
result += "Average Test Score:\n" + ((testScore1+testScore2+testScore3)/3);

return result;
}
}

这是驱动程序:

package lab7;

import java.util.Scanner;

public class StudentBody {

public static void main(String[] args)
//Create student objects
{
Student snm24 = new Student("Sarah", "M", "18 79th Street", "5000 Forbes Ave.", 95, 80, 63);
Student adk28 = new Student("Andrew", "K", "16 Collins Ave.", "16401 NW 37th Ave.", 90, 82, 76);

//Get average for snm24
snm24.getAverage();
System.out.println("snm24 average initial: " + snm24.getAverage());

//Set new test score for test 3 for snm24 and see new average
snm24.setTestScore(3, 68);
System.out.println("snm24 average after adjustment: " +snm24.getAverage());

//Get test score for adk28
Scanner scan = new Scanner(System.in);
System.out.println("Which test score are you looking for?:");
int testNum2 = scan.nextInt();
adk28.getTestScore(testNum2);
}
}

最佳答案

it isn't returning anything and the program terminates. Help is very appreciated!

这里>

adk28.getTestScore(testNum2);

返回的值正在丢失..用它做一些事情,打印它,将它分配给一个变量:

int result =  adk28.getTestScore(testNum2);

关于java - Java 中的 Getter 类接受用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45180566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com