How do I transfer the index of a number I received randomly in an array to another array? I'm trying to make an application of some kind of Question&Answer thing and when I call a random question I want to call that answer too. But I don't know how to do it. How can I transfer the index that I called randomly question's to the answer's array?
package javaapplication18;
import java.util.Scanner;
public class JavaApplication18 {
public static void main(String[] args) {
System.out.println("Oyun başlıyor");
String[] kelimeler = new String[] {"bilgisayar","fare","buzdolabi","kitap","otomobil"};
String rastgeleKelime = kelimeler[(int) (Math.random() * kelimeler.length)];
System.out.println("Sorulan kelime "+ rastgeleKelime.length()+" harfli.");
char[] harfler = new char[rastgeleKelime.length()];
for(int i = 0; i<harfler.length; i++){
harfler[i] = '.';
}
int haklar = 3;
Scanner scanner = new Scanner(System.in);
while(haklar > 0){
System.out.print("Kalan hak: ");
for(int i = 0; i < haklar; i++){
System.out.print("X");
}
System.out.println();
System.out.print("Tahmin: ");
String input = scanner.nextLine();
char harf = input.charAt(0);
boolean tahmin = false;
for(int i = 0; i < rastgeleKelime.length(); i++){
char l = rastgeleKelime.charAt(i);
if(l == harf){
harfler[i] = l;
tahmin = true;
}
}
if(!tahmin){
haklar = haklar-1;
}
boolean oyunsonu = true;
System.out.print("Kelime: ");
for(int i = 0; i < harfler.length; i++){
if(harfler[i] == '.'){
oyunsonu = false;
}
System.out.print(harfler[i]);
}
System.out.println();
System.out.println("----------------------");
if(oyunsonu){
System.out.println("Tebrikler. Kazandınız!");
break;
}
if(haklar == 0){
System.out.println("Kaybettiniz! Kelime: "+ rastgeleKelime);
}
}
System.out.println("Oyun bitti");
}
}
I did it. I created rnd object from using Random library. And then I assigned rnd to an "a" object and then I called this object in my Array so it worked. Thank you for your supports.
public static void main(String[] args) {
System.out.println("Oyun başlıyor");
String[] cümleler = new String[] {"Elephant", "Deer", "Fish", "Crocodile", "Cat"};
String[] kelimeler = new String[] {"bilgisayar","fare","buzdolabi","kitap","otomobil"};
Random random =new Random();
int a=random.nextInt(5);
String rastgeleKelime = kelimeler[a];
String rastgeleCümle = cümleler[a];
System.out.println(rastgeleCümle);
System.out.println("Sorulan kelime "+ rastgeleKelime.length()+" harfli.");
char[] harfler = new char[rastgeleKelime.length()];
for(int i = 0; i<harfler.length; i++){
harfler[i] = '.';
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.