Results 1 to 16 of 16
  1. #1
    d2011 is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default can someone look at this

    i want to make sure this is right i have to:

    1. prompt the user to input 5 decimal numbers (1,2,3,4,5)

    2. print the 5 decimal numbers

    3, convert decimal to nearest integer

    4. add the 5

    5. prints the sum and average

    import java.util.*;

    public class chapter 2_1
    {
    public static void main (string[] args)
    {
    scanner input = new scanner( System.in);

    int number1;
    int number2;
    int number3;
    int number4;
    int number5;
    double sum;
    double average;

    number1 = 1;

    number2 = 2;

    number3 = 3;

    number4 = 4;

    number5 = 5;

    System.out.print( "Enter first integer: " );
    number1 = input.nextInt();
    System.out.println;

    System.out.print( "Enter second integer: " );
    number2 = input.nextInt();
    System.out.println;

    System.out.print( "Enter third integer: " );
    number3 = input.nextInt();
    System.out.println;

    System.out.print( "Enter fourth integer: " );
    number4 = input.nextInt();
    System.out.println;

    System.out.print( "Enter fifth integer: " );
    number5 = input.nextInt();
    System.out.println;

    System.out.println("Enter 5 decimal numbers");
    num = console.nextInt();
    System.out.println;


    sum = number1 + number2 + number3 + number4 + number5;

    System.out.print("sum is")

    average = sum / 5;

    outfile.printf(number1, number2, number3, number4, number5, sum, average)
    }

    input.close();
    outfile.close();
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Why don't you use an array int[] numbers= new int[5]? and what is that outfile thing near the end of your code?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    d2011 is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    cool ill try that out where would i put the int [] at?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by d2011 View Post
    cool ill try that out where would i put the int [] at?
    I'd say just before you want to use the array, so instead of defining those five numbers you define your array there.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I don't see the step where you convert from decimal to integer, do you mean to be able to take 1.34 as an input and convert it to 1? If so you may want to declare a float array and store everything as floats, then use Math.round(float num) to round them to ints.

  6. #6
    d2011 is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    So I Would put it after the scanner input

  7. #7
    d2011 is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    Yea that's the part I need help on right now

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    instead of
    Java Code:
    int num1;
    int num2;
    ..
    int numn;
    it would be much better to declare JUST
    Java Code:
    float[] nums = new float[5];
    You can then make a loop to prompt for numbers and store items in the array.
    Java Code:
    for(int i = 0; i < nums.length; i++){
    //prompt
    //store input in array positions
    }
    Hope this helps, this will allow you to ask for numbers, and store the inputs.

  9. #9
    d2011 is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    so it would be this :

    import java.util.*;

    public class chapter 2_1
    {
    public static void main (string[] args)
    {
    scanner input = new scanner( System.in);

    float[] nums = new float [5];
    double sum;
    double average;

    for (int i = 0; i < nums.length; i ++0){

    System.out.print( "Enter first integer: " );
    number1 = input.nextInt();
    System.out.println;

    System.out.print( "Enter second integer: " );
    number2 = input.nextInt();
    System.out.println;

    System.out.print( "Enter third integer: " );
    number3 = input.nextInt();
    System.out.println;

    System.out.print( "Enter fourth integer: " );
    number4 = input.nextInt();
    System.out.println;

    System.out.print( "Enter fifth integer: " );
    number5 = input.nextInt();
    System.out.println;

    System.out.println("Enter 5 decimal numbers");
    num = console.nextInt();
    System.out.println;


    sum =
    Last edited by d2011; 02-02-2011 at 05:56 PM.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by d2011 View Post
    So I Would put it after the scanner input
    What happens when you try it? Remember, I wrote: define your array just before you want to use it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Thats close, except the loop will print all that code 5 times.

    Is there a way you can print one statement where the item number changes with each pass through the loop? What happens if you print i? How can you include i to create one print statement which will change?

    Also, you got rid of the variables number1, number2, ...numbern. To reference an item in an array you do
    Java Code:
    arrayName[index number]

  12. #12
    d2011 is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    so it would be this :

    import java.util.*;

    public class chapter 2_1
    {
    public static void main (string[] args)
    {
    scanner input = new scanner( System.in);

    float[] nums = new float [5];
    double sum;
    double average;

    for (int i = 0; i < nums.length; i ++0){

    arrayName[index number]

    do i get rid of all of the

    System.out.print( "Enter first integer: " );
    number1 = input.nextInt();
    System.out.println;

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    no.
    Java Code:
    arrayName[index number]
    are place holders, you want to use your arrays name for arrayName, and some index number in between brackets
    Java Code:
    arr[i];
    arr[0];
    arr[1];
    are all valid
    which one sounds the most important to you?
    also, this simple references that item in the array, if you want to store something there you need
    Java Code:
    arr[index] = add something;
    you can figure out what add something should be yourself.
    Also, EACH time through the loop it should prompt a message asking for a number
    Java Code:
    System.out.println("Please enter a number:");
    System.out.println("Please enter number " + something + ": ");
    are all valid things you can print, choose one which you like the best.

    also, proper loop structure is
    Java Code:
    for(initialization; condition; some change){
      statements
    }
    I usually use
    Java Code:
    for(int i = 0; i < something; i++){
     statements
    }
    Try reading these two things
    http://download.oracle.com/javase/1....il/Arrays.html
    http://download.oracle.com/javase/tu...ts/arrays.html
    Last edited by sunde887; 02-02-2011 at 06:28 PM.

  14. #14
    d2011 is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    i looked over the web pages you gave me about arrays and came up with this

    import java.util.*;

    public class chapter 2_1
    {
    public static void main (string[] args)
    {
    scanner input = new scanner( System.in);

    float[] nums = new float [5];
    double sum;
    double average;
    arrayName[index number]
    arr[1] = 1;
    arr[2] = 2;
    arr[3] = 3;
    arr[4] = 4;
    arr[5] = 5;

    for(int i = 0; i < something; i++){
    statements
    }

    System.out.println("Please enter a number:");
    System.out.println("Please enter number " + something + ": ");

    System.out.print( "Enter first integer: " );
    number1 = input.nextInt();
    System.out.println;

    System.out.print( "Enter second integer: " );
    number2 = input.nextInt();
    System.out.println;

    System.out.print( "Enter third integer: " );
    number3 = input.nextInt();
    System.out.println;

    System.out.print( "Enter fourth integer: " );
    number4 = input.nextInt();
    System.out.println;

    System.out.print( "Enter fifth integer: " );
    number5 = input.nextInt();
    System.out.println;

    System.out.println("Enter 5 decimal numbers");
    num = console.nextInt();
    System.out.println;


    sum = number1 + number2 + number3 + number4 + number5;

    System.out.print("sum is")

    average = sum / 5;
    }
    }

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

    Default

    Quote Originally Posted by d2011 View Post
    i looked over the web pages you gave me about arrays and came up with this ...
    So you've solved your problem then?

    Also you'll want to use code tags when posting code in this forum so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Im sorry to sound rude if you really just don't understand, but, are you even trying this, or just taking what I say and adding it in word for word?

    If you are trolling, oh well, but if you really are trying to understand this and just having some trouble I will continue to help.

    When I say something like statements or something, I am giving you a general idea of how the program should look. However, you must decide what something and statements should be equal to.

    This program should consist of 4 important things, a scanner, an array and 2 for loops. One loop should prompt for input and add something to the array with each pass. The second loop should print out the array.

    This is the basic part of the program, to add the functionality you need you need 2 additional int items, sum and average. and another for loop. This third loop should loop through the array, round the number, then add it to sum.

    Finally compute the avg with the sum.

    This is basically a step by step procedure for this, however, you will need to translate it into java yourself.

Posting Permissions

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