这是我第一次在这里发帖,因为我很绝望。无论如何,我有这个项目,但在实施队列时遇到了麻烦。我的 android 应用程序是一个使用队列实现的 rebus 谜题。在游戏开始时,我想将包含每个谜题答案的所有节点排入队列。然后会显示一张拼图的照片,以及一个文本字段和一个按钮,用户可以在其中输入答案。我的问题是我不知道如何在 java 中实现队列。这样当我按下按钮提交答案时,它将使当前节点出队,然后级别将增加,同时会显示不同的图片。但是如果用户的答案是错误的,那么它将停留在当前的谜题中。
这是我的拼图节点代码:
package com.project.rebus;
public class PuzzleNode {
private String answer;
private PuzzleNode next;
public PuzzleNode(String answer, PuzzleNode next) {
this.answer = answer;
this.next = next;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
public PuzzleNode getNext() {
return next;
}
public void setNext(PuzzleNode next) {
this.next = next;
}
}
这是我的队列代码:
package com.project.rebus;
public class PuzzleQueue {
PuzzleNode head;
PuzzleNode tail;
int size;
int score;
public PuzzleQueue() {
head = null;
tail = null;
size = 0;
score = 0;
}
public int size() {
return size;
}
public boolean isEmpty() {
if(head == null)
return true;
else
return false;
}
public void enqueue (PuzzleNode data)
{
PuzzleNode n = new PuzzleNode(data.getAnswer(), null);
if (tail != null)
tail.setNext(n);
tail = n;
if (head == null)
head = n;
size++;
}
public String dequeue ()
{
String temp;
temp = head.getAnswer();
head = head.getNext();
size--;
if (size == 0)
tail = null;
return temp;
}
public String front()
{
return head.getAnswer();
}
public boolean compare(String answer) {
if (head.getAnswer().equalsIgnoreCase(answer))
return true;
else return false;
}
}
这是我将在其中实现队列的 Android 活动一的代码:
package com.project.rebus;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class RebusActivityOne extends Activity {
PuzzleQueue rebus = new PuzzleQueue();
PuzzleNode p1 = new PuzzleNode("for instance", null);
PuzzleNode p2 = new PuzzleNode("too funny for words", null);
PuzzleNode p3 = new PuzzleNode("once in a blue moon", null);
int level = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rebus_activity_one);
ImageView image = (ImageView) findViewById (R.drawable.r1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.rebus_activity_one, menu);
return true;
}
public void button (View view) {
rebus.enqueue(p1);
rebus.enqueue(p2);
rebus.enqueue(p3);
ImageView image = null;
while (level <= 3) {
if (level == 1) {
image = (ImageView) findViewById (R.id.r1);
}else if (level == 2) {
image.setImageResource(R.drawable.r2);
}else if (level == 3) {
image.setImageResource(R.drawable.r3);
}
boolean correct = false;
do {
EditText answerField = (EditText)findViewById(R.id.answerField);
String answer = answerField.getText().toString();
if (rebus.compare(answer) == true) {
correct = true;
rebus.dequeue();
level++;
}//else System.out.println("Wrong answer");
}while(correct == false);
}
}
}
我不断收到按钮代码错误。IDK该做什么了。呼呼呼。任何帮助将不胜感激。