I'll write you up something.
brb2min
|
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: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); } } }
SUPPOSE TO BE:
WHAT IT'S DOING NOW: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.
If you need a better understanding, you can run it in eclipse. But can anyone tell me why this isn't working? :/Code:Please tlel me how many grades you wish to average. 4 22 Please enter in 22 grades.
I'll write you up something.
brb2min
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); } }
Default License, lol.
you call nextInt() twiceCode: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.");
Also, finding the average is not dividing by 10 (unless you have 10 values).
You divide by the total number of values.
« Previous Thread | Next Thread » |
Thread Information |
Users Browsing this ThreadThere are currently 1 users browsing this thread. (0 members and 1 guests) |