尝试修改游戏,以便程序保留一个未接受的答案列表并在最后打印它们。 (Java)的

时间:2013-11-20 14:33:32

标签: java arraylist

我正在尝试编写一个程序野餐游戏,其中列出了被拒绝的所有项目(但每次只发生一次|如果用户两次输入项目并且两次被拒绝,它应该出现在被拒绝的项目列表中只有一次)。在游戏结束时,打印出所有被拒绝的项目以及用户答案被拒绝的次数。我应该使用数组列表。我该怎么办?这是我到目前为止所拥有的。

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

    }
}

和继承人相应的野餐班

import java.util.*;

public class Picnic
{
    // INSTANCE VARIABLES:
    private ArrayList<String> stuffToBring; // items to bring on the picnic

    // CONSTRUCTOR:

    //-----------------------------------------------------
    // Construct a new Picnic.
    //-----------------------------------------------------
    public Picnic()
    {
        stuffToBring = new ArrayList<String>(); // 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 < 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();
    }
}

3 个答案:

答案 0 :(得分:1)

我不确定我是否理解了您的问题,但似乎很容易:添加ArrayList<String>并在item内插入else while。然后,在picnic.show()之后,您只需打印ArrayList

以下是代码:

Scanner scan = new Scanner(System.in);
Picnic picnic = new Picnic();
ArrayList<String> unaccepted = new ArrayList<>();

while (picnic.numberOfItems() < 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
    {
        if(!unaccepted.contains(item)) unaccepted.add(item);
        System.out.println("Sorry, you can't bring " + item);
    }
}

System.out.println("\nHere's what we'll have at the picnic:");
picnic.show();
System.out.println(Arrays.toString(unaccepted.toArray()));

答案 1 :(得分:0)

创建数组列表

 ArrayList<String> rejectedItems = new ArrayList<String>();

拒绝优惠时

System.out.println("Sorry, you can't bring " + item);

您将该项目添加到被拒绝项目列表

System.out.println("Sorry, you can't bring " + item);
rejectedItems.add(item);

如果您需要使用ArrayList,此时您还需要遍历列表并确保您添加的项目不存在,因为ArrayLists允许重复项。

最后,将它们全部打印出来

for (String item : rejectedItems)
{
   System.out.println("Rejected " + item);
}

答案 2 :(得分:0)

您可以使用另一个ArrayList来跟踪未接受的项目。 由于您没有将重复项添加到未接受的ArrayList,因此您还应该跟踪错误答案的数量。 这是一个非常基本的解决方案:

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

public class PlayPicnic {
   public static void main(String[] args) {

      Scanner scan = new Scanner(System.in);
      Picnic picnic = new Picnic();
      ArrayList<String> unaccepted = new ArrayList<String>();
      int wrongAttempts = 0;

      while (picnic.numberOfItems() < 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 {
            wrongAttempts++;
            if (!unaccepted.contains(item)) {
               unaccepted.add(item);
            }
            System.out.println("Sorry, you can't bring " + item);
         }
      }

      System.out.println("\nHere's what we'll have at the picnic:");
      picnic.show();
      System.out.println("Unaccepted items");
      for (String item : unaccepted) {
         System.out.println(item);
      }
      System.out.println("Your answer was rejected " + wrongAttempts + " times");

   }
}