Java 数组循环正在打印引用而不是字符串
我已经被提供了一个对象和几种使用它的方法。我很难打印分配给变量的字符串。目前,我不确定是否分配了新值,并且无法打印任何值。以前的迭代只打印引用 问题:我正在分配新的字符串值吗?如何使用给定的方法打印字符串 这是我收到的代码Java 数组循环正在打印引用而不是字符串,java,if-statement,arraylist,Java,If Statement,Arraylist,我已经被提供了一个对象和几种使用它的方法。我很难打印分配给变量的字符串。目前,我不确定是否分配了新值,并且无法打印任何值。以前的迭代只打印引用 问题:我正在分配新的字符串值吗?如何使用给定的方法打印字符串 这是我收到的代码 public class Team implements Comparable<Team> { public String toString(String team, int wins) { return team + ": " + wins; } /
public class Team implements Comparable<Team> {
public String toString(String team, int wins) {
return team + ": " + wins;
}
// Data fields
private String name;
private int winCount;
Team() {
name = "Sooners";
winCount = 1;
}
Team(String inputName) {
name = inputName;
winCount = 1;
}
Team(String inputName, int inputWinCount) {
name = inputName;
winCount = inputWinCount;
}
// ----------------------------------------------------
// Getters and Setters
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the winCount
*/
public int getWinCount() {
return winCount;
}
/**
* @param winCount
* the winCount to set
*/
public void setWinCount(int winCount) {
this.winCount = winCount;
}
/**
* Increments the winCount variable by one for this Team
*/
public void incrementWinCount() {
winCount++;
}
/**
* This method allows you to check to see if this Team object has the same
* name as another Team object.
*
* This method allows you to use the contains method in ArrayList to see
* if any element in an array list has the same name as a specific Team.
*
* @param o
* the other Team being compared to.
*/
@Override
public boolean equals(Object o) {
return name.equals(((Team) o).name);
}
/**
* This method allows you to check to see if this Team object has the same
* name as another Team object
*
* @param otherTeam
* one team
*/
public boolean sameName(Team otherTeam) {
return name.equals(otherTeam.name);
}
/**
* This method allows you to check to see if this Team object has the same
* name as another Team object
*
* @param team1
* one team
* @param team2
* the other team
*/
public static boolean sameName(Team team1, Team team2) {
return team1.name.equals(team2.name);
}
/**
* This method allows you to sort an ArrayList of Team items using
* Collections.sort
*
* @param o
* the other Team being compared to.
* @return -1 if this Team item should come first, +1 if this Team item
* should come after the other, and 0 if this Team item is
* equivalent to the other.
*/
@Override
public int compareTo(Team o) {
if (this.winCount < o.winCount) {
return -1;
} else if (this.winCount > o.winCount) {
return 1;
}
return 0;
}
}
公共类团队实现了可比较的{
公共字符串toString(字符串组,整数获胜){
返回队+“:”+获胜;
}
//数据字段
私有字符串名称;
私人温克蒙特;
小组(){
name=“Sooners”;
winCount=1;
}
团队(字符串输入名称){
名称=输入名称;
winCount=1;
}
团队(字符串inputName,int inputWinCount){
名称=输入名称;
winCount=输入winCount;
}
// ----------------------------------------------------
//接球手和接球手
/**
*@返回名称
*/
公共字符串getName(){
返回名称;
}
/**
*@param name
*要设置的名称
*/
公共void集合名(字符串名){
this.name=名称;
}
/**
*@返回winCount
*/
public int getWinCount(){
返回winCount;
}
/**
*@param-winCount
*要设置的winCount
*/
公共无效设置winCount(内部winCount){
this.winCount=winCount;
}
/**
*为此团队将winCount变量增加1
*/
public void incrementWinCount(){
winCount++;
}
/**
*此方法允许您检查此团队对象是否具有相同的
*命名为另一个团队对象。
*
*此方法允许您使用ArrayList中的contains方法查看
*如果数组列表中的任何元素与特定团队具有相同的名称。
*
*@param o
*与之相比的另一支球队。
*/
@凌驾
公共布尔等于(对象o){
返回name.equals((团队)o.name);
}
/**
*此方法允许您检查此团队对象是否具有相同的
*作为另一个团队对象命名
*
*@param-otherTeam
*一队
*/
公共布尔sameName(团队其他团队){
返回name.equals(otherTeam.name);
}
/**
*此方法允许您检查此团队对象是否具有相同的
*作为另一个团队对象命名
*
*@param team1
*一队
*@param team2
*另一队
*/
公共静态布尔sameName(团队1、团队2){
返回team1.name.equals(team2.name);
}
/**
*此方法允许您使用对团队项目的ArrayList进行排序
*集合。排序
*
*@param o
*与之相比的另一支球队。
*@return-如果该团队物品优先,则为1;如果该团队物品优先,则为+1
*应位于另一个之后,如果此团队项目为
*相当于另一个。
*/
@凌驾
公共内部比较(o组){
如果(此winCounto.winCount){
返回1;
}
返回0;
}
}
这是我目前的代码
Scanner scnr = new Scanner(System.in);
Random rando = new Random();
String name = "no";
int cycles = 0;
int value = 0;
int match = 0;
ArrayList<Team> teams = new ArrayList<Team>();
Team newTeam = new Team(name,1);
System.out.println("Welcome to the Advanced Sportsball Tracker!");
while (!name.equals("x")) // looping print statement
{ // x loop begins
System.out.println("Which team just won? (x to exit)");
match = 0;
cycles++;
name = scnr.next();
for (Team list : teams)
{
if (list.getName().equals(name)) // compares the name of the team to the input value
{
match++;
}
}
if (match == 0)
{
teams.add(newTeam);
}
}// x loop ends
System.out.print(newTeam.getName());
if (cycles == 1) // prints no data if user immediately exits
{
System.out.println("No data input");
}
if (cycles > 1)
{
System.out.println("Final Tally: "); // loop to print final Talley
for (Team list : teams) // FIXME
{
list.toString(list.getName(),list.getWinCount()); // makes a string out of the string and wincount of the team
}
Scanner scnr=新扫描仪(System.in);
随机随机数=新随机数();
String name=“否”;
整数周期=0;
int值=0;
int匹配=0;
ArrayList团队=新建ArrayList();
团队新团队=新团队(名称,1);
System.out.println(“欢迎使用高级运动球跟踪器!”);
while(!name.equals(“x”)//循环打印语句
{//x循环开始
System.out.println(“哪支球队刚刚获胜?(x退出)”;
匹配=0;
循环++;
name=scnr.next();
对于(团队列表:团队)
{
if(list.getName().equals(name))//将团队名称与输入值进行比较
{
match++;
}
}
如果(匹配==0)
{
团队。添加(新团队);
}
}//x循环结束
System.out.print(newTeam.getName());
if(cycles==1)//如果用户立即退出,则不打印数据
{
System.out.println(“无数据输入”);
}
如果(周期>1)
{
System.out.println(“最终计数:”;//循环以打印最终计数
for(团队列表:团队)//FIXME
{
list.toString(list.getName(),list.getWinCount());//用字符串和团队的wincount生成一个字符串
}
他们为您提供的toString方法的原始代码是什么?您添加了参数吗?更好的方法是让对象在方法内部使用其数据字段。在for循环中传递成员变量是不必要的,只是错误的代码。相反,您希望执行以下操作:
public String toString() {
return name + ": " + wins;
}
在循环中,如果要打印结果,只需执行以下操作:
System.out.print(list.toString());
首先修复您给团队的
公共字符串toString()
方法。它需要匹配对象的toString()的方法签名方法--意味着它不应该有任何参数,这是有意义的,因为当前方法的参数完全不必要且有害。您指的是哪个实例?周期的if语句中的实例还是我提供的方法中的实例?哪个实例?团队类中有注释的方法,“这是您将要修复的方法”。修复它。我已经修复了语句,但忘了从实例中省略该语句。很抱歉造成混淆。该语句最初只返回“”并接收到字符串参数。同样,有效的toString()
方法不应接收任何参数。无。请阅读