static int num=0;
//char layOut[4][4] = {'p','.','.','p','.','#','.','#','P','#','.','.','#','p','#','p'};
static char[][] layOut = new char[4][4]{'p','.','.','p','.','#','.','#','P','#','.','.','#','p','#','p'};
SeekFriend()
{
//layOut[4][4] = {"p",".",".","a","#","#","b",".","c",".",".","#","#","#","d","."};
// xLocation = 0;
// yLocation = 0;
// length = 0;
}。 public MyStack ms = new MyStack();错误提示:
--------------------Configuration: <Default>--------------------
D:\Java\TopCoderEx\SeekFriend.java:13: ';' expected
static char[][] layOut = new char[4][4]{'p','.','.','p','.','#','.','#','P','#','.','.','#','p','#','p'};
^
D:\Java\TopCoderEx\SeekFriend.java:61: illegal start of expression
public MyStack ms = new MyStack();
^
2 errors
//char layOut[4][4] = {'p','.','.','p','.','#','.','#','P','#','.','.','#','p','#','p'};
static char[][] layOut = new char[4][4]{'p','.','.','p','.','#','.','#','P','#','.','.','#','p','#','p'};
SeekFriend()
{
//layOut[4][4] = {"p",".",".","a","#","#","b",".","c",".",".","#","#","#","d","."};
// xLocation = 0;
// yLocation = 0;
// length = 0;
}。 public MyStack ms = new MyStack();错误提示:
--------------------Configuration: <Default>--------------------
D:\Java\TopCoderEx\SeekFriend.java:13: ';' expected
static char[][] layOut = new char[4][4]{'p','.','.','p','.','#','.','#','P','#','.','.','#','p','#','p'};
^
D:\Java\TopCoderEx\SeekFriend.java:61: illegal start of expression
public MyStack ms = new MyStack();
^
2 errors
第一个改成char[][] layout = {{'p','.','.','p'},{'.','#','.','#'},{'P','#','.','.'},{'#','p','#','p'}};
第二个你应该是在方法内部定义的ms吧,方法内部不能用public,去掉就ok
第二个问题看不出来,给的代码不够。也有可能是和第一个问题想关的。有时候前面的问题解决,后面的问题也就不是问题了。你先试试吧。
出现20个问题!!就是这个类中的所有方法都提示找不到!
但是这个类就在这个文件的下面紧接着编写的~!
加上以后就直接提示 illegal start of expression
第一个问题是解决了~
第二个~本来也没加那个public ~可是去掉它就会提示ms中的方法都找不到~(ms定义在main方法中)
MyStack是紧接着下面写得~
class MyStack //FILO
{
private LinkedList ll = new LinkedList();
public void push(Object o)
{
ll.addFirst(o);
}
public Object pop()
{
return ll.removeFirst();
}
public Object peek()
{
return ll.getFirst();
}
public boolean empty()
{
return ll.isEmpty(); //判断是否为空列表
}
}
main函数中的变量是不能用public,protected,private访问权限修饰词的。
class MyStack 和main函数在不在一个文件里?
如果在,去掉public以后是可以看到ms中的方法的。
static char[][] layOut = new char[][]{{'p','.','.','p'}, {'.','#','.','#'}, {'P','#','.','.'} ,{'#','p','#','p'}};
第二个问题
如果MyStack ms是类属性 你可以放到定义类属性的地方
如果是方法内部属性 那需要把public去掉
可以参考下http://www.99inf.net/SoftwareDev/Java/29764.htm
{ static int xLocation = 0;
static int yLocation = 0;
static int length = 0;
static int num=0;
//char layOut[4][4] = {'p','.','.','p','.','#','.','#','P','#','.','.','#','p','#','p'};
static char[][] layOut = new char[][]{{'p','.','.','p'},{'.','#','.','#'},{'P','#','.','.'},{'#','p','#','p'}};
SeekFriend()
{
//layOut[4][4] = {"p",".",".","a","#","#","b",".","c",".",".","#","#","#","d","."};
// xLocation = 0;
// yLocation = 0;
// length = 0;
} static boolean left(char[][] layOut,int x,int y)
{
if(x-1<0)return false;
return layOut[x-1][y]!='#';
}
static boolean right(char[][] layOut,int x,int y)
{
if(x+1>3)return false;
return layOut[x+1][y]!='#';
}
static boolean down(char[][] layOut,int x,int y)
{
if(y+1>3)return false;
return layOut[x][y+1]!='#';
}
static void getFriend()
{
System.out.println("Get "+num+" friend,"
+"it's name is "+layOut[xLocation][yLocation]);
}
static boolean doSeek(char[][] layOut)
{
length++;
switch(layOut[xLocation][yLocation])
{
// case '#':break;
case '.':break;
default:num++;getFriend();break;
}
}
public static void main(String[] args)
{
// MyStack ms = new MyStack();
MyStack ms = new MyStack();
for(;xLocation<4;xLocation++)
{
ms.push(Integer.valueOf(length));
ms.push(Integer.valueOf(xLocation));
ms.push(Integer.valueOf(yLocation));
doSeek(layOut);
for(;down(layOut,xLocation,yLocation);)
{
yLocation++;
if(rigth(layOut,xLocation,yLocation))
{
ms.push(length);
ms.push(xLocation);
ms.push(yLocation);
while(rigth(layOut,xLocation,yLocation))
{
xLocation++;
doSeek(layOut);
}
yLocation = ms.pop().intValue();
xLocation = ms.pop().intValue();
length = ms.pop().intValue();
}
if(left(layOut,xLocation,yLocation))
{
ms.push(Integer.valueOf(length));
ms.push(Integer.valueOf(xLocation));
ms.push(Integer.valueOf(yLocation));
while(left(layOut,xLocation,yLocation))
{
xLocation--;
doSeek(layOut);
}
yLocation = ms.pop().intValue();
xLocation = ms.pop().intValue();
length = ms.pop().intValue();
}
yLocation = ms.pop().intValue();
xLocation = ms.pop().intValue();
length = ms.pop().intValue();
}
}
}
}
class MyStack //FILO
{
private LinkedList ll = new LinkedList();
public void push(Object o)
{
ll.addFirst(o);
}
public Object pop()
{
return ll.removeFirst();
}
public Object peek()
{
return ll.getFirst();
}
public boolean empty()
{
return ll.isEmpty(); //判断是否为空列表
}
}
第一个问题在各位的帮助下解决了~
可是还没有解决第二个问题~
这是所有程序~拜托了~
import java.util.*; class SeekFriend
{ static int xLocation = 0;
static int yLocation = 0;
static int length = 0;
static int num=0;
//char layOut[4][4] = {'p','.','.','p','.','#','.','#','P','#','.','.','#','p','#','p'};
static char[][] layOut = new char[][]{{'p','.','.','p'},{'.','#','.','#'},{'P','#','.','.'},{'#','p','#','p'}};
SeekFriend()
{
//layOut[4][4] = {"p",".",".","a","#","#","b",".","c",".",".","#","#","#","d","."};
// xLocation = 0;
// yLocation = 0;
// length = 0;
} static boolean left(char[][] layOut,int x,int y)
{
if(x-1<0)return false;
return layOut[x-1][y]!='#';
}
static boolean right(char[][] layOut,int x,int y)
{
if(x+1>3)return false;
return layOut[x+1][y]!='#';
}
static boolean down(char[][] layOut,int x,int y)
{
if(y+1>3)return false;
return layOut[x][y+1]!='#';
}
static void getFriend()
{
System.out.println("Get "+num+" friend,"
+"it's name is "+layOut[xLocation][yLocation]);
}
static boolean doSeek(char[][] layOut)
{
length++;
switch(layOut[xLocation][yLocation])
{
// case '#':break;
case '.':break;
default:num++;getFriend();break;
}
return true;
}
public static void main(String[] args)
{
// MyStack ms = new MyStack();
MyStack ms = new MyStack();
for(;xLocation<4;xLocation++)
{
ms.push(Integer.valueOf(length));
ms.push(Integer.valueOf(xLocation));
ms.push(Integer.valueOf(yLocation));
doSeek(layOut);
for(;down(layOut,xLocation,yLocation);)
{
yLocation++;
if(right(layOut,xLocation,yLocation)) 错误 rigth-> right 跟上面写的方法名不一致
{
ms.push(length);
ms.push(xLocation);
ms.push(yLocation);
while(right(layOut,xLocation,yLocation)) 错误 rigth ->right
{
xLocation++;
doSeek(layOut);
}
yLocation = ((Integer) ms.pop()).intValue();
xLocation = ((Integer) ms.pop()).intValue();
length = ((Integer) ms.pop()).intValue();
}
if(left(layOut,xLocation,yLocation))
{
ms.push(Integer.valueOf(length));
ms.push(Integer.valueOf(xLocation));
ms.push(Integer.valueOf(yLocation));
while(left(layOut,xLocation,yLocation))
{
xLocation--;
doSeek(layOut);
}
yLocation = ((Integer) ms.pop()).intValue();
xLocation = ((Integer) ms.pop()).intValue();
length = ((Integer) ms.pop()).intValue();
}
yLocation = ((Integer) ms.pop()).intValue();
xLocation = ((Integer) ms.pop()).intValue();
length = ((Integer) ms.pop()).intValue();
}
}
}
}
class MyStack //FILO
{
private LinkedList ll = new LinkedList();
public void push(Object o)
{
ll.addFirst(o);
}
public Object pop()
{
return ll.removeFirst();
}
public Object peek()
{
return ll.getFirst();
}
public boolean empty()
{
return ll.isEmpty(); //判断是否为空列表
}
}