简体   繁体   English

在 do while 循环中我的 if else 语句出现问题

[英]Issue with my if else statement in a do while loop

My do-while loop is supposed to print out numbers from 1 - 106 (including 1 and 106)我的 do-while 循环应该打印出 1 - 106 的数字(包括 1 和 106)

whats supposed to happen: multiples of 3 are supposed to print out Big, multiples of 5 are supposed to print out Mean, multiples of 7 are supposed to print out Bugs, multiples of 3 and 5 are supposed to print out BigMean, multiples of 3 and 7 are supposed to print out BigBugs, multiples of 5 and 7 are supposed to print out MeanBugs, multiples of 3, 5 and 7 are supposed to print out BigMeanBugs.应该发生什么: 3 的倍数应该打印出 Big,5 的倍数应该打印出 Mean,7 的倍数应该打印出 Bugs,3 的倍数和 5 应该打印出 BigMean,3 的倍数和 7 应该打印出 BigBugs,5 和 7 的倍数应该打印出 MeanBugs,3、5 和 7 的倍数应该打印出 BigMeanBugs。

What actually happens: my do-while loop only runs the first "if" statement.实际发生了什么:我的 do-while 循环只运行第一个“if”语句。 Any help?有什么帮助吗?

My code:我的代码:

public static void main(String [] args){
      
    int i = 0;      
    do{
        ++i;
        if(i %3 == 0){
            System.out.print("Big, ");
             
            if(i %5 ==0){
                System.out.print("Mean, ");
            }
            if(i %7 ==0){
                System.out.print("Bugs, "); 
            }
            if((i %3 == 0) && (i %5 == 0)){
                System.out.print("BigMean, ");
            }
            if((i %3 == 0) && (i %7 == 0)){
                System.out.print("BigBugs, ");
            }
               
        }else if((i %5 == 0) && (i %7 == 0)){
            System.out.print("MeanBugs, ");
               
        }else if((i %3 == 0) && (i %5 == 0) && (i %7 ==0)){
            System.out.print("BigMeanBugs, ");
                     
        }else{
            System.out.print(i + " ");
        }               
    }while(i<=106);   
            
}//closing main

As I could see your problem is probably that your if-statements are inside of first if-statement , so maybe you want this:正如我所看到的,您的问题可能是您的if-statements在第一个if-statement内,所以也许您想要这个:

public class BigMeanBugsDoWhileLoop
{
  public static void main(String [] args)
  {
  
    int i = 0;  
    
    do
    {
        ++i;
        boolean bug = false;
        String result = "";

        if(i %3 == 0)
        {
            result += "Big";
            bug = true;
        }
        
        if(i %5 ==0)
        {
            result += "Mean";
            bug = true;
        }
        
        if(i %7 ==0)
        {
            result += "Bugs";
            bug = true;
        }
        
        if(!bug)
        {
           System.out.println(i + "");
        }
        else
        {
           System.out.println(result);
        }
    }while(i<106);   
        
  }//closing main
 
} // closing class

You don't need other if-statements because you don't use break so other expressions will be evaluated before next iteration.您不需要其他if 语句,因为您不使用break ,因此将在下一次迭代之前评估其他表达式。

  • First, if something is divisible by 3 and 7 it is divisible by 21.首先,如果一个东西能被 3 和 7 整除,它就能被 21 整除。
  • Second, if you reverse the order, you will catch divisible by 3 and 5 before divisible by 3 or 5 separately (assuming that is what you want).其次,如果您颠倒顺序,您将在分别被 3 或 5 整除之前捕获可被 3 和 5 整除(假设这是您想要的)。 If you do it the other way you may print the values prematurely or miss them altogether before checking the other possible factors.如果您以其他方式执行此操作,您可能会在检查其他可能因素之前过早打印这些值或完全错过它们。 This behavior is also dependent on if vs if/else if statements.此行为还取决于ifif/else if语句。

I have limited the printout to 25 to avoid a long list.我已将打印输出限制为 25 以避免长列表。 Modify as you see fit.修改你认为合适的。

public class BigMeanBugsDoWhileLoop {
    public static void main(String[] args) {
        int i = 1;
        do {
            String bugType = "";
            if (i % 105 == 0) {
                bugType = "BigMeanBugs";  // 3 5 7
            } else if (i % 35 == 0) {
                bugType = "MeanBugs";     // 5 7 
            } else if (i % 21 == 0) {   
                bugType = "BigBugs";      // 3 7
            } else if (i % 15 == 0) {
                bugType = "BigMean";      // 3 5
            } else if (i % 7 == 0) {       
                bugType = "Bugs";         // 7
            } else if (i % 5 == 0) {
                bugType = "Mean";         // 5
            } else if (i % 3 == 0) {
                bugType = "Big";          // 3
            }
            System.out.println(bugType.isEmpty() ? i : bugType);
            i++;
        } while (i <= 25);
        
    }// closing main
    
} // closing class

Prints this.打印这个。

1
2
Big
4
Mean
Big
Bugs
8
Big
Mean
11
Big
13
Bugs
BigMean
16
17
Big
19
Mean
BigBugs
22
23
Big
Mean

You have to increment the value of i in the end of loop您必须在循环结束时增加 i 的值

public static void main(String [] args){
  
     int i = 1;
     
     do{   
        
        if(i %3 == 0){
          System.out.print("Big, ");
         
           if(i %5 ==0){
             System.out.print("Mean, ");
           }
              if(i %7 ==0){
                System.out.print("Bugs, "); 
              }
                 if((i %3 == 0) && (i %5 == 0)){
                   System.out.print("BigMean, ");
                 }
                    if((i %3 == 0) && (i %7 == 0)){
                      System.out.print("BigBugs, ");
                    }
           
                    }else if((i %5 == 0) && (i %7 == 0)){
                      System.out.print("MeanBugs, ");
           
                 }else if((i %3 == 0) && (i %5 == 0) && (i %7 ==0)){
                   System.out.print("BigMeanBugs, ");
                 
           }else{
             System.out.print(i + " ");
           }
           i++;
        }while(i<=106);

        
     }//closing main
 
  } // closing class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM