Results 1 to 9 of 9
Thread: My first Java Code [PROBLEMS]
- 04-12-2012, 08:25 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
My first Java Code [PROBLEMS]
hey, I don`t know a lot about codes, but at uni we have to do battleships Java code.
I have created this code so far but they are a lot errors, that I cant find out how to fix, I wound appreciate is some could give me a hand and tell were i going wrong. incomparable types: char and java.lang.String is the main error I font understand.
the code is ...
thanks to anyone who may read this, I feel so silly having to do this but we got to start some place.Java Code:/** * @(#)Battleships.java * * Battleships application * * @author * @version 1.00 2012/3/11 */ import java.util.Scanner; public class bs3 { public static void main(String[] args) { //*instructions { System.out.println ("to play enter a number from 1-10 to place your ships, and the same to fire."); } //*player grid int c[][]={ {1,2,3,4,5,6,7,8,9,10}, {1,0,0,0,0,0,0,0,0,0}, {2,0,0,0,0,0,0,0,0,0}, {3,0,0,0,0,0,0,0,0,0}, {4,0,0,0,0,0,0,0,0,0}}; for(int a=0;a<5;a++) { //*keybord inputs Scanner fromkeyboard=new Scanner (System.in) ; int mynumberx=fromkeyboard.nextInt (); int mynumbery=fromkeyboard.nextInt (); //*ship inputs { int a=0,b=0; char[][] c=new char[10][4]; if(c[a][b]=="1"||c[a][b+1]=="1");} //*3box ship { c[a][b]="1"; c[a][b+1]="1"; c[a][b+2]="1"; } //*output (hit or miss) System.out.print(""); for (int b=0;b<9;b++) {System.out.print(c[a][b]);} { if(c[mynumberx][mynumbery]==1) System.out.print("HIT"); else System.out.print("MISS"); } } } }Last edited by JosAH; 04-12-2012 at 08:37 PM. Reason: added [code] ... [/code] tags
- 04-12-2012, 09:01 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,393
- Blog Entries
- 7
- Rep Power
- 17
Re: My first Java Code [PROBLEMS]
Character literals need single quotes, as in 'a', 'b' or 'c'; not double quotes (those are Strings) as in "a", "b" or "c".
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-12-2012, 09:05 PM #3
Re: My first Java Code [PROBLEMS]
The c array contains int values, the code is comparing its elements to Strings. The int 1 != to the String: "1"
If you don't understand my response, don't ignore it, ask a question.
- 04-12-2012, 09:24 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,393
- Blog Entries
- 7
- Rep Power
- 17
- 04-12-2012, 09:29 PM #5
Re: My first Java Code [PROBLEMS]
whoops
The second c is in {}s and hides the first one.
Lots going on here. Good "bad coding techniques" example.Last edited by Norm; 04-12-2012 at 09:40 PM.
If you don't understand my response, don't ignore it, ask a question.
- 04-12-2012, 09:37 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,393
- Blog Entries
- 7
- Rep Power
- 17
- 04-12-2012, 10:55 PM #7
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Re: My first Java Code [PROBLEMS]
sorry, first time I ever coded, I have fixed a bit.
only error now is "reached end of file while parsing"
Java Code:/** * @(#)Battleships.java * * Battleships application * * @author * @version 1.00 2012/3/11 */ import java.util.Scanner; public class bs3 { public static void main(String[] args) { //*instructions { System.out.println ("to play enter a number from 1-10 to place your ships, and the same to fire."); } //*Computer grid int c[][]={ {1,2,3,4,5,6,7,8,9,10,11,12}, {1,0,0,0,0,0,0,0,0,0,0,0}, {2,0,0,0,0,0,0,0,0,0,0,0}, {3,0,0,0,0,0,0,0,0,0,0,0}, {4,0,0,0,0,0,0,0,0,0,0,0}, {5,0,0,0,0,0,0,0,0,0,0,0}, {6,0,0,0,0,0,0,0,0,0,0,0}, {7,0,0,0,0,0,0,0,0,0,0,0}, {8,0,0,0,0,0,0,0,0,0,0,0}, {9,0,0,0,0,0,0,0,0,0,0,0}, {10,0,0,0,0,0,0,0,0,0,0,0}}; for(int a=0;a<5;a++) { //*playergrid int d[][]={ {1,2,3,4,5,6,7,8,9,10,11,12}, {1,0,0,0,0,0,0,0,0,0,0,0}, {2,0,0,0,0,0,0,0,0,0,0,0}, {3,0,0,0,0,0,0,0,0,0,0,0}, {4,0,0,0,0,0,0,0,0,0,0,0}, {5,0,0,0,0,0,0,0,0,0,0,0}, {6,0,0,0,0,0,0,0,0,0,0,0}, {7,0,0,0,0,0,0,0,0,0,0,0}, {8,0,0,0,0,0,0,0,0,0,0,0}, {9,0,0,0,0,0,0,0,0,0,0,0}, {10,0,0,0,0,0,0,0,0,0,0,0}}; for(int a=0;a<5;a++) { //*keybord inputs Scanner fromkeyboard=new Scanner (System.in) ; int mynumberx=fromkeyboard.nextInt (); int mynumbery=fromkeyboard.nextInt (); //*ship inputs { int a=0,b=0; char[][] c=new char[10][4]; if(c[a][b]=="1"||c[a][b+1]=="1");} //*3box ship { c[a][b]="1"; c[a][b+1]="1"; c[a][b+2]="1"; } //*output (hit or miss) System.out.print(""); for (int b=0;b<9;b++) {System.out.print(c[a][b]);} { if(c[mynumberx][mynumbery]==1) System.out.print("HIT"); else System.out.print("MISS"); } } } }
- 04-12-2012, 10:56 PM #8
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Re: My first Java Code [PROBLEMS]
p.s any good books to help, I am reading Head start and java for dummy`s ?
sorry about been pest.
- 04-12-2012, 10:57 PM #9
Similar Threads
-
More problems writing this code. any help appreciated.
By JBeese in forum New To JavaReplies: 10Last Post: 06-05-2011, 08:16 PM -
A few problems with my code..
By Kerrai in forum New To JavaReplies: 1Last Post: 10-25-2010, 07:49 PM -
Problems with my source code
By bilak09 in forum New To JavaReplies: 10Last Post: 04-26-2010, 06:23 AM -
Problems with code
By jforce93 in forum New To JavaReplies: 8Last Post: 08-14-2009, 02:49 AM -
problems with java code! (very new - need help asap!)
By sumkindafreek in forum New To JavaReplies: 1Last Post: 01-07-2009, 05:00 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks