gpt4 book ai didi

Java - 获取 StackOverflowError

转载 作者:行者123 更新时间:2023-12-01 06:58:28 25 4
gpt4 key购买 nike

这是我的程序,我不断收到此错误

Exception in thread "main" java.lang.StackOverflowError

这是作业

Description
For this program, you are to create a Quarterback class. This is a simple object that represents a quarterback in the NFL. You may create additional private methods that you feel are necessary or helpful to implement this class.

package quarterback;    
public class Quarterback
{

private int attempts;
private int completions;
private String firstName;
private int interceptions;
private String lastName;
private int touchdowns;
private int yards;
private int passerRating;
//************************************************************
public Quarterback()
{
new Quarterback();
}

//****************************************************************

public Quarterback(String firstName, String lastName, int completions, int attempts, int yards, int interceptions, int touchdowns)
{
this.firstName = firstName;
this.lastName = lastName;
this.completions = completions;
this.attempts = attempts;
this.yards = yards;
this.interceptions = interceptions;
this.touchdowns = touchdowns;

}

//*****************************************************************

public Quarterback copy()
{
Quarterback o = new Quarterback();
o.firstName = this.firstName;
o.lastName = this.lastName;
o.completions = this.completions;
o.attempts = this.attempts;
o.yards = this.yards;
o.interceptions = this.interceptions;
o.touchdowns = this.touchdowns;
return o;



}

//******************************************************************

public boolean equals(Object o)
{
if (((Quarterback) o).getAttempts() == this.attempts) return true;
if (((Quarterback) o).getCompletions() == this.completions) return true;
if (((Quarterback) o).getFirstName() == this.firstName) return true;
if (((Quarterback) o).getInterceptions() == this.interceptions) return true;
if (((Quarterback) o).getLastName() == this.getLastName()) return true;
if (((Quarterback) o).getTouchdowns() == this.touchdowns) return true;
if (((Quarterback) o).getYards() == this.yards) return true;

return false;
}



//*********************************************************************

public int getAttempts()
{
return this.attempts;
}

//*******************************************************************************

public int getCompletions()
{
return this.completions;
}

//**********************************************************

public String getFirstName()
{
return this.firstName;
}

//*******************************************************************************

public int getInterceptions()
{
return this.interceptions;

}

//****************************************************************************

public String getLastName()
{
return this.lastName;
}

//****************************************************************************

public double getRating(int a, int b, int c, int d, int passerRating)
{
a = (int) (((this.completions/this.attempts)-.3)*5);
b = (int) (((this.yards / this.attempts)-3)*.25);
c = ((this.touchdowns / this.attempts) *20);
d = (int) (((this.interceptions/this.attempts)*25)-2.375);

passerRating = (((a + b + c + d)/6)*100);
return passerRating;
}

//****************************************************************************

public int getTouchdowns()
{
return this.touchdowns;

}

//*****************************************************************************

public int getYards()
{
return this.yards;
}

//***************************

public void setAttempts(int attempts)
{
this.attempts = attempts;

}

//*******************************************************************************

public void setCompletions(int completions)
{
this.completions = completions;

}

//*******************************************************************************

public void setFirstName(String firstName)
{
this.firstName = firstName;

}
//*******************************************************************************

public void setInterceptions(int interceptions)
{
this.interceptions = interceptions;
}
//*****************************************************************************

public void setLastName(String lastName)
{
this.lastName = lastName;
}
//*****************************************************************************

public void setTouchdowns(int touchdowns)
{
this.touchdowns = touchdowns;
}
//***********************************************

public void setYards(int yards)
{
this.yards = yards;
}
//*****************************************************************************

public String toString()
{
System.out.println(this.firstName + " " + this.lastName + passerRating);
return toString();
}
}

这是驱动程序

package quarterback;

import java.util.Scanner;

public class QBtester {


public static void main(String[] args) {
// Create a Quarterback
Quarterback qb = new Quarterback ();

// Test OK quarterback
qb.setFirstName("qb");
qb.setLastName("ok");
qb.setAttempts(465);
qb.setCompletions(272);
qb.setYards(2972);
qb.setTouchdowns(18);
qb.setInterceptions(12);
if ("qb ok 79.6".equals(qb.toString())) {
System.out.println("Passed OK QB");
} else {
System.out.println("Failed OK QB");
System.out.println("Expected: qb ok 79.6");
System.out.println("Got: " + qb);
}

// Test max quarterback
qb.setFirstName("qb");
qb.setLastName("max");
qb.setAttempts(100);
qb.setCompletions(78);
qb.setYards(1275);
qb.setTouchdowns(12);
qb.setInterceptions(0);
if ("qb max 158.3".equals(qb.toString())) {
System.out.println("Passed max QB");
} else {
System.out.println("Failed max QB");
System.out.println("Expected: qb max 158.3");
System.out.println("Got: " + qb);
}

// Test completions too high
qb.setFirstName("qb");
qb.setLastName("comp2hi");
qb.setAttempts(100);
qb.setCompletions(78);
qb.setYards(1000);
qb.setTouchdowns(7);
qb.setInterceptions(5);
if ("qb comp2hi 110.8".equals(qb.toString())) {
System.out.println("Passed completions too high");
} else {
System.out.println("Failed completions too high");
System.out.println("Expected: qb comp2hi 110.8");
System.out.println("Got: " + qb);
}

// Test completions too low
qb.setFirstName("qb");
qb.setLastName("comp2low");
qb.setAttempts(100);
qb.setCompletions(25);
qb.setYards(1000);
qb.setTouchdowns(7);
qb.setInterceptions(5);
if ("qb comp2low 71.3".equals(qb.toString())) {
System.out.println("Passed completions too low");
} else {
System.out.println("Failed completions too low");
System.out.println("Expected: qb comp2low 71.3");
System.out.println("Got: " + qb);
}

// Test completions 0
qb.setFirstName("qb");
qb.setLastName("comp0");
qb.setAttempts(100);
qb.setCompletions(30);
qb.setYards(1000);
qb.setTouchdowns(7);
qb.setInterceptions(5);
if ("qb comp0 71.3".equals(qb.toString())) {
System.out.println("Passed completions 0");
} else {
System.out.println("Failed completions 0");
System.out.println("Expected: qb comp0 71.3");
System.out.println("Got: " + qb);
}

// Test yards too high
qb.setFirstName("qb");
qb.setLastName("yards2hi");
qb.setAttempts(100);
qb.setCompletions(50);
qb.setYards(1275);
qb.setTouchdowns(7);
qb.setInterceptions(5);
if ("qb yards2hi 98.3".equals(qb.toString())) {
System.out.println("Passed yards too high");
} else {
System.out.println("Failed yards too high");
System.out.println("Expected: qb yards2hi 98.3");
System.out.println("Got: " + qb);
}

// Test yards too low
qb.setFirstName("qb");
qb.setLastName("yards2low");
qb.setAttempts(100);
qb.setCompletions(50);
qb.setYards(250);
qb.setTouchdowns(7);
qb.setInterceptions(5);
if ("qb yards2low 58.8".equals(qb.toString())) {
System.out.println("Passed yards too low");
} else {
System.out.println("Failed yards too low");
System.out.println("Expected: qb yards2low 58.8");
System.out.println("Got: " + qb);
}

// Test yards 0
qb.setFirstName("qb");
qb.setLastName("yards0");
qb.setAttempts(100);
qb.setCompletions(50);
qb.setYards(300);
qb.setTouchdowns(7);
qb.setInterceptions(5);
if ("qb yards0 58.8".equals(qb.toString())) {
System.out.println("Passed yards 0");
} else {
System.out.println("Failed yards 0");
System.out.println("Expected: qb yards0 58.8");
System.out.println("Got: " + qb);
}

// Test td too high
qb.setFirstName("qb");
qb.setLastName("td2hi");
qb.setAttempts(100);
qb.setCompletions(50);
qb.setYards(1000);
qb.setTouchdowns(14);
qb.setInterceptions(5);
if ("qb td2hi 104.2".equals(qb.toString())) {
System.out.println("Passed td too high");
} else {
System.out.println("Failed td too high");
System.out.println("Expected: qb td2hi 104.2");
System.out.println("Got: " + qb);
}

// Test td 0
qb.setFirstName("qb");
qb.setLastName("td0");
qb.setAttempts(100);
qb.setCompletions(50);
qb.setYards(1000);
qb.setTouchdowns(0);
qb.setInterceptions(5);
if ("qb td0 64.6".equals(qb.toString())) {
System.out.println("Passed td 0");
} else {
System.out.println("Failed td 0");
System.out.println("Expected: qb td0 64.6");
System.out.println("Got: " + qb);
}

// Test int too low
qb.setFirstName("qb");
qb.setLastName("int2low");
qb.setAttempts(100);
qb.setCompletions(50);
qb.setYards(1000);
qb.setTouchdowns(7);
qb.setInterceptions(11);
if ("qb int2low 69.2".equals(qb.toString())) {
System.out.println("Passed int too low");
} else {
System.out.println("Failed int too low");
System.out.println("Expected: qb int2low 69.2");
System.out.println("Got: " + qb);
}

// Test int 0
qb.setFirstName("qb");
qb.setLastName("int0");
qb.setAttempts(1000);
qb.setCompletions(500);
qb.setYards(10000);
qb.setTouchdowns(70);
qb.setInterceptions(95);
if ("qb int0 69.2".equals(qb.toString())) {
System.out.println("Passed int 0");
} else {
System.out.println("Failed int 0");
System.out.println("Expected: qb int0 69.2");
System.out.println("Got: " + qb);
}

// Test copy and equals
Quarterback qbcopy = new Quarterback();
qbcopy = qb.copy();
if (qbcopy.equals(qb)) {
// copy and equals works
System.out.println("Passed qb copy and equals");
} else {
System.out.println("Failed qb copy and equals");
System.out.println("Original qb: " + qb);
System.out.println("Copied qb: " + qbcopy);
}

}

}

最佳答案

public Quarterback()
{
new Quarterback();
}

此代码进入无限递归循环。删除new Quarterback();线。

既然我们正在做作业帮助......

public boolean equals(Object o) ,如果任意字段相等,则返回 true。更有可能的是,您希望在返回 true 之前确保所有字段都相等(使用 && 运算符)。

您的getRating函数实际上不需要任何参数。您可以删除它们。

与此相关的是,您的驱动程序总是会失败,因为通过评级永远不会被计算。删除 getRating 的参数后,替换passerRatinggetRating()Quarterback.toString() .

关于Java - 获取 StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5360700/

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