Compiler: Line 39 { expected
From: Andrew (awallwork_at_gmail.com)
Date: 02/24/05
- Next message: Ryan Stewart: "Re: Compiler: Line 39 { expected"
- Previous message: BladeZ: "Text Areas and drawString"
- Next in thread: Ryan Stewart: "Re: Compiler: Line 39 { expected"
- Reply: Ryan Stewart: "Re: Compiler: Line 39 { expected"
- Reply: Bjorn Abelli: "Re: Compiler: Line 39 { expected"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 24 Feb 2005 08:48:18 -0800
Hi
When I compile the following code, I get 4 errors, the first of which
is really bugging me. I've checked my braces, and there doesn't seem
to be anything wrong with them, all methods appear to be braced. Other
than that, I've had some issues with "cannot make a static reference to
nonstatic variable" errors which do not make sense.
This is a very basic battleship program, designed to take xy
co-ordinates from one user, then another user randomly guesses xy
co-ordinates, eventually sinking all of the user placed ships. This is
for a computer science class, I'v run it past my instructor who has
gone over it, and isn't having much luck finding anything wrong there.
If anyone could shed some light into the issue, it would be greatly
appreciated.
import java.*;
import EasyReader;
public class BattleShip
{
int hits; // defines the number of hits
int shots; // defines shots fired
int percentage; // defines a percentage of shots hit vs shots fired
int x;
static int grid [][]; // defines the grid
EasyReader console = new EasyReader ();
public static void main() // loads all the methods in sequence and
loads EasyReader
{
fillGrid();
addShips();
playGame();
}
public static void fillGrid() // fills the grid with false tags
{
int x;
int y;
for (x=1; x<=40; x++)
{
for (y=1; y<=40; y++)
{ grid[y][x] = 'F'; }
}
}
public static void addShips()
{
static int x = 0;
int y = 0;
int c = 0;
for (c = 1; c <= 5; c++)
{
System.out.println("Please Enter X Co-Ordinate");
x = console.readInt;
System.out.println("Please Enter y Co-Ordinate");
y = console.readInt;
// checks the grid to ensure ship isn't there if not, it places a
ship in place
if (grid [y][x] == 'T')
{
System.out.println("You already have a ship there");
}
else
{
grid [y][x]= true;
}
}
}
public static void playGame() // plays the game and calculates winloss
{
int x = 0;
int y = 0;
// creates loop that only allows 10 shots
do
{
System.out.println("Please Enter X Co-Ordinate to fire at");
x = console.readInt;
System.out.println("Please Enter y Co-Ordinate to fire at");
y = console.readInt;
if (grid [y][x] = 'T')
{
System.out.println("HIT");
hits = hits+1;
shots = shots+1;
System.out.println("You have hit" +((hits/shots)*100) +"% in your
game");
grid [y][x]= 'F';
}
else
{
System.out.println("NO HIT");
shots = shots+1;
System.out.println("You have hit" +((hits/shots)*100) +"% in your
game");
}
} while ( shots <= 10 || hits ==5);
}
}
- Next message: Ryan Stewart: "Re: Compiler: Line 39 { expected"
- Previous message: BladeZ: "Text Areas and drawString"
- Next in thread: Ryan Stewart: "Re: Compiler: Line 39 { expected"
- Reply: Ryan Stewart: "Re: Compiler: Line 39 { expected"
- Reply: Bjorn Abelli: "Re: Compiler: Line 39 { expected"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|