# Creating an amount of variables based on user input

• 04-11-2012, 04:20 AM
HDInfinity
Creating an amount of variables based on user input
I am fairly new to java so for my first program I though I would create a grading system based on Homework,Quizzes and Tests. The user selects what percentage of each is over the total grade. Then asks you how many of each you are grading. Using that info it will determine how much each individual Homework, Quiz, and Test is worth if they were given 100%. I am trying to allow the person to individually grade each homework/quiz/test so that the total grade will be completley accurate.

So overall what I need to do is when a person for example inputs that they are grading 5 homeworks, 2 quizzes, and 1 test. Java will create 5 variables for Homework, 2 for quizzes, and 1 new test variable. Each allowing the user to input what the value for each variable is. How do I go about doing this?

Here is the code:

Code:

``` package test; import java.util.Scanner; public class TGrade {                 static Scanner scanner = new Scanner(System.in);                 public static void main(String[] args) {                                 System.out.println("Welcome to Mario's Grader!");                                 System.out.println("What % is Homework");                 int hwp = scanner.nextInt();                 // hwp = Homework / Total                                                 System.out.println("How many Homework's are you grading?");                 int hwa = scanner.nextInt();                 // hwa = Amount of Homeworks Being Graded                                 int hwe = hwp / hwa;                 // hwe = How much each Homework is worth                                 System.out.println("What % are Quizzes");                 int qp = scanner.nextInt();                 // qp = Quiz / Total                                 System.out.println("How many Quizzes are you grading?");                 int qa = scanner.nextInt();                 // qa = Amount of Quizzes being graded                                 int qe = qp / qa;                                 int tp = 100 - hwp - qp;                 // tp = Percentage for Test/Total                                 System.out.println("How many Tests are you grading?");                 int ta = scanner.nextInt();                 // ta = Amount of Tests being graded                                 int te = tp / ta;                                 System.out.println("The percentage for Quiz/Total is =" + qp);                 System.out.println("The percentage for Homwork/Total is=" + hwp);                 System.out.println("The perecentage for Test/Total is=" + tp);                                 System.out.println("You are grading " + qa + " Quizzes");                 System.out.println("You are grading " + hwa + " Homeworks");                 System.out.println("You are grading " + ta + " Tests");                                 System.out.println("Each quiz is worth " + qe + " points");                 System.out.println("Each homework is worth " + hwe + " points");                 System.out.println("Each test is worth " + te + " points");                                                         }                 }```
• 04-11-2012, 04:41 AM
pbrockway2
Re: Creating an amount of variables based on user input
Quote:

So overall what I need to do is when a person for example inputs that they are grading 5 homeworks, 2 quizzes, and 1 test. Java will create 5 variables for Homework, 2 for quizzes, and 1 new test variable.
Separate variables are not needed. (And there is no way in Java of declaring some number of variables decided on at runtime.). Consider using arrays: for example the homework marks could be put into an int array which you declare to have size hwa.
• 04-11-2012, 05:06 AM
HDInfinity
Re: Creating an amount of variables based on user input
Can I make it so for every Int created by the array the user can provide input for each one?
• 04-11-2012, 07:16 AM
DarrylBurke
Re: Creating an amount of variables based on user input
HDInfinity, please don't remove the content of your posts. This time round, I've deleted the thread in which you replaced the content with the single word "Resolved." Next time something like that happens, please leave the question and share the solution. That's what a forum is all about.

db
• 04-11-2012, 08:23 AM
pbrockway2
Re: Creating an amount of variables based on user input
Quote:

Can I make it so for every Int created by the array the user can provide input for each one?

The array provides access to each of its element: you can assign values to each element, and read what they are. So, yes, you can use input from the user to give the array elements values.

See Arrays (The Java Tutorials > Learning the Java Language > Language Basics)