lundi 26 avril 2021

How can I code a program so that when a random string is selected from an array a specific value is produced in java

I am new to coding and I wanted to make a program where when a random string value from an array is selected, it prints out that string as well as an int value that corresponds to it. So if "a" was selected, I want it to print out 1 and if "B" was selected, 2 and so forth. How would I go about doing something like that?

package example;

import java.util.Random;

public class Example {

public static String [] letter = {"a", "b", "c"};
public static int [] number = {1, 2, 3};

public static void main(String[] args) {

Random random = new Random();
int randomLetter = random.nextInt(letter.length);

System.out.println(letter [randomLetter] + number);
}
}



Aucun commentaire:

Enregistrer un commentaire