본문 바로가기

코딩/Java, SpringBoot

[JAVA]variable is already defined in method main

https://stackoverflow.com/questions/23148347/java-variable-is-already-defined-in-method

 

Java: Variable is already defined in method

I am trying to make a program which makes a random number between 1000-9999 with no repeating numbers. The rules are I have to label the random number as a String, and then use a while loop to figu...

stackoverflow.com

You've already define your variable answer2 in line 2, and in your while loop you are trying to redefine the variable answer2, i.e:int answer2 = (int)answer1;

You can do it like :

answer2 = (int)answer1;

 

앞에 type을 붙이면 새로 선언한다는 의미가 됌!