문제

At this point I'm starting to go insane. please point out what I've input incorrectly here.

After pardoning the code for incrementing twice, I receive the error on line 11, which is the print out line, counter cannot be resolved to a variable.

import java.util.Scanner;
//import java.util.Random;
public class overpower {
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        //Random numgen = new Random();
        int arraytest[]={1,2,5,8,9};
        //int counter = 0;

        for(int counter = 0; counter < arraytest.length; counter++);
            System.out.println(arraytest[counter]);
        counter++;
    }
}
도움이 되었습니까?

해결책 2

You have a three main issue:

  1. You missed the if opening bracket.
  2. You have to remove the semicolon at the end of if statement
  3. You need to remove the second increment for the counter

So try to change your code like:

for(int counter = 0; counter < arraytest.length; counter++) { // remove the semicolon and add the opening bracket.
    System.out.println(arraytest[counter]);
}

다른 팁

Here are more details, but the MaxMega comment is 100% correct

You do not have a for loop. You have a 1 line for statement that increments counter and does nothing.

Change your code as follows:

for (int counter = 0; counter < arraytest.length; ++counter) // No semicolon here.
{
    // this stuff will execute each time through the loop.
    System.out.println(arraytest[counter]);
    // remove this: counter++, as stated by Salah,
    // it is double incrementing the counter variable
}
import java.util.Scanner;
//import java.util.Random;
public class overpower {
public static void main(String args[]){
   Scanner input = new Scanner(System.in);
   //Random numgen = new Random();
   int arraytest[]={1,2,5,8,9};
   //int counter = 0;

for(int counter = 0; counter < arraytest.length; counter++)//; - you don't need this semi-colon
    {// you do need this brace character
    System.out.println(arraytest[counter]);
    // counter++; you don't need to increment the counter if you do it in the for loop.
    }

}

When you write the code, I suggest you use your formatter in your IDE. It would format your code like this.

for(int counter = 0; counter < arraytest.length; counter++);
System.out.println(arraytest[counter]);

The println is not in the loop because you have a ; at the end of the for(). As it is not inside the the counter is out of scope and not available.

Remove the ; at the end of the for() and it should compile.

for any loop, you do not need to put in any semicolon at it's end. Like here you did,

for(int counter = 0; counter < arraytest.length; counter++); // This semicolon doesn't live here{
System.out.println(arraytest[counter]);
}

it'd be better if you make this to:

for(int counter = 0; counter < arraytest.length; counter++) {
System.out.println(arraytest[counter]);

And if you really want the counter to be incremented by 1, then why put the counter++ twice? That doesn't make it an efficient program and moreover it may result in a possible bugged output, because it gets incremented by 2 everytime the loop runs, once while entering and while taking the exit because of the duplicate counter++. Suggest you to remove it unless you require, else if you really require, then instead of placing two counter++ you may put this:

for(int counter = 0; counter < arraytest.length; counter = counter + 2) {
System.out.println(arraytest[counter]);
}

I hope I may have helped you. Thanks!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top