我正在制定一项旨在帮助学生学习校长的课程。我正在使用堆栈。它应该要求用户输入,然后将输入与堆栈顶部进行比较。如果它是正确的,它将删除顶部项目。否则它要求下一任总统。当我到达终点时,它再次要求下一任总统,即使筹码应该是空的。
这是我的主要计划。
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;
}
}
答案 0 :(得分:2)
选项2和3中有错误。请查看此简化循环代码:
do
{
if(studentPresidents.equalsIgnoreCase(firstHalf.top()))
{
firstHalf.pop(); // pops firstHalf
}
}while(!presidents.isEmpty()); // tests presidents
这就是复制和粘贴编程很糟糕的原因。它容易出错。
答案 1 :(得分:1)
对于案例2和3,你的while()测试应该检查半列表,而不是总统列表。