【问题标题】:Java program that takes an input a color code in HSV and outputs the equivalent RGB code and Vice versaJava 程序,输入 HSV 中的颜色代码并输出等效的 RGB 代码,反之亦然
【发布时间】:2014-12-02 13:59:24
【问题描述】:

我正在尝试编写从颜色 rgb 转换为 hsv 的代码,但编译器似乎看不到 if 条件中的内容 这是代码 包问题2;

public class HSVRGB {

    double d;
    double e;
    double g;

 //Java program that takes an input a color code in HSV and

输出等效的 RGB 代码,反之亦然。

public static void convert(double d,double e,double g){ /// my hsv inputs that am gonna check

    double t;
    double p ;
    double q;
    double f;
    double Hi;
    //my other variable am gonna use to produce the output

    Hi=(d/60);
    f=(d/60)-Hi;// here's the formula that will proceed the code




    if(Hi==0) {
        t=g*(1-(1-f)*e);
        p=g*(1-e);
        System.out.print("R="+ g + "G=" + t + "b=" + p );
        // the given input should pass into the above if condition since its true



    }
    else 
        if(Hi==1){
            q=g*(1-f*e);
            p=1-e;
            System.out.print("R="+q+"G="+g+"b="+p);



        }
        else
            if(Hi==2){
                p=g*(1-e);
                t=g*(1-(1-f)*e);
                System.out.print("R="+p + "G="+ g + "B="+t);
            }
            else 
                if(Hi==3){
                    p=g*(1-e);
                    q=g*(1-(f*e));
                    System.out.print("R="+p + "G="+q + "B=" + g );


                }
                else
                    if(Hi==4){
                        t=g*(1-(1-f)*e);
                        p=g*(1-e);
                        System.out.print("R"+t+"G"+p+"B"+g);

                    }
                    else 
                        if(Hi==5){
                            p=g*(1-e);
                            q=g*(1-(f*e));
                            System.out.print("R="+g+ " G"+p+"B="+q );


                        }
                        else
                            System.out.print("Invalid Inpjdsnabfvkdbfjsv,hdbut"); //it only print this message


}


public static void main (String[]args){
    convert(100.34,0.74,0.78);

   }
}

【问题讨论】:

  • 为什么不将Color.getHSBColor() 与 RGB 组件一起使用?

标签: java if-statement nested conditional-statements


【解决方案1】:

这里

convert(100.34,0.74,0.78);

之后变量d 的值将是100.34

Hi=(d/60);

变量Hi 的值将是1.6723333333333334

在你的 if 语句中,例如

else 
        if(Hi==1){

你认为变量Hi 的值1.6723333333333334等于1 吗?

建议:我真的不知道您为什么尝试将 double 与 int 进行比较,但您可以进行强制转换或尝试比较 this 和 that 之间的范围

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-19
    • 2023-03-19
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 2018-11-07
    • 1970-01-01
    相关资源
    最近更新 更多