Results 1 to 6 of 6
  1. #1
    jonvh432 is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default Need some help with java program

    HI everyone im new to java and I am in the attempt to right a java program for my class.

    What my assignment is asking me to do is to use a while loop inside a Java code.

    Then to use a sentinel value to signal the end of a loop.

    So far i have been able to add the while loop to the code. Also somehow from what the user inputs i need to find out the smallest number and the largest number and get them to display.

    A sample session output should look like this:
    Enter an integer number or -99 to quit: 5
    Enter an integer number or -99 to quit: 10
    Enter an integer number or -99 to quit: -99
    Largest: 10
    Smallest: 5


    My code so far look like this

    import java.util.Scanner;

    public class BigAndSmall
    {
    public static void main(String[] args)
    {
    // Declare variables for user input
    int number = 0;

    // Create a Scanner object to read from the keyboard
    Scanner keyboard = new Scanner(System.in);

    // Get the integers from users


    while (number != -99)

    {
    System.out.print("Enter an integer number or -99 to quit:");

    number = keyboard.nextInt();

    }
    System.out.println("You did not enter any numbers");
    number = keyboard.nextInt();
    }
    }


    Any Help would really be appreciated!!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Any help with what? Ask a specific question.

  3. #3
    jonvh432 is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    I need help adding a sentinel value to signal the end of a loop.
    I also need help to figuring out how to get it to display the smallest number and the largest number in the output

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Quote Originally Posted by jonvh432 View Post
    I need help adding a sentinel value to signal the end of a loop.
    You have already done it.
    Java Code:
    while (number != -99)
    I also need help to figuring out how to get it to display the smallest number and the largest number in the output
    Have two variables to keep track which is largest and smallest. The use if statements to compare each user input to those values to see if the latest value is smaller or larger.

  5. #5
    jonvh432 is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    I have added the if statement but i cant seem to get it give me the right out put format. Which is this one below

    Enter an integer number or -99 to quit: 5
    Enter an integer number or -99 to quit: 10
    Enter an integer number or -99 to quit: -99
    Largest: 10
    Smallest: 5



    import java.util.Scanner;

    public class BigAndSmall
    {
    public static void main(String[] args)
    {
    // Declare variables for user input
    int number = 0;
    int largest = 0;
    int smallest = 0;
    // Create a Scanner object to read from the keyboard
    Scanner keyboard = new Scanner(System.in);

    // Get the integers from users


    while (number != -99)

    {
    System.out.print("Enter an integer number or -99 to quit:");

    number = keyboard.nextInt();

    }


    System.out.println("You did not enter any numbers");
    number = keyboard.nextInt();

    //if staement Largenumber
    if ( largest >= number)
    {
    largest = keyboard.nextInt();
    }
    System.out.println("Largest:" + largest);

    //If Statemet Smallest number
    if (smallest <= number)
    {
    smallest = keyboard.nextInt();
    }
    largest = keyboard.nextInt();
    System.out.println("Smallest:" + smallest);


    }
    }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Your loop:
    Java Code:
    while (number != -99) {
        System.out.print("Enter an integer number or -99 to quit:");
        number = keyboard.nextInt();
    }
    Perhaps you should be checking each number the user enters is larger or smaller.
    Java Code:
    if ( largest >= number) {
        largest = keyboard.nextInt();
    }
    Lets assume largest is 12 and number is 54. The if statement fails and you throw away the 54. Lets assume largest is 45 and number is 5. The if statement is true so you assign largest to the next number entered by user. Which brings me to why the heck are you calling nextInt all over the bluddy place. Just ask for user input once inside the loop.

Similar Threads

  1. Call one Java Program from another Java Program
    By rajpalparyani in forum New To Java
    Replies: 3
    Last Post: 02-14-2011, 04:13 AM
  2. Replies: 4
    Last Post: 07-30-2010, 12:25 PM
  3. execute java program within java program
    By popey in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 05:32 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •