0

How do I use variables from different while loops and insert them in a print statement?

 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);
   }
 }
7
  • I only get one line when I should be getting several Commented Oct 14, 2013 at 18:33
  • What are you actually trying to do? Post your problem statement? Commented Oct 14, 2013 at 18:33
  • Your print statement will only be executed once. Consider moving it inside a loop. Commented Oct 14, 2013 at 18:33
  • If you want multiple lines, you need to put your println call inside a loop. Commented Oct 14, 2013 at 18:34
  • Please make the problem statement more clearer... Commented Oct 14, 2013 at 18:36

4 Answers 4

2

I think this is your answer

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);
  }
 }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Declare the variables outside your loop, and assign them the values inside the loop!

Comments

0

you need to declare the local variables one and two outside the loops

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);
   }
 }

Comments

0

This is quite broad, as there are many ways to do this. You just need to collect the results from within the loops into a global variable. If you want to specifically make a string, then you can use something like a StringBuilder.

Here is an example with no spacing between numbers:

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());

You can also put the variables into arrays, etc:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.