Thread: Simple Average program, simple error?

Results 1 to 7 of 7
  1. #1 Simple Average program, simple error? 
    Rukin sux

    Sieg's Avatar
    Join Date
    May 2009
    Posts
    1,041
    Thanks given
    223
    Thanks received
    281
    Rep Power
    244
    Code:
    import java.util.Scanner;
    
    class apples {
    public static void main(String [] args) {
    	
    		System.out.println("Please tell me how many grades you wish to average.");
    		
    		Scanner value = new Scanner(System.in);
    		if (value.nextInt() < 2) {
    			System.out.print("The number you entered must be greater than or equal to 2.");
    		}
    		else
    		{
    		
    		int amount;
    		amount = value.nextInt();
    		System.out.print("Please enter in " + amount + " grades.");
    		Scanner input = new Scanner(System.in);
    		int total = 0;
    		int grade;
    		double average;
    		int counter = 0;
    		
    		while (counter < amount) {
    			grade = input.nextInt();
    			total = total + grade;
    			counter ++;
    		}
    		average = total/10;
    		System.out.print("Your average is: " + average);
    		}
    	}
    
    }
    When I type in 4, which is the scanner value in the code, I'm suppose to be telling the script that I want to average four grades. But when I type in something, it stops when I type in two grades. For example:

    SUPPOSE TO BE:

    Code:
    Please tell me how many grades you wish to average.
    4
    Please enter in 4 (int amount) grades.
    11
    11
    11
    11
    Your average is 11.
    WHAT IT'S DOING NOW:

    Code:
    Please tlel me how many grades you wish to average.
    4
    22
    Please enter in 22 grades.
    If you need a better understanding, you can run it in eclipse. But can anyone tell me why this isn't working? :/

     

  2. #2  
    Registered Member
    Mister Maggot's Avatar
    Join Date
    Dec 2008
    Posts
    7,227
    Thanks given
    3,283
    Thanks received
    2,875
    Rep Power
    5000
    I'll write you up something.

    brb2min
     

  3. #3  
    Registered Member
    Mister Maggot's Avatar
    Join Date
    Dec 2008
    Posts
    7,227
    Thanks given
    3,283
    Thanks received
    2,875
    Rep Power
    5000
    Code:
    /*
     *            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
     *                    Version 2, December 2004
     * 
     * Copyright (C) 2010 Mister Maggot
     * Everyone is permitted to copy and distribute verbatim or modified
     * copies of this license document, and changing it is allowed as long
     * as the name is changed.
     * 
     *            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
     *   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
     * 
     *  0. You just DO WHAT THE FUCK YOU WANT TO.
     */
    
    package org.maggot.scanner;
    
    import java.util.Scanner;
    
    /**
     * A simple class to find the average grades.
     * @author Mister Maggot (Daniel R.) <[email protected]>
     * @version 1.0
     */
    public class Main {
    
        /**
         * The main method; called on application startup.
         * @param args The command line arguments.
         */
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while(in != null) {
                System.out.println("Please enter the grades to average.");
                System.out.println("Average: " + getAvg(in.nextLine().split(" ")));
            }
        }
    
        /**
         * Finds the average of any amount of numbers.
         * @param split The numbers to average.
         * @return The average of the two 
         */
        private static String getAvg(String[] split) {
            int count = split.length;
            int total = 0;
            for(int i = 0; i < split.length; ++i)
                total += Integer.parseInt(split[i]);
            return String.valueOf((double) total / count);
        }
    }
     

  4. #4  
    Rukin sux

    Sieg's Avatar
    Join Date
    May 2009
    Posts
    1,041
    Thanks given
    223
    Thanks received
    281
    Rep Power
    244
    Quote Originally Posted by Mister Maggot View Post
    Code:
    /*
     *            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
     *                    Version 2, December 2004
     * 
     * Copyright (C) 2010 Mister Maggot
     * Everyone is permitted to copy and distribute verbatim or modified
     * copies of this license document, and changing it is allowed as long
     * as the name is changed.
     * 
     *            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
     *   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
     * 
     *  0. You just DO WHAT THE FUCK YOU WANT TO.
     */
    
    package org.maggot.scanner;
    
    import java.util.Scanner;
    
    /**
     * A simple class to find the average grades.
     * @author Mister Maggot (Daniel R.) <[email protected]>
     * @version 1.0
     */
    public class Main {
    
        /**
         * The main method; called on application startup.
         * @param args The command line arguments.
         */
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while(in != null) {
                System.out.println("Please enter the grades to average.");
                System.out.println("Average: " + getAvg(in.nextLine().split(" ")));
            }
        }
    
        /**
         * Finds the average of any amount of numbers.
         * @param split The numbers to average.
         * @return The average of the two 
         */
        private static String getAvg(String[] split) {
            int count = split.length;
            int total = 0;
            for(int i = 0; i < split.length; ++i)
                total += Integer.parseInt(split[i]);
            return String.valueOf((double) total / count);
        }
    }
    Lol thanks.

    I'll have to figure out what a few things mean, but it's appreciated. I also appreciate you making it a "DO WHAT THE FUCK YOU WANT TO DO" license.

     

  5. #5  
    Registered Member
    Mister Maggot's Avatar
    Join Date
    Dec 2008
    Posts
    7,227
    Thanks given
    3,283
    Thanks received
    2,875
    Rep Power
    5000
    Default License, lol.
     

  6. #6  
    Renown Programmer
    veer's Avatar
    Join Date
    Nov 2007
    Posts
    3,746
    Thanks given
    354
    Thanks received
    1,370
    Rep Power
    3032
    Code:
    		System.out.println("Please tell me how many grades you wish to average.");
    		
    		Scanner value = new Scanner(System.in);
    		if (value.nextInt() < 2) {
    			System.out.print("The number you entered must be greater than or equal to 2.");
    		}
    		else
    		{
    		
    		int amount;
    		amount = value.nextInt();
    		System.out.print("Please enter in " + amount + " grades.");
    you call nextInt() twice
     

  7. #7  
    Banned

    Join Date
    Mar 2008
    Posts
    2,595
    Thanks given
    128
    Thanks received
    191
    Rep Power
    0
    Also, finding the average is not dividing by 10 (unless you have 10 values).

    You divide by the total number of values.
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Simple Java program [Rep++]
    By mige5 in forum Requests
    Replies: 0
    Last Post: 07-31-2010, 04:14 PM
  2. Simple program
    By MH-60S in forum Application Development
    Replies: 3
    Last Post: 08-07-2009, 04:13 AM
  3. QBasic - Making a simple program
    By Bridget7298 in forum Website Development
    Replies: 3
    Last Post: 07-19-2009, 08:33 PM
  4. simple reminder program
    By Light Moger in forum Application Development
    Replies: 2
    Last Post: 02-26-2009, 02:08 AM
  5. A simple question requires a simple answer!
    By Matt_ in forum RS2 Server
    Replies: 2
    Last Post: 12-23-2007, 12:10 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •