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

twjohns29

我正在研究一个计划,该计划应该可以帮助学生学习总统。我正在使用堆栈。应该要求用户输入,然后将输入与堆栈顶部进行比较。如果正确,则删除最上面的项目。否则,要求下任总统。当我走到尽头时,即使堆栈为空,它也会再次要求下任总统。

这是我的主程序。

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


}
Radiodef

您在选项2和3中有误。请看以下简化的循环代码:

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么这没有在正确的位置添加正确的字符串?

为什么这无法解码base64字符串?

为什么这个 Python 函数对包含为字符串的整数进行排序比这慢?

控制台继续输出在Swift中包装为Optional的字符串,为什么?

为什么键入错误:<string>中的字符串要求字符串作为左操作数,而不是列表

为什么FindNextFile和FindFirstFile在开始时会打印这2个奇怪的字符串?

为什么Python搁置要求所有键都是字符串?

为什么C ++ regex_match函数要求在该函数之外定义搜索字符串?

为什么Grunt文件中止并显示消息path.join的参数必须为字符串使用--force才能继续

为什么字符串文字不是字符串?

为什么真相&&“字符串”返回“字符串”

为什么“字符串”-“字符串”为NaN?

为什么字符串不是值?

为什么字符串的startwith比in慢?

为什么++ [[]] [+ []] + [+ []]返回字符串“ 10”?

为什么字符串被密封

为什么字符串循环被忽略?

为什么不显示字符串?

为什么Express Multer要求输入字段名称与传递给中间件的字符串相同?

为什么不计算字符串中的字符?

为什么字符串从字符数组创建不拘留?

为什么不能计算字符串中的管道字符?

为什么我的 for 循环返回字符而不是字符串

Python样式-字符串继续行吗?

继续接收空字符串

grep继续打印相同的字符串

以字符串继续if / else语句(是或否)

为什么这个分割字符串的 SQL 使用逗号分隔的字符串而不是空格分隔的字符串?

字符串生成器不使用从字符串池的字符串常量。为什么?