如何制作多个if语句

mam*_*tah 2 java processing if-statement

我正在使用java模式进行处理,如果我按照这样的正常方式制作条件,我想要超过32

If (-------- == --------){//dosomething}

Else If (-------- == --------){//dosomething}

Else If (-------- == --------){//dosomething}

Else If (-------- == --------){//dosomething}

Else If (-------- == --------){//dosomething}

Else {//dosomething}
Run Code Online (Sandbox Code Playgroud)

但只有第一个条件有效,其余条件不起作用有时它会给我错误"Eof"

我也试过这种风格

If (-------- == --------){//dosomething}

Else {//dosomething} 

If (-------- == --------){//dosomething}
Else {//dosomething} 


If (-------- == --------){//dosomething}
Else {//dosomething} 
Run Code Online (Sandbox Code Playgroud)

它也不起作用只有第一个if条件工作

注意有一次gui没有显示按钮或标签

我怎么能回答这个问题,如果有条件我怎么写多少

这是一个例子,y1和y的值来自其他设备并且在我按下硬件按钮时改变1和0这个代码不完整但只有概念而且从y1到y20以及从x1到x20只有第一个条件这工作,我尝试所有的矩形

这是完整代码问题的更新

**在这段代码中我使用了17个按钮,如果按下它们就会读取它们,矩形颜色将是黑色,如果没有按下,矩形将是白色,它效果很好,但只有9个输入,但其余的17没有用,请注意我检查了所有输入和所有连接,它们运行良好,我怎么能解决这个问题**

import processing.serial.*;
import cc.arduino.*;

Arduino arduino;
int x1 = 255;
int x2 = 255;
int x3 = 255;
int x4 = 255;
int x5 = 255;
int x6 = 255;
int x7 = 255;
int x8 = 255;
int x9 = 255;
int x10 = 255;
int x11= 255;
int x12 = 255;

void setup()
{
  size(780, 600);

  //println(Arduino.list());

  arduino = new Arduino(this, Arduino.list()[0], 57600);

    arduino.pinMode(13, Arduino.INPUT);
    arduino.pinMode(12, Arduino.INPUT);
    arduino.pinMode(11, Arduino.INPUT);
    arduino.pinMode(10, Arduino.INPUT);
    arduino.pinMode(9, Arduino.INPUT);
    arduino.pinMode(8, Arduino.INPUT);
    arduino.pinMode(7, Arduino.INPUT);
    arduino.pinMode(6, Arduino.INPUT);
    arduino.pinMode(5, Arduino.INPUT);
    arduino.pinMode(4, Arduino.INPUT);
    arduino.pinMode(3, Arduino.INPUT);
    arduino.pinMode(2, Arduino.INPUT);

}


void draw()

{ 

  roundRect(10, 10, 150, 90, x1);
  roundRect(180, 10, 150, 90, x2);
  roundRect(350, 10, 150, 90, x3);
  roundRect(520, 10, 150, 90, x4);

  roundRect(10, 120, 150, 90, x5);
  roundRect(180, 120, 150, 90, x6);
  roundRect(350, 120, 150, 90, x7);
  roundRect(520, 120, 150, 90, x8);

  roundRect(10, 230, 150, 90, x9);
  roundRect(180, 230, 150, 90, x10);
  roundRect(350, 230, 150, 90, x11);
  roundRect(520, 230, 150, 90, x12);


  ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(13) == Arduino.HIGH ) {
    x1=0;
  }                                                                                                       
  else if (arduino.digitalRead(13) == Arduino.LOW) {
    x1=255;
  }
  /////////////////////////////////////////////////////////////

  ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(12) == Arduino.HIGH ) {
    x2=0;
  }
  else if (arduino.digitalRead(12) == Arduino.LOW) {
    x2=255;
  }
  /////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(11) == Arduino.HIGH ) {
    x3=0;
  }
  else if (arduino.digitalRead(11) == Arduino.LOW) {
    x3=255;
  }
  ///////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(10) == Arduino.HIGH ) {
    x4=0;
  }                                                                                                         
  else if (arduino.digitalRead(10) == Arduino.LOW) {
    x4=255;
  }
  ///////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(9) == Arduino.HIGH ) {
    x5=0;
  }
  else if (arduino.digitalRead(9) == Arduino.LOW) {
    x5=255;
  }
  //////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(8) == Arduino.HIGH ) {
    x6=0;
  }
  else if (arduino.digitalRead(8) == Arduino.LOW) {
    x6=255;
  }
  ///////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(7) == Arduino.HIGH ) {
    x7=0;
  }
  else if (arduino.digitalRead(7) == Arduino.LOW) {
    x7=255;
  }
  ////////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(6) == Arduino.HIGH ) {
    x8=0;
  }
  else if (arduino.digitalRead(6) == Arduino.LOW) {
    x8=255;
  }
  /////////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(5) == Arduino.HIGH ) {
    x9=0;
  }
  else if (arduino.digitalRead(5) == Arduino.LOW) {
    x9=255;
  }

  ////////////////////////////////////////////////////////////////

  ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(4) == Arduino.HIGH ) {
    x10=0;
  }
  else if (arduino.digitalRead(4) == Arduino.LOW) {
    x10=255;
  }
  ///////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(3) == Arduino.HIGH ) {
    x11=0;
  }
  else if (arduino.digitalRead(3) == Arduino.LOW) {
    x11=255;
  }
  //////////////////////////////////////////////////////////////

  ///////////////////////////////////////////////////////////////////////

  if (arduino.digitalRead(2) == Arduino.HIGH ) {
    x12=0;
  }
  else if (arduino.digitalRead(2) == Arduino.HIGH) {
    x12=255;
  }
  //////////////////////////////////////////////////////////////
}

void roundRect(float x, float y, float w, float h, float f) {

    float corner = w/10.0;
    float midDisp = w/20.0;

    fill(f);

    beginShape();  

    curveVertex(x+corner, y);
    curveVertex(x+w-corner, y);
    curveVertex(x+w+midDisp, y+h/2.0);

    curveVertex(x+w-corner, y+h);
    curveVertex(x+corner, y+h);
    curveVertex(x-midDisp, y+h/2.0);

    curveVertex(x+corner, y);
    curveVertex(x+w-corner, y);
    curveVertex(x+w+midDisp, y+h/2.0);

    endShape();

  } 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Pet*_*rey 10

当你写作

if (-------- == --------){//dosomething};
Run Code Online (Sandbox Code Playgroud)

这是整个声明.这;意味着你不能放置else它.

相反,你应该写

if (-------- == --------) {
    //dosomething
} else if (-------- == --------) {
    //dosomething else
}
Run Code Online (Sandbox Code Playgroud)

如果第一个条件是唯一被调用的条件,则意味着它始终为真.

顺便说一句如果你有这些链的长链,很可能有更好的方法来做这个,比如使用switch或多态,这取决于你想要做什么.