涉及字符串比较的Java程序优化

时间:2014-10-01 08:30:26

标签: java optimization comparison

所以,我制作了这个简单的程序,测试两个字符串值(代表两个单独的原色)的差异和类型,以确定所得混合物的颜色。

/**
 * A program that prompts the user to enter the names of two different primary colors to create a mixture.
 * 
 * @author A. Mackey
 * @version 1.0
 */
import java.util.*;
public class ColourMixer {
    public static void main(String [] args) {
        colourMixer();
    }

    public static void colourMixer() {
        Scanner in = new Scanner(System.in);
        String colourOne = "";
        String colourTwo = "";

        System.out.println("You are mixing two different primary colours.");

        System.out.print("Enter your first colour: ");
        colourOne = in.nextLine();

        System.out.print("Enter your second colour: ");
        colourTwo = in.nextLine();

        if((colourOne.equalsIgnoreCase("red") || colourOne.equalsIgnoreCase("blue") ) && (colourTwo.equalsIgnoreCase("blue") || colourTwo.equalsIgnoreCase("red") && !(colourOne.equalsIgnoreCase(colourTwo)))) {
            System.out.println("Your colour combination creates purple!");
        } else if ((colourOne.equalsIgnoreCase("red") || colourOne.equalsIgnoreCase("yellow") ) && (colourTwo.equalsIgnoreCase("yellow") || colourTwo.equalsIgnoreCase("red") && !(colourOne.equalsIgnoreCase(colourTwo)))) {
            System.out.println("Your colour combination creates orange!");
        } else if ((colourOne.equalsIgnoreCase("blue") || colourOne.equalsIgnoreCase("yellow") ) && (colourTwo.equalsIgnoreCase("yellow") || colourTwo.equalsIgnoreCase("blue") && !(colourOne.equalsIgnoreCase(colourTwo)))) {
            System.out.println("Your colour combination creates green!");
        } else
            System.out.println("You have not entered two different primary colours.");
    }
}

它完全按照预期运行,没有可观察到的问题。

然而,它的逻辑感觉笨拙和过分。

我很好奇这个算法是否可以改进。

4 个答案:

答案 0 :(得分:2)

也许有些布尔可以删除重复。

boolean isRed = colourOne.equalsIgnoreCase("red") || colourTwo.equalsIgnoreCase("red") ;
boolean isBlue =  colourOne.equalsIgnoreCase("blue") || colourTwo.equalsIgnoreCase("blue") ;

if (isRed && isBlue){
   // ...
} else if (isRed && isYellow) {
  // ...

答案 1 :(得分:0)

您可以将输入字符串(在我的情况下为AS小写字符串)放入Set中,并且可以调用Sets contains。

如果setContains(红色) - >检查是否包含黄色或蓝色。 现在你可以选择setContains(蓝色)或setContains(黄色),并测试是否存在其他颜色之一,因为通过这样做,颜色的组合无关紧要,所有可能的情况都会被测试。 黄色和蓝色与蓝色和黄色相同。

Set<String> set = new HashSet<String>();
set.add(colourOne.toLowerCase());
set.add(colourTwo.toLowerCase());

if(set.contains("red")){
    if(set.contains("blue")){
        //return purple
    }
    if(set.contains("yellow"){
        //returnorange
    }
}
if(set.contains("yellow")){
    if(set.contains("blue"){
        //return green
    }
}
//unidentified colour

答案 2 :(得分:0)

另一个可用选项是将它们存储在列表中。然后,您可以检查列表中是否有颜色。

例如

String colourOne = "blue";
String colourTwo = "red";

List<String> colours= new ArrayList<String>(2);
colours.add(colourOne);
colours.add(colourTwo);


if((colours.contains("red") && colours.contains("blue")
    System.out.println("Your colour combination creates purple!");
etc

答案 3 :(得分:0)

这看起来像是使用枚举的完美之处。

enum COLOR{
  RED,GREEN,BLUE,BLACK,WHITE; //etc.
}

然后你可以使用一种方法将用户输入字符串转换为颜色:

public static final COLOR convertToColor(final String input){
    try{
        return COLOR.valueOf(input.toUpperCase());
    }catch(IllegalArgumentException e){
       //invalid color, handle it properly
       System.out.println(input + " is not a valid color !! Enter a new valid color this time!!");
       return null;
    }
}

public static void colourMixer() {
    Scanner in = new Scanner(System.in);
    COLOR color1;
    COLOR color2;

    System.out.print("Enter your first colour: ");
    do{
       color1 = convertToColor(in.nextLine());
    }while( color1 == null);

    System.out.print("Enter your second colour: ");
    do{
       color2 = convertToColor(in.nextLine());
    }while( color2 == null);

    if(COLOR.RED.equals(color1) && COLOR.BLUE.equals(color2)){
          System.out.println("Your colour combination creates purple!");
    }else if(COLOR.RED.equals(color1) && COLOR.YELLOW.equals(color2)){
           System.out.println("Your colour combination creates orange!");
    }//....


}