我創建了一個名爲「球隊」的字符串數組和一個名爲nums的int數組。 num數組中的每個整數對應於字符串數組中的一個組。Java while循環和1D數組
例:
蒙特利爾加拿大人= 1,Chicagao黑鷹= 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();':-) – mcuenez