2

I have created a string array called teams and an int array called nums. Each integer in the num array corresponds to a team in the string array.

Ex:

Montreal Canadiens = 1, Chicagao Blackhawks = 2, etc.

I need to randomly pick numbers from 1-10 (corresponding to int[] num) and this loop must continue until each element in the integer array is called once. Meaning byt the end of the loop, each team in the string array is called on once. This must be done through a while loop. I can't seem to figure out how to exactly create a loop that would do this.

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;
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
peanut
  • 27
  • 6

6 Answers6

1

Use a list/vector instead...

then you don't need a random number anymore, only shuffling the list

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);
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • Isn't there a chance the same team would get printed more than once and, thus, others not at all? – mcuenez Mar 06 '17 at 21:25
1

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

Or:

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

Edit1: If you don't want the team name, but the team number, just replace teams -> nums.

Edit2:

Import those classes:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Screwb
  • 28
  • 6
  • Thank you for your time. When I try to use what you have proposed, I get multiple error on the first line (list....) "ArrayList cannot be resolved to a variable", "List cannot be resolved to a variable", "Arrays cannot be resolved" – peanut Mar 06 '17 at 21:39
  • You welcome =) Imports have to be added for those classes (see my updated post) – Screwb Mar 06 '17 at 21:43
0

First of all, if you want the values in nums to correspond to the values in teams, you're going to want the values to start at 0 and end at 9, so that way the numbers correspond to the indices of the teams.

If you want to do this manually, I would suggest a loop where you move the randomly selected value up to the front, like so:

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++
}

Then, you can loop through the list again, and the values that you loop through will be (psuedo)random from 1-10. You can then sort the list if you need it sorted.

Lavaman65
  • 863
  • 1
  • 12
  • 22
0

You can shuffle your array and then go one by one with while-loop.

Take a look on this solution https://stackoverflow.com/a/1520212/814304 (just use while-loop instead of for-loop);

Community
  • 1
  • 1
iMysak
  • 2,170
  • 1
  • 22
  • 35
0

If the numbers in your nums array are supposed to be indexes, I guess, any other answer would work just fine. I, for one, did interpret your question as so your numbers are supposed to be that way and might be any other number as well.

In this case I'd create a Team class as seen below.

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 that I closed the keyboard, which you could do after reading the user's name as well. Also, I do not remove the teams from the list, as I can imagine you would like to keep them for further processing.

Output

= 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
mcuenez
  • 1,579
  • 2
  • 20
  • 28
0

here are 30 hockey teams in the NHL (National Hockey League). Some stores have vending machines that dispense miniature team hockey pucks for a toonie ($2) each. When you put in a toonie you never know which puck you will get; any one of the 30 team pucks is as likely as any other to be dispensed by the machine. They are given out at random. For this exercise we will limit the teams to 10. Your job is to write a program to simulate the dispensing of NHL miniature pucks until one of each 10 miniature team hockey pucks is dispensed. Your program should proceed as follows: 1. Display a welcome message and ask the user to enter their name. 2. Store your 10 favorite hockey teams’ name in an array of String. Assign the team names in the declaration statement directly. 3. Your program should loop(use a while loop) until at least one miniature puck of each team has been dispensed. Create an integer array of size 10, which will serve as a counter array to keep track of the number of each team puck dispensed by the vending machine. You will need to use the Math.random()function to randomly dispense a miniature puck. The Math.random() method returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. 4. Once you have accumulated at least one of each puck, display how many of each team puck you had to purchase, the total number of pucks purchased and the total cost in a personalized message

1 - One dimensional array & while loops

Mansi Edi
  • 1
  • 1