字符串数组上的for循环

时间:2019-02-21 23:06:16

标签: java arrays for-loop

注意:我的if语句:if(newExteriorColor = availableExteriorColors)在这种情况下不应该使用,因为它不正确,所以只想澄清一下我知道它的错误

如何在availableExteriorColors数组上进行for循环,以便程序检查所选颜色是否等于数组中的任何String值?

public class Cars {

    String [] availableExteriorColors = {"Black", "White", "Blue", "Red", "Yellow", "Brown"};

    public static String exteriorColor;
    public static String interiorColor;
    public static String carBrand;
    public static String carModel;
    public static String carPlateLetters;
    public static String carPlateNumber;
    public static int yearOfMake;
    //public static ? vinNumber; 

    public Cars(String newExteriorColor,String newInteriorColor,String newCarBrand,String newCarModel,String newCarPlateLetters,String newCarPlateNumber,int newYearOfMake)  
    {
        if (newExteriorColor = availableExteriorColors)
        {
            newExteriorColor = exteriorColors;
        } 
        else {
            System.out.println("Selected exterior color not available");
     return; }

1 个答案:

答案 0 :(得分:-1)

for (String color : AvailableExteriorColors) {
    if (color.equals(selectedColor)) return color;
}