Java While Loop & 1D Array



我创建了一个名为teams的字符串数组和一个名为nums的int数组。NUM数组中的每个整数都对应于字符串数组中的团队。

ex:

蒙特利尔加拿大人= 1,芝加哥黑鹰= 2,等等

我需要从1-10中随机选择数字(对应于int [] num),并且必须继续进行此循环,直到整数数组中的每个元素一次调用。含义循环的末端,字符串数组中的每个团队都会在一次调用。这必须通过一段时间循环完成。我似乎无法弄清楚如何精确创建可以做到这一点的循环。

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;

使用列表/向量...

那么,您不再需要随机数,而只需要整理列表

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);
}

this:

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);
}

edit1:如果您不想要团队名称,但是团队编号,只需替换团队 -> nums。

edit2:

导入这些类:

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

首先,如果您希望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++
}

然后,您可以再次循环浏览列表,并且您循环的值将是(psuedo)从1-10中随机。然后,如果需要排序,则可以对列表进行排序。

您可以洗牌,然后与环。

请看一下此解决方案https://stackoverflow.com/a/1520212/814304(只需使用wher-loop而不是for-loop);

如果您的 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();
    }
}

MAIN

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);
    }
}

note i 封闭键盘,您也可以在阅读用户的名字后可以执行此操作。另外,我不会从列表中删除团队,我可以想象您想保留他们进行进一步处理。

输出

= 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

这是NHL(国家曲棍球联盟)中的30支曲棍球队。一些商店的自动售货机可以将微型团队曲棍球冰球分配为Toonie(2美元)。当您放入Toonie时,您永远不会知道会得到哪种冰球;30个冰球中的任何一个都可能与其他机器分配。它们是随机分发的。对于此练习,我们将将团队限制在10。您的工作是编写一个程序,以模拟NHL微型冰球的分配,直到分发10个微型团队曲棍球冰球中的每一个。您的程序应如下以下操作:1.显示一条欢迎消息,并要求用户输入其名称。2.将您最喜欢的曲棍球队名称存储在一系列弦乐中。分配团队名称声明声明直接。3.您的程序应循环(使用时循环),直到每个团队的至少一个微型冰球已经分发了。创建一个大小10的整数阵列,该数组将用作计数器阵列,以跟踪自动售货机分配的每个团队的数量。您需要使用Math.random()函数来随机分配微型冰球。Math.random()方法返回一个带正符号的双值,大于或等于0.0或小于1.0。4.一旦您累积了至少每个冰球中的一个,请显示您必须购买多少个冰球,购买的冰球总数以及个性化消息中的总成本

1-一维数组&amp;loops

最新更新