gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:49:11 25 4
gpt4 key购买 nike

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

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

}
}

这是相应的野餐类(class)

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

最佳答案

我不确定我是否理解你的问题,但这似乎很简单:添加 ArrayList<String>并插入 itemelse里面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()));

关于java - 尝试修改游戏,以便程序保留未接受的答案列表并在最后打印它们。 (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20098726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com