# Thread: Creating an amount of variables based on user input

1. Member
Join Date
Apr 2012
Posts
2
Rep Power
0

## 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:

Java Code:
```package test;

import java.util.Scanner;

static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {

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

}

}```

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12

## Re: Creating an amount of variables based on user input

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.

3. Member
Join Date
Apr 2012
Posts
2
Rep Power
0

## 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?

4. ## 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

5. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12

## Re: Creating an amount of variables based on user input

Originally Posted by HDInfinity
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)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•