简体   繁体   English

为什么我的 isEmpty() 方法不是空的?

[英]Why isn't my isEmpty() method reading as empty?

I have made two boxes within the Driver, one with names and the others with numbers.我在 Driver 中做了两个盒子,一个是名字,另一个是数字。 They randomly choose a name to a number and print it out.他们随机为一个数字选择一个名称并将其打印出来。 It also prints out if the box of names is empty (true or false) but on the 5th name after it reads out the name it should also read out the box is empty.如果名称框为空(真或假),它也会打印出来,但在读出名称后的第 5 个名称时,它也应该读出框为空。 Instead, it causes an error because all of the boxes are empty.相反,它会导致错误,因为所有的框都是空的。 What is wrong with my isEmpty method?我的 isEmpty 方法有什么问题? Also, can I get it to ignore the error and instead get it to print out 'The box is empty'?另外,我可以让它忽略错误,而是让它打印出“盒子是空的”吗?

Driver司机

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

Draw class画画班

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

Your method is assigning false to isEmpty and thus always returning it as false .您的方法将 false 分配给isEmpty ,因此始终将其返回为false Change it to something simpler as follows:将其更改为更简单的内容,如下所示:

public boolean isEmpty() {
    return items.size() == 0;
}

Or as suggested below by @chrylis-cautiouslyoptimistic- (thanks for the hint):或者按照@chrylis-cautiouslyoptimistic- 的建议(感谢提示):

public boolean isEmpty() {
    return items.isEmpty();
}

Regarding your second question, you will need to add an if condition to check if the myIntegerBox is also empty before trying to draw an item:关于第二个问题,在尝试绘制项目之前,您需要添加一个 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'");
    }
}

Still, I think there are some things to improve in the code.不过,我认为代码中有一些需要改进的地方。 For example, why a for loop with the condition index < 10 ?例如,为什么使用条件index < 10的 for 循环? Why 10?为什么是10?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM