如何在Java中返回两个或多个字符串?我尝试了以下代码,但不知道正确的语法是什么。
import java.util.Scanner;
class cat{
public String description() {
String color;
Scanner sc= new Scanner(System.in);
System.out.println("enter breed: ");
String breed= sc.nextLine();
System.out.println("enter color");
color= sc.nextLine();
System.out.println("color is "+ color);
return new String[] {breed, color};
}
}
public static void main(String[] args)
{
cat cat1= new cat();
System.out.println("breed is: " + cat1.description());
System.out.println("color is: "+ cat1.description());
}
}
我认为您应该为“ cat”类创建String属性。
public class Cat {
private String breed;
private String color;
public Cat(String breed, String color) {
this.breed = breed;
this.color = color;
}
public String getBreed() {
return this.breed;
}
public String getColor() {
return this.color;
}
}
现在您可以在主方法中访问getBreed()和getColor()方法。
似乎您刚开始使用oop。也许您应该阅读一些基础知识。