Java 用户输入的迷宫位置
我的程序编译没有错误。当我试着跑的时候它就结束了。有人能帮我用用户输入的开始和结束位置来完成这项跑步吗?我有点迷路了。以下是作业: 修改第4章中的迷宫问题,使其可以从用户定义的起始位置(0、0除外)开始,并搜索用户定义的终点。 代码基本上就是前面提到的迷宫问题,只有一个Position类,没有用户输入的部分Java 用户输入的迷宫位置,java,stack,maze,Java,Stack,Maze,我的程序编译没有错误。当我试着跑的时候它就结束了。有人能帮我用用户输入的开始和结束位置来完成这项跑步吗?我有点迷路了。以下是作业: 修改第4章中的迷宫问题,使其可以从用户定义的起始位置(0、0除外)开始,并搜索用户定义的终点。 代码基本上就是前面提到的迷宫问题,只有一个Position类,没有用户输入的部分 public class Maze { public static void main(String[] args) { } public interface S
public class Maze {
public static void main(String[] args) {
}
public interface StackADT<T> {
public void push(T element);
public T pop();
public T peek();
public boolean isEmpty();
public int size();
public String toString();
}
class LinkedStack<T> implements StackADT<T> {
private int count;
private LinearNode<T> top;
public LinkedStack() {
count = 0;
top = null;
}
class LinearNode<T> {
private LinearNode<T> next;
private T element;
public LinearNode() {
next = null;
element = null;
}
public LinearNode(T elem) {
next = null;
element = elem;
}
public LinearNode<T> getNext() {
return next;
}
public void setNext(LinearNode<T> node) {
next = node;
}
public T getElement() {
return element;
}
public void setElement(T elem) {
element = elem;
}
}
class Position {
private int x;
private int y;
Position() {
x = 0;
y = 0;
}
public int getx() {
return x;
}
public int gety() {
return y;
}
public void setx1(int a) {
x = a;
}
public void sety(int a) {
y = a;
}
public void setx(int x2) {
}
}
private final int TRIED = 3;
private final int PATH = 7;
private int[][] grid = {{1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1},
{1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1},
{1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0},
{0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1},
{1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1},
{1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
public StackADT<Position> push_new_pos(int x, int y,
StackADT<Position> stack) {
Position npos = new Position();
npos.setx1(x);
npos.sety(y);
if (valid(npos.getx(), npos.gety())) {
stack.push(npos);
}
return stack;
}
public boolean traverse() {
boolean done = false;
Position pos = new Position();
Object dispose;
StackADT<Position> stack = new LinkedStack<Position>();
stack.push(pos);
while (!(done)) {
pos = stack.pop();
grid[pos.getx()][pos.gety()] = TRIED; // this cell has been tried
if (pos.getx() == grid.length - 1 && pos.gety() == grid[0].length - 1) {
done = true; // the maze is solved
} else {
stack = push_new_pos(pos.getx(), pos.gety() - 1, stack);
stack = push_new_pos(pos.getx(), pos.gety() + 1, stack);
stack = push_new_pos(pos.getx() - 1, pos.gety(), stack);
stack = push_new_pos(pos.getx() + 1, pos.gety(), stack);
}
}
return done;
}
private boolean valid(int row, int column) {
boolean result = false;
if (row >= 0 && row < grid.length
&& column >= 0 && column < grid[row].length) {
if (grid[row][column] == 1) {
result = true;
}
}
return result;
}
public String toString() {
String result = "\n";
for (int row = 0; row < grid.length; row++) {
for (int column = 0; column < grid[row].length; column++) {
result += grid[row][column] + "";
}
result += "\n";
}
return result;
}
@Override
public void push(T element) {
// TODO Auto-generated method stub
}
@Override
public T pop() {
// TODO Auto-generated method stub
return null;
}
@Override
public T peek() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
}
}
公共类迷宫{
公共静态void main(字符串[]args){
}
公共接口StackADT{
公共空间推送(T元素);
公共T pop();
公共T peek();
公共布尔值为空();
公共整数大小();
公共字符串toString();
}
类LinkedStack实现StackADT{
私人整数计数;
专用线性节点顶部;
公共LinkedStack(){
计数=0;
top=null;
}
类线性节点{
其次是专用线性节点;
私有T元素;
公共线性节点(){
next=null;
元素=空;
}
公共线性节点(T元素){
next=null;
元素=元素;
}
public LinearNode getNext(){
下一步返回;
}
public void setNext(LinearNode节点){
下一个=节点;
}
公共T getElement(){
返回元素;
}
公共无效集合元素(T元素){
元素=元素;
}
}
阶级地位{
私人INTX;
私营企业;
职位({
x=0;
y=0;
}
公共int getx(){
返回x;
}
公共int gety(){
返回y;
}
公共无效集合X1(int a){
x=a;
}
公共空间设置(int a){
y=a;
}
公共无效集合x(整数x2){
}
}
私人最终投资=3;
私有最终整数路径=7;
私有int[][]网格={{{1,1,1,0,1,1,0,0,1,1,1,1},
{1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1},
{1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0},
{0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1},
{1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1},
{1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
公共堆栈推送新位置(int x、int y、,
堆栈(ADT堆栈){
职位npos=新职位();
npos.setx1(x);
npos.sety(y);
if(有效(npos.getx(),npos.gety()){
堆栈推送(npos);
}
返回栈;
}
公共布尔遍历(){
布尔完成=假;
位置pos=新位置();
物体处理;
StackADT stack=newlinkedstack();
堆栈推送(pos);
而(!(完成)){
pos=stack.pop();
网格[pos.getx()][pos.gety()]=trued;//此单元格已尝试
if(pos.getx()==grid.length-1&&pos.gety()==grid[0].length-1){
done=true;//迷宫已解决
}否则{
堆栈=推送新位置(位置getx(),位置gety()-1,堆栈);
堆栈=推送新位置(位置getx(),位置gety()+1,堆栈);
堆栈=推送新位置(位置getx()-1,位置gety(),堆栈);
堆栈=推送新位置(位置getx()+1,位置gety(),堆栈);
}
}
已完成的返回;
}
私有布尔值有效(int行,int列){
布尔结果=假;
如果(行>=0&&row=0&&column
您在主方法中什么都不做:
public static void main(String[] args){}
因此,程序不执行任何操作。您在主方法中不执行任何操作:
public static void main(String[] args){}
因此,程序什么也不做。当我将整个程序设为Main时,接口StackADT告诉我:成员接口StackADT只能在顶级类或接口或静态上下文中定义,您不必将所有内容都放在Main方法中,你知道吗?我去掉了main方法,然后Position类中的x和y就不能解析为变量了。你熟悉面向对象编程的概念吗?我不知道你想做什么,因为