gpt4 book ai didi

java - 帮助使用 process 方法查找字符串的长度

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

这是我的歌曲应用程序

/* 
*
*/

public class SongApp{
public static void main(String[] args){
Song song1 = new Song();
song1.setsongLine("While my guitar gently weeps.");
song1.display();
song1.process();
Song song2 = new Song();
song2.setsongLine("Let it be");
song2.display();
}
}

这是我的支持类(class)

/* 
*
*/

public class Song{
// data field declaration
private String songLine;


/*sets the value of the data field songLine to input parameter value*/
public void setsongLine(String songLine){
this.songLine = songLine;
} //end method

/*returns the value of the data field songLine */
public String getsongLine(){
return songLine;
} //end method


//method called process
public String process(){
int stringLength = songLine.length();
}
/*displays formatted songLine information to the console window */
public void display(){
System.out.println(songLine);
System.out.println("Length is :" + process());
}
}

所以我的问题是使用处理方法,我需要打印出 SongLine 的长度,然后生成输出,例如长度为:9。但我的处理方法到目前为止似乎不起作用

最佳答案

您需要从 process() 方法返回一个String

public static void main(String[] args){
...
System.out.println(song1.process());
...
}
public String process(){
return "Length is " + songLine.length();
}

...或者您可以将其设为无效:

public void process(){
System.out.println("Length is " + songLine.length());
}

关于java - 帮助使用 process 方法查找字符串的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7091879/

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