Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: hello! can i please get some help with a simple loop problem?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default hello! can i please get some help with a simple loop problem?

    hello , im just begining my java programing. forgive me if im displaying my code wrong , but this the only way i could figure it out lol .been trying to do these loop problems in my book but ive gotten stuck on the very first problem , which is suppossed to be easy lol . anyhow this is as far as ive got. im supposed to:
    Write a program that does the following:

    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
    import java.util.Scanner;

    so far i have it so that it asks for the integer ina loop and it ends when the user puts a -1. i just dont understand how to select the max and min values and display them. i can only use loops , as that how far we've gotten in class.

     
     
    import java.util.Scanner;
     
       public class MinMax {
           public static void main(String [] args) {
    	         // creates the scanner
                Scanner input = new Scanner(System.in);
    		int number;
     
     
    	do {
    		System.out.print("Enter a positive integer (enter -1 to end): ");
    		       number = input.nextInt();
     
     
     
    			}
    			while (number !=-1);
    			System.out.println("max value is ");
     
    	}
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: hello! can i please get some help with a simple loop problem?

    Hi and welcome to the forum zerocool18!
    forgive me if im displaying my code wrong , but this the only way i could figure it out
    It is not wrong, but by saying [code=java] instead of just [code] you also get java style highlighting on the code as seen below:

    import java.util.Scanner;
     
    public class MinMax {
     
       /**pretty javadoc color*/
       public static void main(String[] args) {
          // creates the scanner
          Scanner input = new Scanner(System.in);
          int number;
     
          do {
             System.out.print("Enter a positive integer (enter -1 to end): ");
             number = input.nextInt();
          } while(number != -1);
          System.out.println("max value is ");
       }
    }
    Given a list of numbers, how would you keep track of the largest and smallest without the use of a computer? Pretend I am giving you a list of numbers, and you were to use a pencil and paper to do the work. Think about the steps you would take and write them down. Once you have the plan (algorithm) you can start working on how to solve the problem in code.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: hello! can i please get some help with a simple loop problem?

    alright , well i would take the list of numbers and sort them in order from the largest to smallest . the largest would be my max. then id take the numbers again and sort them from smallest to largest and smallest number would be my min. what i dont get is how i would be able to sort the numbers from only 1 input? i was also thinking that maybe my input method might be wrong. I did a triangle problem similar to this , without loops. but in that problem , we had a set number of inputs. the user entered 3 sides of a triangle and we stored each side separately and you could easily sort the values from that. but in this case , theres not a set number of inputs the user could enter. they could enter how many numbers they please. thats whats confusing me.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: hello! can i please get some help with a simple loop problem?

    First number given is 24.
    24 is now the min. 24 is now the max.

    Second number given is 48.
    24 is still the min. 48 is now the max.

    Third number is 12.
    12 is now the min. 48 is still the max.

    Fourth number given is 32.
    12 is still the min. 48 is still the max.

    Fifth number given is 92.
    12 is still the min. 92 is now the max.

    Sixth number given is -1.
    Final results are min = 12 and max = 92.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: hello! can i please get some help with a simple loop problem?

    thanks alot for the help , well see how it goes. it makes since in my head, but sometimes i lose it when i try to put it into code lol . ill let you know how it works out!!

  6. #6
    Junior Member
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: hello! can i please get some help with a simple loop problem?

    well i ended up with this lol. been trying all night , and i no its probably way off.

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

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: hello! can i please get some help with a simple loop problem?

    The posted code does not validate the input to the extent that the given integer must be >= -1.
    If -1 is given, the loop should end without comparing -1 to max/min. Otherwise min would always be set to -1 on the way out of the loop.
    The logic in the code for max seems correct, but I am not so sure the min is.
    I would think the first iteration through the loop would be special. The first iteration there is no previous min/max, and the number given would be the min and max.

    So if this were my project, I would start by writing down the requirements of the project in list style. Then sort them into the order that they should be done in. For example if -1 is given, do not try to compare to max/min. So checking for -1 would be a step required to be done before comparing max/min. This ordered list will be useful in keeping the code on track. What I like to do is break the steps down into smaller steps. Then go over the list of steps again and break each step down more and more until each step seems so simple. Then as I write code I start with the first step, and get the code working to that point, and test before moving on to add code for the next step.

Similar Threads

  1. Simple Do Loop Problem
    By inflames098 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 10th, 2012, 03:43 PM
  2. One of the most simple loop awnsers
    By Firestar912 in forum Loops & Control Statements
    Replies: 8
    Last Post: May 7th, 2012, 07:13 PM
  3. [SOLVED] WHILE LOOP USING || (or) SIMPLE PROBLEM
    By SPACE MONKEY in forum Loops & Control Statements
    Replies: 3
    Last Post: May 7th, 2012, 12:23 AM
  4. Simple Nested Do/While Loop
    By Farmer in forum Loops & Control Statements
    Replies: 2
    Last Post: July 25th, 2011, 08:31 AM
  5. Replies: 4
    Last Post: May 15th, 2011, 06:03 AM