How can I make this shorter
I want to make the following code shortened into a loop
can anyone help me out
Code Java:
while (usernumber >= 1 && usernumber <= 7)
{
usernumber = scanner.nextInt();
if (usernumber == vehicle[0]);
{
counter1 = counter1 + 1;
}
if (usernumber == vehicle[1])
{
counter2 = counter2 + 1;
}
if (usernumber == vehicle[2])
{
counter3 = counter3 + 1;
}
if (usernumber == vehicle[3])
{
counter4 = counter4 + 1;
}
if (usernumber == vehicle[4])
{
counter5 = counter5 + 1;
}
if (usernumber == vehicle[5])
{
counter6 = counter6 + 1;
}
if (usernumber == vehicle[6])
{
counter7 = counter7 + 1;
}
Thanks
Re: How can I make this shorter
Compress the counterX variables into an array.
If only one of the if statements can be true, then
To get the code you've posted to execute faster, use if/else if vs all ifs. The first true if means that none of the rest of the ifs can be true.
Re: How can I make this shorter
Look into using loops, switch statements, and different data types - together with Norm's advice would boil your code down to a few lines.