If-else-if:它总是给我相同的结果

时间:2016-10-14 01:30:31

标签: java

类别:

package work;

public class col2 {

    private double color;
    private double colorO;

    public col2(int c){
        color = c;
    }

    public void setColor(int c){
        color = c;
        calcN();
    }

    public void calcN(){
        colorO = (double) color%2;
    }

    public void color0(int c){
        color = c;
    }

    public String getcolor(){
        String coco = "";

        if(color == 0){
            coco = "green";
        }
        else if (color>=1 && color<=10){
            if (colorO==0){
                coco = "red";
            }
            else if (colorO>0){
                coco = "black";
            }
        }

        return coco;
    }
}

主:

Scanner y = new Scanner(System.in);
System.out.println("number");
int gg = y.nextInt();

col2 ko = new col2(gg);
System.out.println(ko.getcolor());

问题在于,无论我选择什么数字,总是会给我一个红色作为答案。

我也试过将3个条件放在一个条件下,但那个时候它只给了我黑色。我不知道问题是什么。我在int中启动了我的变量,然后将它们切换为double,以便%可以工作。

3 个答案:

答案 0 :(得分:1)

我认为你这里有问题

public col2(int c){
    color = c;
}

将其更改为

public col2(int c){
    color = c;
    calcN();
}

public col2(int c){
    setColor(c);
}

在您的主页中,您初始化col2 ko = new col2(gg);它只是设置color变量而不设置color0变量。输出总是&#34;红色&#34;因为color0总是0(默认值为double)。

答案 1 :(得分:0)

你的if语句有点混乱:

 else if (color>=1 && color<=10){
        if (colorO==0){
            coco = "red";
        }

如果您要使用color而不是color0:

if (color==0){

永远不会成立,因为只有当颜色在1到10之间时,上述条件才为真

还有:

else if (color > 0)

将始终为true,因为颜色为1到10

不确定为什么要使用color0而不是颜色。

答案 2 :(得分:0)

Tkx guys.
I know, generally my statements are a mess, my teacher keep saying that my variables don't have proper names.
Anyway, your answers work and that is perfect, but i found the solution sooner since the teacher asked this number. I also changed my logic and i forgot to give some details of the problems. I'm sorry about that.
Once again, tkx a lot.

Here are the codes.

public class opop {




        private double colorA;
        private double colorO;

        public opop(int c){
            colorA = c;
        }

        public void setColor(int c){
            colorA = c;
            //calcN();
        }

    /*  public void calcN(){
            colorO = (double) color%2;
        }

        public void color0(int c){
            color = c;
        }
        */

        public String getcolor(){
            String coco = "";
            double color= colorA%2;
            if(colorA == 0){
                coco = "green";
            }
            else if (colorA>=1 && colorA<=10){
                    if (color==0){
                        coco = "black";

                    }
                    else {
                        coco = "red";
                    }
            }


            else if (colorA>=11  && colorA<=18){
                if (color ==0){
                coco = "red";}
                else{
                    coco = "black";
                }
            }

            else if (colorA>=19 && colorA<=28){
                if(color ==0){
                coco = "black";}
                else {
                    coco = "red";
                }
            }

            else if (colorA>=29 && colorA<=36){
                if (color ==0){
                coco = "red";}
                else {
                    coco = "black";
                }
            }

            else  {
                coco = "Not good";
            }

        return coco;



        }

MAIN

public static void main(String[] args){
Scanner y = new Scanner(System.in);
        System.out.println("number");
        int gg = y.nextInt();


       opop ko = new opop(gg);
       System.out.println(ko.getcolor());
}