I am currently trying to learn java, but I do not know all the references inside java for random generation as well as length. I am trying to translate the following javascript/unityscript code to Java for use in android studio.
Origional javascript/unityscript (unity game engine is where i made it)
// Javascript example follows
var characters : String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var code : String = "";
var code1 : String = "";
var code2 : String = "";
var codet : String = "";
var codet1 : String = "";
var gui_result : UI.Text;
function Generate () {
var code : String = "";
var code1 : String = "";
var code2 : String = "";
var codet : String = "";
var codet1 : String = "";
for (var i : int = 0; i < 5; i++) {
var a : int = Random.Range(0, characters.length);
code = code + characters[a];
}
for (var o : int = 0; o < 5; o++) {
var s : int = Random.Range(0, characters.length);
code1 = code1 + characters[s];
codet = code1 + " - " + code;
}
for (var p : int = 0; p < 5; p++) {
var d : int = Random.Range(0, characters.length);
code2 = code2 + characters[d];
codet1 = codet + " - " + code2;
gui_result.text = codet1;
}
}
I understand that code is extremely poorly made, but I understand it because i laid it out to be simple and not efficient.
Here is my attempt at Java code but I got completely lost on where to define my variables and how to call the function I wanted with a button. It would be best if someone could lay it out as straight forward as above so I understand.
Java attempt but fail
public void generate(View view) {
String characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String code = "";
String code1 = "";
String code2 = "";
String codet = "";
String codet1 = "";
for (int i = 0; i < 5; i++){
int a = new Random(0, characters.length());
code = code + characters[a];
}
}
All help is much appreciated!
Aucun commentaire:
Enregistrer un commentaire