Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-20-2012, 11:34 AM   PM User | #1
__iCode__
New to the CF scene

 
Join Date: Mar 2012
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
__iCode__ is an unknown quantity at this point
Please help, Scanner integers

Hey guys,
I am currently trying to create a program that has number stored in an integer, when all the numbers have been entered the program returns the numbers and on the next line the average of the numbers. I have a problem though, I only want as many variables as the user inputs that way the program will slit out the average. If you dont get what I mean i'll try make it simpler. I want the user to enter a number e.g 28, when 28 is entered the program will aoutomatically create 28 integers called num1 - num28. Then the integers will get their value from the Java scanner. Basically is there a piece of code that allows me to do this. If you can help I will be very greatful. And here is the code if it helps

Code:
import java.util.Scanner;

class averageMark{
	public static void main(String args[]){
            System.out.println("Enter 30 numbers and the average percentage will be displayed!");
            Scanner marks = new Scanner(System.in);
            int mark1 = marks.nextInt();
            int mark2 = marks.nextInt();
            int mark3 = marks.nextInt();
            int mark4 = marks.nextInt();
            int mark5 = marks.nextInt();
            int mark6 = marks.nextInt();
            int mark7 = marks.nextInt();
            int mark8 = marks.nextInt();
            int mark9 = marks.nextInt();
            int mark10 = marks.nextInt();
            int mark11 = marks.nextInt();
            int mark12 = marks.nextInt();
	    int mark13 = marks.nextInt();
	    int mark14 = marks.nextInt();
	    int mark15 = marks.nextInt();
	    int mark16 = marks.nextInt();
	    int mark17 = marks.nextInt();
	    int mark18 = marks.nextInt();
	    int mark19 = marks.nextInt();
	    int mark20 = marks.nextInt();
            int mark21 = marks.nextInt();
	    int mark22 = marks.nextInt();
	    int mark23 = marks.nextInt();
	    int mark24 = marks.nextInt();
	    int mark25 = marks.nextInt();
	    int mark26 = marks.nextInt();
	    int mark27 = marks.nextInt();
            int mark28 = marks.nextInt();
            int mark29 = marks.nextInt();
	    int mark30 = marks.nextInt();
            
            int score[] = {mark1, mark2, mark3, mark4, mark5, mark6, mark7, mark8, mark9, mark10, mark11, mark12, mark13, mark14, mark15, mark16, mark17, mark18, mark19, mark20, mark21, mark22, mark23, mark24, mark25, mark26, mark27, mark27, mark28, mark29, mark30};
            int sum = 0;
            
            for(int counter = 0; counter<score.length; counter++){
                sum += score[counter];
            }
            
            System.out.println("The scores for this test where: " + mark1 + "%" + ", " + mark2 + "%" + ", " + mark3 + "%" + ", " + mark4 + "%" + ", " + mark5 + "%" + ", " + mark6 + "%" + ", " + mark7 + "%" + ", " + mark8 + "%" + ", " + mark9 + "%" + ", " + mark10 + "%" + ", " + mark11 + "%" + ", " + mark12 + "%" + ", " + mark13 + "%" + ", " + mark14 + "%" + ", " + mark15 + "%" + ", " + mark16 + "%" + ", " + mark17 + "%" + ", " + mark18 + "%" + ", " + mark19 + "%" + ", " + mark20 + "%" + ", " + mark21 + "%" + ", " + mark22 + "%" + ", " + mark23 + "%" + ", " + mark24 + "%" + ", " + mark25 + "%" + ", " + mark26 + "%" + ", " + mark27 + "%" + ", " + mark28 + "%" + ", " + mark29 + "%" + ", " + mark30 + "%");
            System.out.println("The average percentage for this test was: " + sum / score.length + "%");
            
	}
}
__iCode__ is offline   Reply With Quote
Old 03-20-2012, 04:11 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Use an array or collection from the start. It would just be silly to use an unknown size of variables.
PHP Code:
int iSize 30// Or prompt for size.
int[] aInput = new int[iSize];
for (
int i 0iSize; ++i)
{
    
aInput[i] = marks.nextInt();

Then iterate that to calculate the average.
Using an ArrayList<Integer> instead may be a better idea, then you don't even need to specify the size. You can iterate until the user enters Q or something, and the only difference is you need to try/catch an Integer.parseInt call and add to the collection.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
code, java, program

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:05 AM.


Advertisement
Log in to turn off these ads.