Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to modify a game so that the program keeps a list of unaccepted answers and prints them at the end. (java)
    text
    copied!<p>I'm trying to write a program a picnic game that keeps a list of all the items that were rejected (but only one occurrence of each|if the user enters an item twice and it is rejected both times, it should appear only once in the list of rejected items). At the end of the game, prints out all the rejected items and the number of times the user's answer was rejected. I'm supposed to use an array list. what should I do? here's what I have so far. </p> <pre><code>import java.util.*; public class PlayPicnic { public static void main(String[] args) { Scanner scan = new Scanner(System.in); Picnic picnic = new Picnic(); while (picnic.numberOfItems() &lt; 5) { System.out.print("What do you want to bring on the picnic? "); String item = scan.nextLine(); if (picnic.okayToBring(item)) { picnic.add(item); } else { System.out.println("Sorry, you can't bring " + item); } } System.out.println("\nHere's what we'll have at the picnic:"); picnic.show(); } } </code></pre> <p>and heres the corresponding picnic class</p> <pre><code>import java.util.*; public class Picnic { // INSTANCE VARIABLES: private ArrayList&lt;String&gt; stuffToBring; // items to bring on the picnic // CONSTRUCTOR: //----------------------------------------------------- // Construct a new Picnic. //----------------------------------------------------- public Picnic() { stuffToBring = new ArrayList&lt;String&gt;(); // initialize list } //----------------------------------------------------- // Given an item s, see if it's okay to add it to the list. // Return true if it is, false otherwise: //----------------------------------------------------- public boolean okayToBring(String s) { // "Secret rule" -- s can't be an item already in the list: if (stuffToBring.contains(s)) // "contains" is in the ArrayList class { return false; } else { return true; } } //----------------------------------------------------- // Given an item s, add it to the list (if it's okay to add it) //----------------------------------------------------- public void add(String s) { if (okayToBring(s)) { stuffToBring.add(s); } } //----------------------------------------------------- // Print the items in the list //----------------------------------------------------- public void show() { for (int i = 0; i &lt; stuffToBring.size(); i++) { String s = stuffToBring.get(i); System.out.println(s); } // ALTERNATE APPROACH USING A "for next" LOOP: // for (String s: stuffToBring) // { // System.out.println(s); // } } //----------------------------------------------------- // Returns the number of items in the list: //----------------------------------------------------- public int numberOfItems() { return stuffToBring.size(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload