If-lause



Tämä simulaatio havainnollistaa if-valintalausetta kokonaisluvuilla. Kopioi alla oleva koodi ja aja ohjelma. Älä tee koodiin muutoksia.

// Ohjelman tehnyt e-Oppi Oy
// 30.1.2018
int a = 470; // Laskurin alkuarvo 
int b = 240; // Laskurin alkuarvo 
int c = 95; // Laskurin alkuarvo 

void setup () {
     size(1000,700); // ikkunan koko
     stroke(0); // Musta viivan väri
     textSize(30); // Tekstin koko
}

void draw() {
     background(255); // Tyhjennä tausta
     int a2 = round(map(a,0,950,-10,10));
     int b2 = round(map(b,0,950, -10,10));
     int c2 = round(map(c,0,950,0,5));
     String [] c2a = {">",">=","==","<=","<","!="};
     strokeWeight(1);
     fill(255);
     rect(500,200,500,500);
     stroke(200);
     for (int t=1; t <= 10; t++) {
        line(500+t*50,200,500+t*50,700);
        line(500,200+t*50,1000,200+t*50);
     }
     fill(0);
     textSize(30);
     stroke(0);
     if (c2a[c2] == ">") {
        if (a2 > b2) {
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+100);
        }
     }
     if (c2a[c2] == ">=") {
        if (a2 >= b2) {
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+100);
        }
     } 
     if (c2a[c2] == "==") {
        if (a2 == b2) {
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+100);
        }
     } 
     if (c2a[c2] == "<=") {
        if (a2 <= b2) {
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+100);
        }
     } 
     if (c2a[c2] == "<") {
        if (a2 < b2) {
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+100);
        }
     }
     if (c2a[c2] == "!=") {
        if (a2 != b2) {
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+100);
        }
     } 
      
     fill(240);
     noStroke();
     rect(0,200,500,500);
     rect(0,0,1000,200);
  
     stroke(0);  
     strokeWeight(1);
  
     fill(255,255,0); // Keltainen täyttöväri
     rect(0,0,999,50); // Piirrä keltainen suorakulmio
     rect(0,50,999,50); // Piirrä keltainen suorakulmio
     rect(0,100,999,50); // Piirrä keltainen suorakulmio

     fill(255,0,0); // Punainen täyttöväri
     rect(a,0,50,50); // Piirrä punainen neliö
     rect(b,50,50,50); // Piirrä punainen neliö
     rect(c,100,50,50); // Piirrä punainen neliö
 
     int x1 = mouseX; // Selvitä hiiren vaakakoordinaatti
     int y1 = mouseY;
     if (mousePressed == true) {
      if ((y1 > 0) && (y1 < 50)) {
          if ((x1>a) && (a < 950)) { // Jos hiiri on oikealla puolen
                a++; // niin kasvata laskuria
          }
          if ((x1<a) && (a > 0)) { // Jos hiiri on vasemmalla puolen
                a--; // niin pienennä laskuria
          }
      }
      if ((y1 > 50) && (y1 < 100)) {
          if ((x1>b) && (b < 950)) { // Jos hiiri on oikealla puolen
                b++; // niin kasvata laskuria
          }
          if ((x1<b) && (b > 0)) { // Jos hiiri on vasemmalla puolen
                b--; // niin pienennä laskuria
          }
      }
      if ((y1 > 100) && (y1 < 150)) {
          if ((x1>c) && (c < 950)) { // Jos hiiri on oikealla puolen
                c++; // niin kasvata laskuria
          }
          if ((x1<c) && (c > 0)){ // Jos hiiri on vasemmalla puolen
                c--; // niin pienennä laskuria
          }
      }
     
     }
     fill(0); // Musta tekstin väri
     textSize(20); // Tekstin koko
     text(a2,a+5,35);
     text(b2,b+5,85);
     text(c2a[c2],c+10,135);
      
     textSize(25); // Tekstin koko
     text("void setup () {",10,240);
     text("size(500,500); ",50,270);
     text("textSize(30);",50,300);
     text("background(255);",50,330);
     text("fill(0);",50,360);
     text("}",10,390);
     text("void draw () {",10,420);
     text("int x = "+a2+";",50,450);
     text("if (x "+c2a[c2]+" "+b2+") {",50,480);
     text("text(\"Ehto on tosi\",50,100);",90,510);
     text("} else {",50,540);
     text("text(\"Ehto on epätosi\",50,100);",90,570);
     text("}",50,600);
     text("}",10,630);
     strokeWeight(5);
     line(100,170,940,170);
     triangle(930,157,930,183,948,170);
     for (int r=0; r <= 20; r++) {
       line(100+r*40,165,100+r*40,175);
     }
     for (int r=0; r <= 9; r++) {
       textSize(20);
       text(-10+r,83+r*40,195);
     }
     for (int r=0; r <= 10; r++) {
       textSize(20);
       text(r,493+r*40,195);
     }
     stroke(255,0,0);
     if (c2a[c2] == ">") {
         noFill();
         ellipse(100+40*(10+b2),170,20,20);
         line(100+40*(10+b2),160,900,160);
     }
      if (c2a[c2] == ">=") {
         fill(255,0,255);
         ellipse(100+40*(10+b2),170,20,20);
         line(100+40*(10+b2),160,900,160);
     }
     if (c2a[c2] == "==") {
         fill(255,0,255);
         ellipse(100+40*(10+b2),170,20,20);
     }
     if (c2a[c2] == "<=") {
         fill(255,0,255);
         ellipse(100+40*(10+b2),170,20,20);
         line(100+40*(10+b2),160,100,160);
     }
     if (c2a[c2] == "<") {
         noFill();
         ellipse(100+40*(10+b2),170,20,20);
         line(100+40*(10+b2),160,100,160);
     }
       if (c2a[c2] == "!=") {
         noFill();
         ellipse(100+40*(10+b2),170,20,20);
         line(100,160,900,160);
     }
     stroke(0,0,255);
     line(100+40*(10+a2),155,100+40*(10+a2),175);
     stroke(0);
}