user variable input


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: user variable input

  1. #1
    Join Date
    Oct 2002
    Posts
    15

    user variable input

    Im trying to create a program that asks the user how many numbers they wish to input. then the program shall print out the 3 largest numbers from the list. so far i have gotten the program to ask for the numbers according to how many the user desires, but once the program prints the outcome, it only prints zeros. im not sure why.... if you would like my source to look over, please feel free to email me or message me...
    wuwang84@hotmail.com
    AIM- toxicity61284

    thank you

  2. #2
    Join Date
    Sep 2002
    Posts
    78
    plz post the source code (dont forget code tags! look at my sig on how to do this) and i'll try to help
    ¿Red Baron?
    [code] /* dont forget code tags! */ [/code]
    Come visit the Café!

  3. #3
    Join Date
    Oct 2002
    Posts
    15

    user variable input #2

    /*
    import java.io.*;
    public class Assignment4
    {
    public static void main(String args[]) throws IOException
    {


    BufferedReader input= new BufferedReader(new InputStreamReader(System.in));
    System.out.println("How many numbers do you have?>");
    int n= Integer.parseInt(input.readLine());
    int x=0;
    int largest=0;
    int second=0;
    int third=0;
    while(x<n)
    {


    System.out.println("Input number "+ (x+1) + ">");
    int i = Integer.parseInt(input.readLine());
    //i=largest;

    //x+=1;
    if(i>largest)
    {
    largest=second;
    i=largest;
    }
    else if(i<largest)
    {
    //second=third;
    //largest=second;
    i=second;
    }
    if(i<second)
    {
    //third=second
    i=third;
    }
    x+=1;
    }

    System.out.println(" the three largest numbers are: " + largest + "," + second + "," + third);


    }
    }
    */

  4. #4
    Join Date
    Sep 2002
    Posts
    78
    very simple mistake, you were setting the assigning values to the variables improperly
    such as "largest=second;"
    this sets the value of variable second as the variable largest its suppose to be the other way around :) "second=largest"

    Code:
    import java.io.*;
    public class Assignment4
    {
          public static void main(String args[]) throws IOException
          {
                BufferedReader input= new BufferedReader(new InputStreamReader(System.in));
                System.out.println("How many numbers do you have?>");
                int n = Integer.parseInt(input.readLine());
                int x=0;
                int largest=0;
                int second=0;
                int third=0;
                while(x<n)
                {
                      System.out.println("Input number "+ (x+1) + ">");
                      int i = Integer.parseInt(input.readLine());
    
                      if(i>largest) //changed here a bit
                      {
                            third=second;
                            second=largest;
                            largest=i;
                      }
                      else if(i>second) //changed here a bit
                      {
                            third=second;
                            second=i;
                      }
                      else if(i>third)  //changed here a bit
                      {
                            third=i;
                      }
                      x+=1;
                }
                System.out.println(" the three largest numbers are: " + largest + "," + second + "," + third);
          }
    }
    ¿Red Baron?
    [code] /* dont forget code tags! */ [/code]
    Come visit the Café!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center