Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to use same input for multiple while loops/cal.? Please help

loop

  • Please log in to reply
3 replies to this topic

#1 thieflock

thieflock

    CC Regular

  • Just Joined
  • PipPipPip
  • 29 posts

Posted 10 November 2007 - 11:20 AM

Basically the user inputs two int values and I need to output the odd's between the two number (inclusive) and also output the sum of the evens (inclusive). I have the first part done with the odds. For the second part I have tried nesting but doesn't work to well. How to I use the same input values for different while loops, so I can calculate the sum of even ints?

P.S. I can only use while loops.


import java.util.Scanner;

public class WhileLoopExercise {

  public static void main(String[] args) {
  
    // declare variables
	int firstNum,
		secondNum,
		evenSum = 0;
	Scanner input;
    
	// prompt user
	System.out.println("Please input two integers");
	input = new Scanner(System.in);
	firstNum = input.nextInt();
	secondNum = input.nextInt();
	
	// output the odd numbr and sum of the sum of the even integers
	if (firstNum % 2 == 0) {
	  firstNum += 1;
	    while (firstNum <= secondNum) {
		  System.out.print(firstNum + " ");
		  firstNum += 2;
		}
		System.out.println();
	} else if (firstNum % 2 != 0) {
	    while (firstNum <= secondNum) {
		  System.out.print(firstNum + " ");
		  firstNum += 2;
	    }
	  System.out.println();
	}
  }
}

  • 0

#2 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 10 November 2007 - 11:27 AM

If I understand correctly the solution is rather simple -- Just assign another name to the input variables. That variable would be considered a "local variable." Thus you can loop with it and manipulate it while the original instance variable remains untouched.



firstNum = input.nextInt();
secondNum = input.nextInt();

x = firstNum;
y = secondNum;

loop 1

x = firstNum;
y = secondNum;

loop 2


Or did I misunderstand?

Also if you want to impress your teacher, you might consider finding a closed form solution to the summation of the integers, rather than a recursive definition. The efficiency is a matter of O(n) vs O(1), implying the closed form solution is n times faster where n is the range of numbers that need to be summed.
  • 0

#3 thieflock

thieflock

    CC Regular

  • Just Joined
  • PipPipPip
  • 29 posts

Posted 10 November 2007 - 11:43 AM

Is there a way to make the input values a constant so that after I use them in the first while block they are then reset back to their constant value for the second while block?
  • 0

#4 thieflock

thieflock

    CC Regular

  • Just Joined
  • PipPipPip
  • 29 posts

Posted 12 November 2007 - 06:26 AM

If anyone wants to know what I did because they may have the same problem. I just simplified the loop by making an if else if block with most of the code inside them, with the counter at the very end.
  • 0





Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download