java - 为什么我的 isEmpty() 方法读取的不是空的?
问题描述
我在驱动程序中制作了两个框,一个带有名称,另一个带有数字。他们随机选择一个数字的名称并将其打印出来。如果名称框为空(真或假),它也会打印出来,但在第 5 个名称读出名称后,它也应该读出该框为空。相反,它会导致错误,因为所有框都是空的。我的 isEmpty 方法有什么问题?另外,我可以让它忽略错误,而是让它打印出“盒子是空的”吗?
司机
import java.util.*;
public class DrawTester
{
/** two boxes within drawTester
* box of names (5)
* box of places at a table (5)
*/
public static void main ( String[] args)
{
String code;
Draw<String> myStringBox = new Draw<>();
Draw<Integer> myIntegerBox = new Draw<>(1, 2, 3, 4, 5);
//scanner to draw when user requests
Scanner scan = new Scanner(System.in);
System.out.print("Let's draw some names. Press 'd' to draw. ('q' to stop)");
code = scan.nextLine();
if (!code.equals("q"))
{
if (code.equals("d"))
{
myStringBox = new Draw<>("Happy", "Lucky", "Freddy", "Muddy", "Fuddy");
myIntegerBox = new Draw<>(1, 2, 3, 4, 5);
for (int index = 0; index < 10 ; index++) {
String person = myStringBox.drawItem();
int seat = myIntegerBox.drawItem();
System.out.println(String.format("%s will occupy seat %s.",
person, seat));
boolean isEmpty = myStringBox.isEmpty();
System.out.println("The box is empty: " + isEmpty);
System.out.print("Enter 'd' to make draw. ('q' to stop)");
code = scan.nextLine();
if (code.equals("q")){
System.out.println("you quit");
break;
}
}
}
} System.out.println("You pressed 'q'");
}
}
绘图类
import java.util.*;
@SuppressWarnings("unchecked")
public class Draw<T>
{
boolean isEmpty = true;
Random random = new Random();
List<T> items;
public Draw(List<T> items) {
this.items = items;
}
public Draw(T...items) {
this(new ArrayList<>(Arrays.asList(items)));
}
public T drawItem() {
if (items.size() > 0) {
int index = random.nextInt(items.size());
return items.remove(index);
} else return null;
}
public boolean isEmpty() {
if (items.size() <= 0) {
isEmpty = true;
} return isEmpty = false;
}
public String toString() {
return items.toString();
}
}
解决方案
您的方法将 false 分配给isEmpty
,因此始终将其返回为false
. 将其更改为更简单的内容,如下所示:
public boolean isEmpty() {
return items.size() == 0;
}
或者正如@chrylis-cautiouslyoptimistic- 所建议的那样(感谢您的提示):
public boolean isEmpty() {
return items.isEmpty();
}
关于你的第二个问题,你需要添加一个 if 条件来检查myIntegerBox
在尝试绘制项目之前是否也是空的:
public class DrawTester {
public static void main ( String[] args)
{
String code;
Draw<String> myStringBox = new Draw<>();
Draw<Integer> myIntegerBox = new Draw<>(1, 2, 3, 4, 5);
//scanner to draw when user requests
Scanner scan = new Scanner(System.in);
System.out.print("Let's draw some names. Press 'd' to draw. ('q' to stop)");
code = scan.nextLine();
if (!code.equals("q"))
{
if (code.equals("d"))
{
myStringBox = new Draw<>("Happy", "Lucky", "Freddy", "Muddy", "Fuddy");
myIntegerBox = new Draw<>(1, 2, 3, 4, 5);
for (int index = 0; index < 10 ; index++) {
String person = myStringBox.drawItem();
if (!myIntegerBox.isEmpty()) {
int seat = myIntegerBox.drawItem();
System.out.println(String.format("%s will occupy seat %s.",
person, seat));
boolean isEmpty = myStringBox.isEmpty();
System.out.println("The box is empty: " + isEmpty);
System.out.print("Enter 'd' to make draw. ('q' to stop)");
code = scan.nextLine();
if (code.equals("q")) {
System.out.println("you quit");
break;
}
} else {
System.out.println("The box is empty");
}
}
}
} System.out.println("You pressed 'q'");
}
}
不过,我认为代码中还有一些需要改进的地方。例如,为什么要使用带有条件的 for 循环index < 10
?为什么是10?