【问题标题】:Using variables from two different while loops使用来自两个不同 while 循环的变量
【发布时间】:2013-10-22 09:45:08
【问题描述】:

如何使用来自不同 while 循环的变量并将它们插入到打印语句中?

 public class Squares{
   public static void main (String [] args){
      int counterA = 0;
      int counterB= 0;

      while (counterA<51){
           counterA++;
           if (counterA % 5 == 0){
                int one = (counterA*counterA);
           }               
      }
      while (counterB<101){
           counterB++;
           if (counterB % 2 == 0){
                int two = (counterB*counterB);         
           }    
      }
       System.out.println(one+two);
   }
 }

【问题讨论】:

  • 当我应该得到几行时,我只得到了一行
  • 你到底想做什么?发表您的问题陈述?
  • 您的打印语句只会执行一次。考虑将其移动到循环中。
  • 如果你想要多行,你需要把你的 println 调用放在一个循环中。
  • 请让问题陈述更清楚...

标签: java variables loops while-loop scoping


【解决方案1】:

我想这就是你的答案

public class Squares{
 public static void main (String [] args){
  int counterA = 0;
  int counterB= 0;

  while (counterA<101){
       counterA++;
       int one,two;
       if (counterA % 5 == 0){
            one = (counterA*counterA);
       }               
        if (counterA % 2 == 0){
            two = counterA * counterA;
        }
        System.out.println(ont + two);
  }
 }
}

【讨论】:

    【解决方案2】:

    在循环外声明变量,并在循环内为它们赋值!

    【讨论】:

      【解决方案3】:

      你需要在循环外声明局部变量一和二

      public class Squares{
         public static void main (String [] args){
            int counterA = 0;
            int counterB= 0;
            int one=0;
            int two=0;  
      
            while (counterA<51){
                 counterA++;
                 if (counterA % 5 == 0){
                      one = (counterA*counterA);
                 }               
            }
            while (counterB<101){
                 counterB++;
                 if (counterB % 2 == 0){
                      two = (counterB*counterB);         
                 }    
            }
             System.out.println(one+two);
         }
       }
      

      【讨论】:

        【解决方案4】:

        这是相当广泛的,因为有很多方法可以做到这一点。您只需将循环内的结果收集到一个全局变量中。如果你想专门制作一个字符串,那么你可以使用StringBuilder之类的东西。

        这是一个数字之间没有空格的例子:

        StringBuilder sb = new StringBuilder();
        int counterA = 0;
        int counterB = 0;
        
        while (counterA < 51) {
          counterA++;
          if (counterA % 5 == 0){
            sb.append(counterA * counterA);
          }               
        }
        while (counterB<101) {
          counterB++;
          if (counterB % 2 == 0) {
            sb.append(counterB * counterB);         
          }    
        }
        System.out.println(sb.toString());
        

        还可以将变量放入数组等中:

        ArrayList<Integer> list = new ArrayList<Integer>();
        while (counterA < 51) {
          counterA++;
          if (counterA % 5 == 0){
            list.add(counterA * counterA);
          }               
        }
        

        【讨论】:

          猜你喜欢
          • 2017-11-29
          • 2012-02-14
          • 2018-04-14
          • 2014-03-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-14
          • 1970-01-01
          相关资源
          最近更新 更多