2016-10-14 5 views
1

Klasse:If-else-if: Es gibt mir immer das gleiche Ergebnis

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; 
    } 
} 

Haupt:

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

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

Das Problem ist, dass alles, was Zahl, die ich wählen, geben Sie mir als Antwort immer rot .

Ich habe auch versucht, indem sie die drei Bedingungen in einer Bedingung setzen, aber diese Zeit gab es mir nur schwarz. Ich weiß nicht, was das Problem ist. Ich habe meine Variable in int gestartet und sie dann doppelt geschaltet, damit% funktionieren kann.

+1

Solange Sie Eingabe eine Zahl zwischen 1 und 10, wird der Ausgang "rot", weil ... es tut, was Sie sagte es zu tun. – shmosel

Antwort

0

Ihre if-Anweisung ist eine Art ein Chaos:

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

wenn Sie Farbe statt color0 zu setzen waren:

if (color==0){ 

Wird nie wahr sein, da obige Bedingung nur wahr ist, wenn Farbe ist zwischen 1 und 10

auch dies:

else if (color > 0) 

wird immer wahr Ursache Farbe ist 1 bis 10

Nicht sicher, warum Sie color0 statt Farbe setzen.

+0

Wenn es "Farbe" wäre, wäre es immer falsch. – shmosel

+0

@Anton Kim, Ihre Aussage über 'color0' ist falsch. Sie verwechseln die Variable "color" mit "color0" - sie sind nicht die gleiche Variable. –

+0

@sa_leinad ja, habe ein paar Änderungen vorgenommen –

1

Ich glaube, Sie haben etwas falsch hier

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

es Änderung

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

oder

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

in der Haupt, Sie col2 ko = new col2(gg); init es setzen nur die color Variable und Stellen Sie color0 Variable nicht ein. Der Ausgang ist immer "rot", weil color0 immer 0 (Standardwert von double) ist.

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. 

Klasse

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()); 
} 
Verwandte Themen