【发布时间】:2017-03-06 20:52:50
【问题描述】:
我创建了一个名为 teams 的字符串数组和一个名为 nums 的 int 数组。 num 数组中的每个整数对应于字符串数组中的一个团队。
例如:
蒙特利尔加拿大人队 = 1,芝加哥黑鹰队 = 2,等等。
我需要从 1-10 中随机选择数字(对应于 int[] num),并且这个循环必须继续,直到整数数组中的每个元素都被调用一次。这意味着在循环结束时,字符串数组中的每个团队都会被调用一次。这必须通过一个while循环来完成。我似乎无法弄清楚如何准确地创建一个可以做到这一点的循环。
import java.util.Scanner;
public class Question1 {
public static void main(String[] args) {
//declare scanner
Scanner keyboard= new Scanner (System.in);
//display opening message
System.out.println("= 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 =");
System.out.println("= 0 0 =");
System.out.println("= 0 NHL Miniature Hockey Puck Vending Machine 0 =");
System.out.println("= 0 0 =");
System.out.println("= 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 =");
System.out.println("");
System.out.println("");
System.out.println("Hello, what is your first name? ");
//read user input
String name = keyboard.nextLine();
//Welcome message
System.out.println("Welcome " + name + "! Let's see how much money you will need to spend to get all of the pucks.");
//declaring 10 teams in a 1D array
String[] teams = {"Montreal Canadiens","Chicago Blackhawks","Boston Bruins","Toronto Maple Leafs","Vancouver Canucks","Ottawa Senators","Pittsburgh Penguins","Calgary Flames","New York Rangers","Edmonton Oilers"};
int[] nums = {1,2,3,4,5,6,7,8,9,10};
//random number from 1-10
while (
int RandomNum = (int)(Math.random()*10)+1;
【问题讨论】:
-
别忘了打电话给
keyboard.close();:-)