Results 1 to 3 of 3

Thread: JAVA task.

  1. #1
    DEA
    DEA is offline Member
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Default JAVA task.

    Hello to everyone! I am new here and also new at this computer world.

    I have a small problem on this homework assignment:

    Write a program that receives a list of positive integers and shows on the screen the smallest number(the minimum) and the second smallest number.
    The number of numbers is unknown. The list of numbers ends with a negative number.

    What I have done is:
    PHP Code:
    import java.util.Scanner;
            public class solex4 {
        public static void main(String[] args) {
     Scanner input= new Scanner(System.in);
            int num;
    
    
    int num1;
    num=input.nextInt();
    num1=num;
    num=input.nextInt();
    while(num>=0)
    {
     if(num<num1)
    {
    num1=num;
    }
    num=input.nextInt();
    }
    System.out.println("num , num1");
        }}
    ...but this is not working... :(


    Thank you all!

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    1. Format your code properly. If this is how it looks in your editor, then it is not surprising that you find it confusing. If it is properly formatted in your editor, then take the time to format it properly for the forum too -- please don't waste our time.

    2. Think about your requirements before you start coding. Try working it through with pencil and paper, or with real objects -- for instance, shuffle a deck of playing cards and take the first five cards off the top, putting the Smallest and the SecondSmallest in a designated spot on the table, and discarding the rest. Think about each step as you do it, and try to write the steps down.

    3. It's a good idea to write out your thinking in pseudo-code, something like this:
    Java Code:
    // establish variables for smallestValue and secondSmallestValue
    //        and initialize them both to Integer.MAX_VALUE
    // set up a Scanner for user input
    // print out instructions for the user
    // loop
            // prompt user for next number
            // read number entered by user
            // if number is negative (less than zero)
                    // print out smallestValue and secondSmallestValue
                    // exit program
            // if number is less than secondSmallestValue
                    // set secondSmallestValue to number
            // if number is less than smallestValue
                    // set secondSmallestValue to smallestValue
                    // set smallestValue to number entered by user
    // end loop
    By writing your pseudo-code in comments like that, you can leave them in your program to remind yourself what each piece of the program should be doing.

    -Gary-

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I second everything stated above. Also, you'll want to give your variables names that have meaning, that make sense. This way if anyone looks at the variable, they can almost guess immediately what it's supposed to do. For instance instead of num and num1, how about inputNumber, smallestNumber, and nextSmallestNumber.

    Much luck!

Similar Threads

  1. Java Task
    By Sokox in forum NetBeans
    Replies: 4
    Last Post: 12-13-2009, 10:57 AM
  2. java-task.com
    By marcellis in forum Introductions
    Replies: 0
    Last Post: 09-28-2009, 11:46 AM
  3. Replies: 15
    Last Post: 01-07-2009, 05:42 AM
  4. isolated task in java
    By sardare in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-07-2008, 09:54 PM
  5. java : how to hide application icon from a task bar
    By yogeshagashe in forum Advanced Java
    Replies: 0
    Last Post: 03-12-2008, 02:05 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
  •