2017-03-06 70 views
2

我創建了一個名爲「球隊」的字符串數組和一個名爲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; 
+0

不要忘了叫'keyboard.close();':-) – mcuenez

回答

1

使用列表/代替矢量...

那麼你並不需要一個隨機數了,只是洗牌列表

List<String> teams = new Vector<>(Arrays.asList("Montreal Canadiens", "Chicago Blackhawks", "Boston Bruins", 
    "Toronto Maple Leafs", "Vancouver Canucks", "Ottawa Senators", "Pittsburgh Penguins", "Calgary Flames", 
    "New York Rangers", "Edmonton Oilers")); 
int ts = teams.size(); 
for (int i = 0; i < ts; i++) { 
    System.out.println(teams.remove(0)); 
    Collections.shuffle(teams); 
} 
+0

同一個團隊會不會有多次打印的機會,因此其他人根本沒有機會? – mcuenez

+1

是的...你需要刪除元素.... –

1

此:

List<String> teamsList = new ArrayList<String>(Arrays.asList(teams)); 
while(!teamsList.isEmpty()){ 
    int randomNum = (int)(Math.random()*teamsList.size()); 
    String team = teamsList.remove(randomNum); 
} 

或者:

List<String> teamsList = new ArrayList<String>(Arrays.asList(teams)); 
Collections.shuffle(teamsList); 
while(!teamsList.isEmpty()){ 
    String team = teamsList.remove(0); 
} 

編輯1: 如果您不想要球隊名稱,但球隊號碼,只需替換球隊 - >號碼。

EDIT2:

導入這些類:

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 
+0

謝謝你的時間。當我嘗試使用你提出的建議時,我在第一行出現了多個錯誤(列表 ....)「ArrayList無法解析爲變量」,「列表無法解析爲變量」,「陣列不能解析已解決「 – peanut

+0

歡迎您=) 必須爲這些類添加進口(請參閱我更新的帖子) – Screwb

0

首先,如果你想在nums值對應於值teams,你會想要的值開始在0和9結束,這樣的數字對應的團隊指數。

如果你想手動做到這一點,我會建議在那裏您將隨機選擇的值到前面,像這樣一個循環:

int i = 0; 
while(i<nums.length){ 
    int randomIndex = i + (int)Math.random*(nums.length-i); 
    int temp = nums[i]; 
    nums[i] = nums[randomIndex]; 
    nums[randomIndex] = temp; 
    i++ 
} 

然後,您可以再次通過列表循環,您循環的值將從1-10開始隨機(psuedo)。如果您需要排序,您可以對列表進行排序。

0

如果你的nums數組中的數字應該是索引,我想其他任何答案都可以正常工作。舉例來說,我的解釋是,你的問題應該是這樣的,也可能是其他任何數字。

在這種情況下,我會創建一個Team類,如下所示。

public class Team { 
    private static final String DELIMITER = "/"; 

    private String name; 
    private int number; 

    public Team(String name, int number) { 
     this.name = name; 
     this.number = number; 
    } 

    public String getName(){ 
     return this.name; 
    } 

    public int getNumber(){ 
     return this.number; 
    } 

    @Override 
    public String toString(){ 
     return this.getName() + DELIMITER + this.getNumber(); 
    } 
} 

主要

import java.util.ArrayList; 
import java.util.List; 
import java.util.Scanner; 

public class Main { 
    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."); 

     List<Team> teams = new ArrayList<>(); 
     teams.add(new Team("Montreal Canadiens", 1)); 
     teams.add(new Team("Chicago Blackhawks", 2)); 
     teams.add(new Team("Boston Bruins", 3)); 
     teams.add(new Team("Toronto Maple Leafs", 4)); 
     teams.add(new Team("Vancouver Canucks", 5)); 
     teams.add(new Team("Ottawa Senators", 6)); 
     teams.add(new Team("Pittsburgh Penguins", 7)); 
     teams.add(new Team("Calgary Flames", 8)); 
     teams.add(new Team("New York Rangers", 9)); 
     teams.add(new Team("Edmonton Oilers", 10)); 

     List<Team> visitedTeams = new ArrayList<>(); 

     while (teams.size() > visitedTeams .size()) { 
      int randomNum = (int) (Math.random() * teams.size()); 
      Team team = teams.get(randomNum); 

      if (!visitedTeams.contains(team)) { 
       visitedTeams.add(team); 
      } 
     } 

     // Close your scanner 
     keyboard.close(); 

     System.out.println("Teams called: "); 
     visitedTeams.forEach(System.out::println); 
    } 
} 

注意關閉的鍵盤,這你可以讀取用戶的名稱,以及之後做。另外,我不會從名單中刪除這些球隊,因爲我可以想象你想保留這些球隊以便進一步處理。

輸出

= 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 
= 0              0 = 
= 0  NHL Miniature Hockey Puck Vending Machine  0 = 
= 0              0 = 
= 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 


Hello, what is your first name? 
MyName 
Welcome MyName! Let's see how much money you will need to spend to get all of the pucks. 
Teams called: 
Chicago Blackhawks/2 
Toronto Maple Leafs/4 
Edmonton Oilers/10 
Boston Bruins/3 
Ottawa Senators/6 
Calgary Flames/8 
Vancouver Canucks/5 
Pittsburgh Penguins/7 
Montreal Canadiens/1 
New York Rangers/9 
0

這裏是在NHL(國家冰球聯盟)30個曲棍球隊。一些商店有自動售貨機,可以爲每個托盤(2美元)分配小型曲棍球冰球。當你放入一個toonie時,你永遠不知道你會得到哪個冰球; 30個團隊中的任何一個與其他人一樣可能會被機器分配。他們隨機發放。 對於這次練習,我們將限制隊員數量爲10人。 你的工作是編寫一個程序來模擬NHL微型卡盤的分配,直到每10個小型曲棍球卡盤中的一個被分配。 程序應按以下步驟操作: 1.顯示一條歡迎消息並要求用戶輸入其名稱。 2.將您最喜歡的10支曲棍球隊的名字存儲在String數組中。直接在 聲明聲明中指定團隊名稱。 3.你的程序應該循環(使用一個while循環),直到每個團隊的至少一個微型冰球被分配。創建一個大小爲10的整數數組,它將作爲一個計數器陣列來跟蹤自動售貨機分配的每個團隊盤的數量。您將需要使用Math.random()函數隨機分配一個微型冰球。 Math.random()方法返回帶有正號的double值,大於或等於0.0且小於1.0。 4.一旦每個冰球至少一個積累,顯示有多少每支球隊冰球,你必須購買,購買圓盤的總數和在個性化消息的總成本

1 - 一個維數組& while loops