Results 1 to 4 of 4
  1. #1
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default i really need help

    hey, this is my first post on this forum. I'll get right to the point. I'm creating a program that does a bunch of math operations such as Law of Cosines, Quadratic Formula, etc. I'm working on an tool that computes the Average of a specified amount of numbers given by the user. What i have so far, is an integer array that contains the number of numbers that the user can choose. I've also constructed a new integer array for the actual numbers. Now I'm trying to use a for loop in order for it to display "What is number 1?", "What is number 2?", etc. It prints that with no problem, but when i try to get it to have the values inputted and averaged, what it's doing is just dividing the last inputted number. This is all very unclear so take a look at the code:

    Scanner scan = new Scanner(System.in);
    System.out.print("How many numbers? ");
    int x = scan.nextInt(); //numofnums
    int[] numOfNums = {2, 3, 4, 5, 6, 7, 8, 9};
    int z = numOfNums[x-2]; //index of array
    int average = 0;

    for(int y = 1; y < (z+1); y++)
    {
    System.out.print("What is number " + y + "? ");
    Scanner scan2 = new Scanner(System.in);
    int[] nums = new int[8];
    average = scan2.nextInt() / x;
    }

    System.out.println(average);



    -------------------
    Can someone please help me figure out how to make it so that the numbers the user inputs are taken into account and averaged.
    Last edited by gibsonrocker800; 11-14-2007 at 03:30 AM.

  2. #2
    JT4NK3D's Avatar
    JT4NK3D is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Default

    if its a problem with the scanner, theres a good text class i got from a tutorial called TextIO that prints, gets input and other text related things with no problem.. just click the link TextIO.java at Javanotes 5.0, Section 2.4 -- Text Input and Output
    if its a problem with sumthing else i dont know.. btw just compile TextIO, get it in same folder and use either the TextIO.getlnInt(); method or TextIO.getlnDouble(); method or TextIO.getlnBoolean(); method theres on for all primitive types and string i think... use what you want in it

  3. #3
    blackstone is offline Member
    Join Date
    Nov 2007
    Posts
    6
    Rep Power
    0

    Default

    The reason it is only dividing the last number entered is because of the last line in your for loop: average = scan2.nextInt() / x;

    All that is doing is getting another number from the user and dividing it by the total number of integers that there should be. What you need to do is be adding up all the numbers entered by the user and then, outside of the for loop, divide that total by the total number of integers to find the average.

    And just to satisfy my own curiosity, what are you using the numOfNums array for? I don't quite understand that...

  4. #4
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    i figured it out. and the reason why i'm setting up an array for the numOfNums is because i'm trying to learn how to put arrays to use, i could have easily set this program up with if statements or something, but i want to advance in my logic skills.

Posting Permissions

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