【问题标题】:If else statement [closed]If else 语句[关闭]
【发布时间】:2017-09-05 05:10:12
【问题描述】:

想要使用此代码搜索 1950-2050 之间的日期,并找出什么时候什么都没有发生、世界杯、奥运会或您写的信息与任何日期都不匹配。

所以 OL 出现在 1950-2050 之间的数字除以 4 时。WC 出现在 OL 的偶数年之间,如 2002(2004 WC)2006。

然后我们有 1950-2050 年间没有发生 OL/WC 的日期,那一年什么也没发生。最后,如果你输入 700,它应该只说最后一个。

Scanner input = new Scanner(System.in);

System.out.println("Write year between 1950-2050: ");
int keyboard = input.nextInt();
int OL = (keyboard);
int WC = (keyboard);
int nothingspec = (keyboard);
int instru = (keyboard);

if(nothingspec) {

 System.out.println("This year nothing special happened.");

}

else if(OL) { 

 System.out.println("Yes this year it the olympic games. ");

}      

else if(WC) { 

    System.out.println("Yes this year it was a world cup in soccer.");

}     

else(instru) {

    System.out.println("Your instructions were wrong please try again.");
}

input.close();

【问题讨论】:

  • 问题是什么,你需要什么????????????????????
  • 在当前状态下您的代码无法编译。
  • 那个标题也无济于事。说真的,有什么问题?
  • 您遇到错误了吗?你能给我们看看吗?
  • if 语句正在寻找布尔值。您将值设置为整数。

标签: java if-statement


【解决方案1】:

据我所知应该是这样的

         System.out.println("Write year between 1950-2050: ");
         int keyboard = input.nextInt();
         int OL = (keyboard);
         int WC = (keyboard);
         int nothingspec = (keyboard);
         int instru = (keyboard);
         boolean blOL = false;
         boolean blWC = false;
         //this occurs whenever the number can be divided by 4
         if(keyboard>=1950&&keyboard<=2050){
            if(OL%4==0) { 

             System.out.println("Yes this year it the olympic games. ");
            blOL=true;
            }      
            //This will happen every time the date can be divided to 2 so as               you said 2002, 2004, 2006 and so on. 
            else if(WC%2==0) { 

                System.out.println("Yes this year it was a world cup in   soccer.");
            blWC = true;
            }
            //This is when nothing has happend.     
            else if(blOL==false && blWC==false) {

             System.out.println("This year nothing special happened.");

            }
            else{

                System.out.println("Your instructions were wrong please try again.");
            }
          }
          else{

                System.out.println("Your instructions were wrong please try again.");
            }


    input.close();

请试试这个,告诉我这是不是你需要的。

【讨论】:

  • 非常感谢,这很完美。
  • 太好了!很高兴我能帮忙