...

View Full Version : Please help, Scanner integers



__iCode__
03-20-2012, 12:34 PM
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 :)



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 + "%");

}
}

Fou-Lu
03-20-2012, 05:11 PM
Use an array or collection from the start. It would just be silly to use an unknown size of variables.


int iSize = 30; // Or prompt for size.
int[] aInput = new int[iSize];
for (int i = 0; i < iSize; ++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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum