如何在Java中返回两个或多个字符串?

问题描述 投票:-5回答:1

如何在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());

}
}
java string return return-value
1个回答
0
投票

我认为您应该为“ 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。也许您应该阅读一些基础知识。

© www.soinside.com 2019 - 2024. All rights reserved.