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 3 of 3

Thread: It is possible that not using array to list the positive integer <100??

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default It is possible that not using array to list the positive integer <100??

    hi...
    i got problem with my code.
    i unable to display the largest and smallest integer in range of 1 to 100...

    This is the title.
    Write a program that reads a list of positive numbers < 100 and displays the largest and the smallest. A 0 (the number zero) should terminate the list. Do not use an array to answer this question. There is at least one number before the 0.


    this my code.
    [CODE]
    public class Integer3 
    {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) 
        {
            // TODO code appli  public static void main(String [] args)
     
            {
                int min = 0;
                int max = 0;
                int integer = 0;
                max=min=integer;
     
                Scanner keyboard = new Scanner(System.in);
     
     
            System.out.print("Please enter a positive integer or 0 to terminate: ");
            integer = keyboard.nextInt();
            while(integer != 0)
            {
     
                if(integer >max )
                {   
                    max = integer;
                }
                if(integer < min )
                {   
                    min =integer;
     
                }
     
                if (integer < 1 || integer >100)
                {
                    System.out.println("Please enter again a positive integer");
                        integer = keyboard.nextInt()
                }
     
            }
     
                System.out.println("Smaller integer: " + min);//display the smallest integer
                System.out.println("Largest integer: "+max);//display the largest 
            }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: It is possible that not using array to list the positive integer <100??

    Do you have a response to my reply on this other thread?
    It is possible that not using array to list the positive integer <100??
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: It is possible that not using array to list the positive integer <100??

     
    int max = 0;
    int min = 0;
    int num = 0;
    min=max=num;

    There's your problem, don't initialize the variables all as the same number. If you start min as zero then nothing can be less than 0, so you must initialize min as greater than it can possibly be, in this case, 100. take the min=max=num line out completely.

Similar Threads

  1. Replies: 8
    Last Post: April 14th, 2012, 12:24 PM
  2. Array List of Array Lists working for first item but not for second.
    By javapenguin in forum Collections and Generics
    Replies: 6
    Last Post: February 15th, 2012, 04:12 PM
  3. The sum and product of a positive number between 1000 and 9999.
    By metaleddie13 in forum Member Introductions
    Replies: 1
    Last Post: September 15th, 2011, 04:39 AM
  4. Need help deserializing an integer array.
    By jeremy_ in forum Collections and Generics
    Replies: 10
    Last Post: May 30th, 2011, 11:50 AM
  5. Read A File and Store Values into a 2-Dimensional Integer Array?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 08:13 PM