我创建了一个名为teams的字符串数组和一个名为nums的int数组。 num数组中的每个整数对应于字符串数组中的一个团队。
前:
Montreal Canadiens = 1,Chicagao Blackhawks = 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 :(得分: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);
}
答案 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);
}
EDIT1: 如果您不想要团队名称,但团队编号,只需要替换团队 - &gt; NUMS。
EDIT2:
导入这些类:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
答案 2 :(得分: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开始随机(伪)。然后,您可以根据需要对列表进行排序。
答案 3 :(得分:0)
您可以随机播放数组,然后逐个循环播放。
看看这个解决方案np.logical_and
(只使用while循环而不是for循环);
答案 4 :(得分: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
答案 5 :(得分: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 - 一维阵列&amp; while循环