im writing a program where I have to have the computer randomly choose 1 of 10 objects that i have written down as a string array... im using the the math.random function to come up with a number...
即时编写一个程序,我必须让计算机随机选择10个对象中的一个,我已经写下来作为一个字符串数组...我使用math.random函数得出一个数字...
int targetNumber = (int) (Math.random() * 10);
System.out.println("I'm thinking of an item, I will only choose one of 10...");
how do i link that randomly generated int to a string inside a string array with ten different items inside... first time doing java and a pretty big noob, keep failing at this part
我如何将随机生成的int链接到一个字符串数组中的字符串,里面有十个不同的项目...第一次做java和一个相当大的菜鸟,在这部分继续失败
public static String getElement(int x){
String[] stringArray = new String[10];
stringArray[0] = "Gold";
stringArray[1] = "Barnacle";
stringArray[2] = "Wenches";
stringArray[3] = "Wooden Leg";
stringArray[4] = "Davey Jones Locker";
stringArray[5] = "Keira Knightley";
stringArray[6] = "Capt. Sparrow's Sword";
stringArray[7] = "The Black Pearl";
stringArray[8] = "Davey Jones Heart";
stringArray[9] = "Diamonds";
return stringArray[x];
}
its pirates of the caribean themed... school work
它的海盗以caribean为主题......学校工作
3 个解决方案
#1
3
With the code you posted, you can just do:
使用您发布的代码,您可以:
int targetNumber = (int) (Math.random() * 10);
System.out.println("I'm thinking of an item, I will only choose one of 10...");
System.out.println(getElement(targetNumber));
#2
1
You want to select one of the Strings. This is done by addressing the String array's index. Target number will generate that index, so what you do is stringArray[targetNumber]
. This will return the String at the designated index.
您想要选择其中一个字符串。这是通过寻址String数组的索引来完成的。目标号码将生成该索引,因此您所做的是stringArray [targetNumber]。这将返回指定索引处的String。
If targetNumber is 3
, "Wooden leg" will be selected.
如果targetNumber为3,将选择“Wooden leg”。
Try it with System.out.println(getElement(targetNumber));
尝试使用System.out.println(getElement(targetNumber));
Hope that helps.
希望有所帮助。
#3
0
You can use stringArray[targetNumber]
to get the string "pointed to" by the random number.
您可以使用stringArray [targetNumber]来获取随机数字符串“指向”的字符串。
#1
3
With the code you posted, you can just do:
使用您发布的代码,您可以:
int targetNumber = (int) (Math.random() * 10);
System.out.println("I'm thinking of an item, I will only choose one of 10...");
System.out.println(getElement(targetNumber));
#2
1
You want to select one of the Strings. This is done by addressing the String array's index. Target number will generate that index, so what you do is stringArray[targetNumber]
. This will return the String at the designated index.
您想要选择其中一个字符串。这是通过寻址String数组的索引来完成的。目标号码将生成该索引,因此您所做的是stringArray [targetNumber]。这将返回指定索引处的String。
If targetNumber is 3
, "Wooden leg" will be selected.
如果targetNumber为3,将选择“Wooden leg”。
Try it with System.out.println(getElement(targetNumber));
尝试使用System.out.println(getElement(targetNumber));
Hope that helps.
希望有所帮助。
#3
0
You can use stringArray[targetNumber]
to get the string "pointed to" by the random number.
您可以使用stringArray [targetNumber]来获取随机数字符串“指向”的字符串。