解决方案 »
- 救急 救急 有两问题,各位路过的大神别错过,帮帮忙
- 采用JNI对读写器SDK的DLL进行再封装 难不难?
- 字符串数组问题
- 双击可执行Jar文件没反应---我没辙
- 今天编写拉个打开文件的简单程序用到拉I/O流 可是用程序来打开文件不行可以帮我看看吗谢谢 我分快没拉所以给的不多不过重在交流嘛!:)
- 新手求助:一示例程序不知何因不能运行,捣鼓了一天了!
- 请那位大哥,详细介绍一下 如何用JB生成 .exe 文件
- 高手来答
- 关于classpath的疑惑
- java 录屏软件的播放器中添加一个暂停按钮要怎么添加代码
- 各位大GG.DD!小妹又来问问题了...................UP也可.........
- 有没有比较好的源码下载站点推荐!!!
如果试试我的程序就最好了,我要解决nullpointer Exception,逻辑不难的。
谢谢了好心人。
那位帮我调试一下吧,我知道比较过分,但是要影响我得GPA啊。
你把array又定义了一次,改为:
array = new String[size][size];就Ok啦,不过你的程序还有很多问题,自己慢慢解决吧,嘿嘿...
你的array定義了兩遍!!!
你賦值的array是局部變量,而你真正的array並沒有賦值,只要把第二次出現的array前面的String去掉就ok了
snowredfox:你的方法不行啊
public static void main(String args[])
{
array = new String[size][size];
}
public static boolean rowTest(int pos, int size)
这个方法的这个语句出错:if(array[(pos-1)/size][i] != array[(pos-1)/size][i+1])array为空对象!!!
明顯就出錯了, (pos-1)%size-1 這個值為-1,當然數組越界了
你要注意"%"是取模,不是取整,也許你該用"/"
不过我只输入了一次,就说我赢了。唉。
程序很小,哪里要用300分
希望对你还来得及import javax.swing.JOptionPane; public class Tictacto
{
static String array[][];
static int size;// the size of the game
static int counter;
static int pos;
static boolean win = false;
public static void main(String args[])
{
size = Integer.parseInt(JOptionPane.showInputDialog(
"Enter the size fo the :(3-10)"));
array = new String[size][size];
for(int i = 0; i < size; i++ )
for(int t = 0; t < size; t++)
array[t][i] = "e"; // "e" indicates the position
// has not been used.
print(array, size);
while(!win)
{
pos = Integer.parseInt(JOptionPane.showInputDialog("Choose the position: "));
play(pos, counter,size);
counter++;
win = totalTest(pos, size);
print(array, size);
}
System.out.println("Congratulations! You won !!!");
System.exit(0);
}
// show the steps of the palyer
public static void play(int pos, int counter, int size)
{
if(array[(pos-1)/size][(pos-1)%size] =="e")
if(counter%2 == 1)
array[(pos-1)/size][(pos-1)%size] = "x";
else
array[(pos-1)/size][(pos-1)%size] = "o";
}
// total
public static boolean totalTest(int pos, int size)
{
boolean r1, r2, r3, r4;
r1 = rowTest(pos,size);
r2 = colTest(pos,size);
r3 = left(pos,size);
r4 = right(pos, size);
System.out.println(r1);
System.out.println(r2);
System.out.println(r3);
System.out.println(r4);
if(r1||r2||r3||r4)
return true;
else
return false;
}
// test the row
public static boolean rowTest(int pos, int size)
{
for(int i =0; i < size - 1; i++){
if(array[(pos-1)/size][i].equals("e"))
return false;
if(array[(pos-1)/size][i] != array[(pos-1)/size][i+1] )
return false;
}
return true;
}
// test the column
public static boolean colTest(int pos, int size)
{
for(int i = 0; i < size - 1; i++){
if(array[i][(pos-1)%size ].equals("e"))
return false;
if(array[i][(pos-1)%size ] != array[i+1][(pos-1)%size ])
return false;
}
return true;
}
// test the left cross
public static boolean left(int pos, int size)
{
for (int i = 0; i < size - 1; i++){
if(array[i][i].equals("e"))
return false;
if(array[i][i] != array[i+1][i+1])
return false;
}
return true;
}
// test the right cross
public static boolean right(int pos, int size)
{
for(int i = 0; i < size - 1; i++){
if(array[size-1-i][i].equals("e"))
return false;
if(array[size-1-i][i] != array[size-1-i-1][i+1])
return false;
}
return true;
}
// print to Screen
public static void print(String arr[][], int size)
{
for(int p = 0; p < size; p++,System.out.println())
for(int q = 0; q < size; q++)
System.out.print(arr[p][q] + " ");
System.out.println();
}
}