+ 1

[SOLVED][CharGen] I dont know what i am doing wrong.

Im prolly doing A LOT wrong but it works fine up until i try to assign the Strings based on the random numbers generated. The random Numbers work. and i can output them fine, but when i try to assign a sting a value based on the int random generated, it keeps choosing the last option from each variable. https://code.sololearn.com/ci96xmLBZYc4/#java Update: I took Rrestoring faith's suggestion. but everytime i run it it seems like it is ignoring my use of Switch/Case. when i put it in Eclipse it is giving me CE HUMAN null/null every time but when i try to run it oon here i get Compilation error now i have 6 errors: ..\Playground\:69: error: variable randClass is already defined in method main(String[]) int randClass = (int)(Math.random() * 12); ^ ..\Playground\:70: error: cannot find symbol switch(charClass){ ^ symbol: variable charClass location: class charGen ..\Playground\:70: error: illegal start of type switch(charClass){ ^ ..\Playground\:83: error: variable randClass is already defined in method main(String[]) int randClass = (int)(Math.random() * 12); ^ ..\Playground\:84: error: cannot find symbol switch(charClass){ ^ symbol: variable charClass location: class charGen ..\Playground\:84: error: illegal start of type switch(charClass){ ^

10th Jun 2017, 6:25 PM
oddeofreq
oddeofreq - avatar
3 Respuestas
+ 3
I assume you're using netbeans. You wrote an extra class at the top, above the import. public class CharGen { import java.util.Random; May want to remove that extra class. (Edit: NVM looks like you fixed it before I posted) Random.nextInt(x); The x sets the 'seed' of the random. You can use a Random object to generate numbers between a certain range, but if all you want is the range from 0-x, it may be easier for you to use Math.random(); random() in the Math class with return a random number from 0-1. 1 being exclusive. So you can use it like: int randNum = (int)(Math.random() * 9); This will return a random number from 0-8.
10th Jun 2017, 5:53 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
My bad, I thought I saw 2 classes. The import just needs to be outside the class. So like: import java.util.Random; class Program{ }
10th Jun 2017, 6:09 PM
Rrestoring faith
Rrestoring faith - avatar
0
Solved. a buch of variables got renamed and i didnt rename ALL of them. added Breaks into the Switch statments and i overlookedd redeclaring the same variable more than once
10th Jun 2017, 6:24 PM
oddeofreq
oddeofreq - avatar