java - Loop not recognizing variables previously defined -
i'm working on guessing game uses arrays store both names of players , guesses. i'm new arrays, plan user input array them enter amount of people playing, set variable , use loop keep asking names until reached necessary amount of names stated number of players. however, running simple problem loop. here's small bit of code far:
public class guessgame { int w = 0; int[] players = new int[100]; string[] playernames = new string[100]; string numstart = joptionpane.showinputdialog("how many players?"); int j = integer.parseint(numstart); while (w <= j) { string name = joptionpane.showinputdialog("what name?"); playernames[w] = name; w++; } the problem is, i'm getting error regarding variables in loop, w , j. error statement says effect of cannot find symbols class w or class j. don't intend them classes, , i've run similar code in other projects without hitch, don't know what's going wrong here. i'm sure it's stupidly simple, *'ve been stuck @ wall time , can't progress until sorted. part of project 3 separate classes. class posted here, player class, , tester class, main method. had whole thing working in more simplified form earlier, need adjust actual player input , arrays. regardless, tester class supposed main class. using netbeans if matters. thank you. here other 2 classes reference:
package guessgame; public class gamelauncher { public static void main(string[] args) { guessgame game = new guessgame(); game.startgame(); } } and
package guessgame; import java.util.random; public class player { int number = 0; //where guess goes string name; public void guess() { random r = new random(); number = 1 + r.nextint(21); system.out.println("i'm guessing " + number); } }
all code needs in method. cannot have except variable declarations @ class level. move method, example public static void main(string[] args) main method.
public class guessgame { public static void main (string[] args) { int w = 0; int[] players = new int[100]; string[] playernames = new string[100]; string numstart = joptionpane.showinputdialog("how many players?"); int j = integer.parseint(numstart); while (w <= j) { string name = joptionpane.showinputdialog("what name?"); playernames[w] = name; w++; } } }
Comments
Post a Comment