java - 为什么这继续要求字符串?

标签 java stack

我正在开发一个旨在帮助学生研究总统的项目。我正在使用堆栈。它应该要求用户输入,然后将输入与堆栈顶部进行比较。如果正确,它将删除顶部项目。否则它会要求下一任总统。当我读到最后时,它再次要求下一任总统,即使堆栈应该是空的。

这是我的主程序。

package namepresidents;
import java.util.Scanner;

public class NamePresidents 
{

    //=========================MAIN=============================================
    public static void main(String[] args) 
    {
       UnboundedStackInterface<String> presidents;
       presidents = new LinkedStack<String>();

       presidents.push("George Washington");
       presidents.push("John Adams");
       presidents.push("Thomas Jefferson");
       presidents.push("James Madison");
       presidents.push("James Monroe");
       presidents.push("John Quincy Adams");
       presidents.push("Andrew Jackson");
       presidents.push("Martin Van Buren");
       presidents.push("James Madison");
       presidents.push("William Henry Harrison");
       presidents.push("John Tyler");
       presidents.push("James K Polk");
       presidents.push("Zachary Taylor");
       presidents.push("James Madison");
       presidents.push("Millard Fillmore");
       presidents.push("Franklin Pierce");
       presidents.push("James Buchanan");
       presidents.push("Abraham Lincoln");
       presidents.push("Andrew Johnson");
       presidents.push("Ulysses S Grant");
       presidents.push("Rutherford B. Hayes");
       presidents.push("James A Garfield");
       presidents.push("Chester A Arthur ");
       presidents.push("Grover Cleveland");
       presidents.push("Benjamin Harrison");
       presidents.push("Grover Cleveland");
       presidents.push("William McKinley");
       presidents.push("William H Taft");
       presidents.push("Woodrow Wilson");
       presidents.push("Warren G Harding");
       presidents.push("Calvin Coolidge");
       presidents.push("Herbert Hoover");
       presidents.push("Franklin D Roosevelt");
       presidents.push("Harry S Truman");
       presidents.push("Dwight D Eisenhower");
       presidents.push("John F Kennedy");
       presidents.push("Lyndon B Johnson");
       presidents.push("Richard M Nixon");
       presidents.push("Gerald R Ford");
       presidents.push("Jimmy Carter");
       presidents.push("Ronald Reagan");
       presidents.push("George Bush");
       presidents.push("Bill Clinton");
       presidents.push("George W Bush");
       presidents.push("Barack Obama");

       UnboundedStackInterface<String> wrongAnswer;
       wrongAnswer = new LinkedStack<String>();


       String menu = "Would you like to study: \n"
                + "1. All the presidents \n"
                + "2. The first half \n"
                + "3. The second half \n"
                + "4. In reverse \n"
                + "0. Exit \n";

        System.out.print(menu);
        Scanner in = new Scanner(System.in);

        int option = in.nextInt();  
        String studentPresidents = in.nextLine();

        switch(option)
        {
            case 1:                 
                        do
                        {
                            System.out.print("Enter the next president: ");
                            studentPresidents = in.nextLine();
                            if(studentPresidents.equalsIgnoreCase(presidents.top()))
                            {
                                presidents.pop();
                            }
                            else
                            {
                                wrongAnswer.push(studentPresidents);
                                System.out.println("That is not correct. Try Again!");
                            }
                        }while(!presidents.isEmpty());

                            do
                            {
                                System.out.print("You missed: \n" + wrongAnswer.top());
                                wrongAnswer.pop();
                            }while(!wrongAnswer.isEmpty());

            break;

            case 2: UnboundedStackInterface<String> firstHalf;
                    firstHalf = new LinkedStack<String>();

                    firstHalf.push("George Washington");
                    firstHalf.push("John Adams");
                    firstHalf.push("Thomas Jefferson");
                    firstHalf.push("James Madison");
                    firstHalf.push("James Monroe");
                    firstHalf.push("John Quincy Adams");
                    firstHalf.push("Andrew Jackson");
                    firstHalf.push("Martin Van Buren");
                    firstHalf.push("James Madison");
                    firstHalf.push("William Henry Harrison");
                    firstHalf.push("John Tyler");
                    firstHalf.push("James K Polk");
                    firstHalf.push("Zachary Taylor");
                    firstHalf.push("James Madison");
                    firstHalf.push("Millard Fillmore");
                    firstHalf.push("Franklin Pierce");
                    firstHalf.push("James Buchanan");
                    firstHalf.push("Abraham Lincoln");
                    firstHalf.push("Andrew Johnson");
                    firstHalf.push("Ulysses S Grant");
                    firstHalf.push("Rutherford B Hayes");
                    firstHalf.push("James A Garfield");

                             do
                             {
                                 System.out.print("Enter the next president: ");
                                 studentPresidents = in.nextLine();
                                 if(studentPresidents.equalsIgnoreCase(firstHalf.top()))
                                 {
                                     firstHalf.pop();
                                 }
                                 else
                                 {
                                     wrongAnswer.push(studentPresidents);
                                     System.out.println("That is not correct. Try Again!");
                                 }
                              }while(!presidents.isEmpty());

                            do
                            {
                                System.out.print("You missed: \n" + wrongAnswer.top());
                                wrongAnswer.pop();
                            }while(!wrongAnswer.isEmpty());

                 break;

            case 3: UnboundedStackInterface<String> lastHalf;
                    lastHalf = new LinkedStack<String>(); 

                    lastHalf.push("Chester A Arthur");
                    lastHalf.push("Grover Cleveland");
                    lastHalf.push("Benjamin Harrison");
                    lastHalf.push("Grover Cleveland");
                    lastHalf.push("William McKinley");
                    lastHalf.push("William H Taft");
                    lastHalf.push("Woodrow Wilson");
                    lastHalf.push("Warren G Harding");
                    lastHalf.push("Calvin Coolidge");
                    lastHalf.push("Herbert Hoover");
                    lastHalf.push("Franklin D Roosevelt");
                    lastHalf.push("Harry S Truman");
                    lastHalf.push("Dwight D Eisenhower");
                    lastHalf.push("John F Kennedy");
                    lastHalf.push("Lyndon B Johnson");
                    lastHalf.push("Richard M Nixon");
                    lastHalf.push("Gerald R Ford");
                    lastHalf.push("Jimmy Carter");
                    lastHalf.push("Ronald Reagan");
                    lastHalf.push("George Bush");
                    lastHalf.push("Bill Clinton");
                    lastHalf.push("George W Bush");
                    lastHalf.push("Barack Obama");

                            do
                            {
                                System.out.print("Enter the next president: ");
                                studentPresidents = in.nextLine();
                                if(studentPresidents.equalsIgnoreCase(lastHalf.top()))
                                {
                                    lastHalf.pop();
                                }
                                else
                                {
                                    wrongAnswer.push(studentPresidents);
                                    System.out.println("That is not correct. Try Again!");
                                }
                            } while(!presidents.isEmpty());

                            do
                            {
                                System.out.print("You missed: \n" + wrongAnswer.top());
                                wrongAnswer.pop();
                            }while(!wrongAnswer.isEmpty());

                            break;


            case 4: UnboundedStackInterface<String> reversePres;
                     reversePres = new LinkedStack<String>();

                     do
                     {
                        reversePres.push(presidents.top());
                        presidents.pop();
                     }while(!presidents.isEmpty());

                     do
                     {
                         System.out.print("Enter the next president: ");
                                studentPresidents = in.nextLine();
                                if(studentPresidents.equalsIgnoreCase(reversePres.top()))
                                {
                                    reversePres.pop();
                                }
                                else
                                {
                                    wrongAnswer.push(studentPresidents);
                                    System.out.println("That is not correct. Try Again!");
                                }
                      }while(!reversePres.isEmpty());

                            do
                            {
                                System.out.print("You missed: \n" + wrongAnswer.top());
                                wrongAnswer.pop();
                            }while(!wrongAnswer.isEmpty());

                      break;

            case 0: System.out.println("Exit!");
                    break;

            default: break;
        }   
    }

}

这是我的 LinkedStack 类

package namepresidents;

public class LinkedStack<T> implements
        UnboundedStackInterface<T>
{
    protected LLNode<T> top;

    //=================================constructor==============================
    public LinkedStack()
    {
        top = null;
    }

    //===================================push===================================
    public void push(T element)
    {
        LLNode<T> newNode = new LLNode<>(element);
        newNode.setLink(top);
        top = newNode;
    }

    //====================================pop===================================
    public void pop()
    {
        if (!isEmpty())
        {
            top = top.getLink();
        }
        else
        {
            throw new StackUnderflowException("Pop"
            + "Attempted on empty stack.");
        }
    }

    //=====================================top==================================
    public T top()
    {
        if (!isEmpty())
        {
            return top.getInfo();
        }
        else
        {
            throw new StackUnderflowException("top"
            + "Attempted on empty stack.");
        }
    }

    //======================================isEmpty=============================
    public boolean isEmpty()
    {
        if (top == null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

这是我的 LLNODE 类

package namepresidents;

public class LLNode<T> 
{
    private T info;
    private LLNode<T> link;

    public LLNode(T info)
    {
        this.info = info;
        link = null;
    }

    public void setInfo(T info)
    {
        this.info = info;
    }

    public T getInfo()
    {
        return info;
    }

    public void setLink(LLNode<T> link)
    {
        this.link = link;

    }

    public LLNode<T> getLink()
    {
        return link;
    }


}

最佳答案

选项 2 和 3 中有一个错误。看看这个简化的循环代码:

do
{
    if(studentPresidents.equalsIgnoreCase(firstHalf.top()))
    {
        firstHalf.pop(); // pops firstHalf
    }
}while(!presidents.isEmpty()); // tests presidents

这就是复制和粘贴编程不好的原因。很容易出错。

关于java - 为什么这继续要求字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26109593/

相关文章:

java - 在 android sqlite 数据库中插入和执行查询时出错

linux - 进程数是否受 RAM 大小的限制?

c - 为什么堆栈粉碎仅在我的矩阵程序中与列索引一起发生?

c - 为什么我没有得到 0、1、2 作为输出?

Java将iso_instant格式的字符串转换为日期

java - 日历返回错误的月份

JavaFX - 将子阶段居中到父阶段

java - 如何将带有 mongoDB 项目的 spring 4 + Hibernate 切换到 MySQL?

c++ - 使用 std::stack 而不是 deque、vector 或 list 的优点和缺点是什么

C堆栈奇怪的行为