gpt4 book ai didi

java - 在 Main 中调用字符串方法

转载 作者:行者123 更新时间:2023-12-02 03:04:29 24 4
gpt4 key购买 nike

我只是想调用一个返回字符串的简单方法,由于某种原因,我一片空白,无法记住如何正确执行此操作。我正在尝试调用 Bands 类中的方法 getInfo() 并打印出其中的字符串 bandInfo:

public class App {


public static void main(String[] args) {

Bands[] bands = new Bands[5];

bands[0] = new Bands("Joe", "Rick", "Nick", "Dalton", "Doylestown, PA", "RockOn", 4000.50 , "Rock");
bands[1] = new Bands("Luke", "Bill", "Ian", "Matt", "State College, PA", "Blink182", 3500.50 , "Alternative");
bands[2] = new Bands("Corey", "Noah", "Jon", "Kaleb", "Philadelphia, PA", "Rise Against", 10000.50 , "Rock");
bands[3] = new Bands("Jake", "Joey", "Mike", "Mac", "New York, NY", "Thousand Foot Krutch", 2000.50 , "Rock");
bands[4] = new Bands("Bob", "Jeff", "Dom", "Mark", "King of Prussia, PA", "Skillet", 5500.50 , "Rock");

bands[0].compete();
bands[1].compete();
bands[2].compete();
bands[3].compete();
bands[4].compete();

for (int i = 0; i < 5; i++) {
String bandInfo = getInfo(bandInfo);
}
}
}

这是我的 Bands 类代码:

import java.util.Random;

public class Bands {

private String singerName;
private String guitarName;
private String bassistName;
private String drummerName;
private String Hometown;
private String bandName;
private double income;
private String genre;
private int score;

public Bands(String singerName, String guitarName, String bassistName, String drummerName, String Hometown, String bandName, double income, String genre)
{
this.singerName = singerName;
this.guitarName = guitarName;
this.bassistName = bassistName;
this.drummerName = drummerName;
this.bandName = bandName;
this.Hometown = Hometown;
this.income = income;
this.genre = genre;
this.score = -1;
}

public void compete()
{
Random rand = new Random();
this.score = rand.nextInt(20);

}

public String getInfo()
{
String bandInfo = "Band: " + this.bandName + ", Singer: " + this.singerName + ", Guitarist: " + this.guitarName + ", Bassist: " + this.bassistName +
", Drummer: " + this.drummerName + ", Hometown: " + this.Hometown + ", Income: " + this.income + ", Genre: " +
this.genre + ", Final Score: " + this.score;

return bandInfo;
}
}

最佳答案

for (int i = 0; i < 5; i++) {
String bandInfo = bands[i].getInfo();
}

getInfo() 是 Bands 类的实例方法。因此您必须使用 Bands 的实例来调用该方法。

bands[i] 指的是 Bands 对象。

关于java - 在 Main 中调用字符串方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41944375/

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