//当我尝试编译它时会不断弹出这个错误,说它在解析时到达了文件的末尾,这让我相信我的'{}'括号有问题。请帮助!!!
//这是代码:
import java.util.*;
class GameOfNim
{ public static void main(String [] args)
{ Scanner in = new Scanner (System.in);
int marbles, player, difficulty, pick;
marbles = (int)(Math.random()*90+10);
player = (int)(Math.random()+0.5);
difficulty = (int)(Math.random()+0.5);
if(difficulty == 0)
System.out.println("The Computer is now in Beast Mode!");
else if(difficulty == 1)
System.out.println("The Computer is now in easy mode.");
System.out.println("The pile of marbles has: " + marbles + "total marbles. Game On!");
while(marbles>0)
{ if(player==0)//person
{System.out.print("Pick some marbles");
pick = in.nextInt();}
if (pick >= 1 && pick <= marbles/2){
marbles = marbles - pick;
System.out.println(marbles+"left");}
else if(pick < 1 || pick > marbles/2 && pile > 1){
System.out.println("Error: Illegal Move.");
player = 0;
continue;}
else {
marbles = marbles - pick;
System.out.println(marbles + "left.");
}}
if(player == 1 && difficulty ==1)
{
System.out.println("Computer's turn to pick");
pick = (int)(Math.random()*marbles/2+1);
marbles = marbles - pick;
System.out.println("computer picks: " + pick + "marbles." + "There are: " + marbles + " marbles left.");}
else if(player == 1 && difficulty == 0)
{
System.out.println("Computer's turn to pick");
if(marbles > 63)
{pick = marbles - 63;
marbles = marbles - pick;}
else if(pile > 31)
{pick = marbles - 32;
marbles = marbles - pick;}
else if(pile > 15)
{pick = marbles - 15;
marbles = marbles - pick;}
else if(pile > 7)
{pick = marbles - 7;
marbles = marbles - pick;}
else if(pile > 3)
{pick = marbles - 3;
marbles = marbles - pick;}
else{
pick = (int)(Math.random()*marbles/2+1);
marbles = marbles - pick;
System.out.println("Computer picks: " + pick + "marbles." + "There are: " + marbles + " marbles left.");
}
{if(player==0)
player=1;
else player=0;
}
{if(player==0)
System.out.println("Computer: 'I give up! You win!");
else
System.out.println("Computer: '...You are pathetic...'");
}}
//注意:通过调整块中的代码。似乎问题可能出在我的While循环中。但我真的不知道。
答案 0 :(得分:3)
首先,您应该正确缩进代码。您应该阅读一些关于Java的缩进约定。以下是正确缩进代码的示例:
class GameOfNim {
public static void main(String[] args) {
[...]
if (difficulty == 0) {
System.out.println("The Computer is now in Beast Mode!");
} else if (difficulty == 1) {
System.out.println("The Computer is now in easy mode.");
}
}
}
在修复代码缩进后,我很容易发现您在文件末尾缺少两个右括号,一个用于主方法,一个用于您的类。
答案 1 :(得分:2)
好友。您应该首先使用编码约定。一个条款应该是:
void function(){
}
OR
void function()
{
}
同样适用于if,while循环和类。选择一个编码标准并坚持下去:) 至于你的问题,你省略了结束括号'}'关闭课程。
答案 2 :(得分:0)
在某个地方,你错过了两个'}'字符,在将代码复制/粘贴到IDE后,我注意到有人抱怨没有排队的东西。你错过了两个端括号。
此外,经过进一步检查,看起来“桩”永远不会被声明或初始化为任何值。
答案 3 :(得分:-1)
你应该尝试缩进。 你可以尝试改变
{if(player==0)
player=1;
else player=0;
}
{if(player==0)
System.out.println("Computer: 'I give up! You win!");
else
System.out.println("Computer: '...You are pathetic...'");
}} `enter code here`
with:
if(player==0)
player=1;
else player=0;
if(player==0)
System.out.println("Computer: 'I give up! You win!");
else
System.out.println("Computer: '...You are pathetic...'");
}
}