4 Replies - 5398 Views - Last Post: 13 October 2012 - 05:14 AM Rate Topic: -----

#1 zerocool18  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 139
  • Joined: 12-October 12

loop problem to find min and max

Posted 13 October 2012 - 03:23 AM

been stuc on 2 questions for the past 3 days now. getting kinda frustrating as im really starting to beat myself up when i cant figure out a java program :surrender: . but im glad i found this forum , as you guy shave been helping me out immensely and i really do appreciate it. i love that feeling when you finally get the code working lol. anyhow , i have this homework problem that asks me

1. Prompt the user to enter a series of positive integers until they enter -1.
2. After all of the integers have been entered, display the min and max values of all the integers to the user.
NOTE: -1 should never be considered as a min or max

can only use up to what ive learned in class so no arrays.
now the theory was, when the user inputs a number like 5 , that number becomes the max and the min. then when the next number comes in , you compare the 2 numbers and assign the value of max or min to the highest and lowest number right? been trying to figure out how to make that happen all night lol, i got the code as that i pasted at the bottom. hopefully , u can point me in the right direction cause i really dont know what to do and i know my code is probably way off lol.

   	
			int number;
			int max=0;
			int min=0;
			int counter = 0;
			System.out.println("Enter an integer ( press -1 to quit)");
			
	
	do {
		System.out.print("Enter a positive integer (enter -1 to end): ");
		      number = input.nextInt();

			 if (number> max ){
			     number = max;	
			 }
			  else if (number<max ) {
			         number = min;
			   }
			}
			while ( number !=-1);
			System.out.println("max value is " +max);
			System.out.println("min value is "+min);
	}
}	

[/code=]

sorry about that , the code came out all wrong. is there an edit button? lol cant seem to find it


[code]
import java.util.Scanner;

   public class MinMax {
       public static void main(String [] args) {
	         // creates the scanner
            Scanner input = new Scanner(System.in);
		
			int number;
			int max=0;
			int min=0;
			int counter = 0;

	do {
		System.out.print("Enter a positive integer (enter -1 to end): ");
		      number = input.nextInt();

			 if (number> max ){
			     number = max;	
			 }
			  else if (number<max ) {
			         number = min;
			   }
			}
			while ( number !=-1);
			System.out.println("max value is " +max);
			System.out.println("min value is "+min);
	}
}	



sorry about that , the code came out all wrong. is there an edit button? lol cant seem to find it. updated teh code below


import java.util.Scanner;

   public class MinMax {
       public static void main(String [] args) {
	         // creates the scanner
            Scanner input = new Scanner(System.in);
		
			int number;
			int max=0;
			int min=0;
			int counter = 0;

	do {
		System.out.print("Enter a positive integer (enter -1 to end): ");
		      number = input.nextInt();

			 if (number> max ){
			     number = max;	
			 }
			  else if (number<max ) {
			         number = min;
			   }
			}
			while ( number !=-1);
			System.out.println("max value is " +max);
			System.out.println("min value is "+min);
	}
}	



Is This A Good Question/Topic? 0
  • +

Replies To: loop problem to find min and max

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: loop problem to find min and max

Posted 13 October 2012 - 04:19 AM

A do/while loop may not be the best loop because it will require more steps (or filters) in the loop to ignore the -1 quit signal. Can you use a while loop?

If you can, the major difference will be that you'll get the user's first input before the while loop begins and then each subsequent input will be at the end of the while loop:
// get the user's first number
while ( number != -1 )
{
    // test for min and max
    // get user's next number
}

I would modify your strategy for determining min and max a little. First, initialize max to 0 (as you've done) and min to a large number. (If you were also considering the smallest negative values, you'd initialize max to the biggest negative integer.) There's a nice built in Integer constant for the maximum integer, Integer.MAX_VALUE. (Same for the largest negative number, Integer.MIN_VALUE.)

Then, your max/min assignments in the loop are backwards. To "record" the new min and max values, you'll use statements like:
if ( number < min )
{
    min = number;
}

For example, if min was 5 and the user entered 1, then 1 is less than 5 and should become the new min value. The assignment min = number does that. You have to make an equivalent change for you max logic.

That same backwardsness is why your program would never end. Even though the user might enter -1, your logic would change the value of number to 0 since min and max would never change from their initialized values.

Hope this helps. Come back if you need more help.

This post has been edited by GregBrannon: 13 October 2012 - 04:21 AM

Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,083
  • Joined: 05-April 11

Re: loop problem to find min and max

Posted 13 October 2012 - 04:39 AM

You need to set max and min to the first number entered by the user
Right now the default max and min will be 0 and not the first number entered by the user
If the user's first input is 5, then max will be set to 5, but it will not even check if it should set min to 5

//Get number from user

//If number is -1, terminate

//Set max and min to number

while ((number = input.nextInt()) != -1) {
	//Do stuff
}


Was This Post Helpful? 0
  • +
  • -

#4 zerocool18  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 139
  • Joined: 12-October 12

Re: loop problem to find min and max

Posted 13 October 2012 - 04:52 AM

thanks alot! im gonna try out your guy's suggestions!! lol now its starting to get exciting!! illg et back to your guys and let you know how it goes!!
Was This Post Helpful? 0
  • +
  • -

#5 zerocool18  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 139
  • Joined: 12-October 12

Re: loop problem to find min and max

Posted 13 October 2012 - 05:14 AM

wooot!! :bananaman: got it working!! much thanks guys. i dont know why its so hard for me to see things right off the bat like that. ive been sitting here trying this problem forever, and i never would have thought that the do while i was using was making it harder than it is or to make the max and mix equal to the number. much appreciated


import java.util.Scanner;

   public class MinMax {
       public static void main(String [] args) {
	         // creates the scanner
            Scanner input = new Scanner(System.in);
	
			System.out.println("Enter an integer ( press -1 to quit)");
			int number = input.nextInt();
			int min=number;
			int max=number;
          		
			while (number != -1){
			  if ( number > max) {
			     max = number;
				 }
			else if ( number < min) {
			    min = number;
			     } 	 
			  System.out.println("Enter an integer ( press -1 to quit)");
			    number = input.nextInt();
			 }
			  System.out.println("The max is: "+max);
              System.out.println("the min is: "+min);			  
	            
	}
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1