Reputation: 27
This is a pretty basic for
loop that I'm trying to design but for some reason, it just isn't working. I'm making a method that will have the user type in 3 test scores(integers). The problem, is that I need the 3 integer's values to be saved to a variable.
public static int testScore()
{ //starts method
int test1;
int test2;
int test3;
int i;
test1=0
for (i=1; i < 3; i++)
{ //starts for loop
System.out.print("\nPlease enter your test scores: ";
int test1=kb.nextInt();
} //end for loop
Ignoring any syntax errors that I might have (rough draft), will this save the 3 values that I generate to test1,2, and 3, or just test 1 since I have int test1=... thanks.
Upvotes: 0
Views: 285
Reputation: 49
Your loop only gathers input for a single datum per iteration.
You have two options:
Add special cases per variable per iteration (This is a maintenance nightmare)
As earlier poster replied use an Array
of appropriate size and index it with same variable that is driving the for loop:
Array[i]=kb.nextInt()
Upvotes: 0
Reputation: 5324
As I understand your question, you want to save 3 values (that you enter somewhere) in variables.
I think you should use an array for that.
int[] tests = new int[3];
for (int i = 0; i < 3; i++) {
System.out.print("\nPlease enter your test scores: ");
tests[i] = kb.nextInt();
}
Later on you can use Lists (dynamic arrays), if you want to save an undefined number of values.
Upvotes: 3
Reputation: 95948
That's why there are arrays in Java.
int[] test = new int[3];
System.out.print("\nPlease enter your test scores: ";
for (i=0; i < 3; i++) {
test[i] = kb.nextInt();
}
You're declaring test1
twice - Once in the scope of testScore
and once in the scope of the loop and that's not good.
Also please note that arrays are zero-based in Java, you might want to start the loop from 0.
It's recommended to give the variables a meaningful names, not test
.. consider to change it to values
for example..
Upvotes: 3
Reputation: 920
Best way is use array. But you still want to do without array :
System.out.print("\nPlease enter your test1 scores: ");
int test1=kb.nextInt();
System.out.print("\nPlease enter your test2 scores: ");
int test2=kb.nextInt();
System.out.print("\nPlease enter your test3 scores: ");
int test3=kb.nextInt();
But I strongly recommend to use array. Go through on this link to learn array http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Upvotes: 0
Reputation: 5637
Use arrays instead of a single variable
Integer[] test_array = new Integer[3];
for (i=1; i < 3; i++)
{
System.out.print("\nPlease enter your test scores: ";
test_array[i] = kb.nextInt();
}
Upvotes: 0
Reputation: 4313
You are writing the three values three times into the same variable so only the last entry will stay. If you need to store more values into a single variable use an array. Also you are declaring the variable new inside the loop so it will hide the external one...
int[] store = new int[3]
for (int i=0; i < 3; i++)
{ //starts for loop
System.out.print("\nPlease enter your test scores: ";
store[i]=kb.nextInt();
}
Upvotes: 1