Question: My if else statement isn't executing properly. The second value I have stored in the args array is verbose. However, I need it to print

My if else statement isn't executing properly. The second value I have stored in the args array is "verbose". However, I need it to print an updated game board if it does equal verbose and if it doesn't equal verbose, then to just print out only the intial board (just dots) and the final game board (with 3 ships hidden because the first element of args is "3"). This is a battleship game in JAVA.

My code:

package battleship; import java.util.*; public class SBattleship { private char[][] grid; public SBattleship(int size) { grid = new char[size][size]; initialize(); } //creates grid private void initialize() { int rows = grid.length, cols = grid[0].length; for (int i = 0; i out.print("."); else System.out.print(grid[i][j]); System.out.print(" "); } System.out.println(); } System.out.println(); } //hides ship public void hide(char ship, int len, int row, int col, char dir) { int dr = 0, dc = 0; if (dir == 'V') dr = 1; else dc = 1; int r = row, c = col; for (int i = 0; i in); SBattleship p = new SBattleship(10); int row, col, len; char ship, dir; //args first element is "3", second element is "verbose" for (int i = 0; i out.println("args = " + args[i]); } System.out.println("Initial Board"); p.print(); int l = Integer.parseInt(args[0]); for (int i = 1; i out.print("Which ship do you want to place? "); ship = stdin.next().toUpperCase().charAt(0); System.out.print("How long is the ship? "); len = stdin.nextInt(); System.out.print("Please enter a X coordinate to place a ship: "); row = stdin.nextInt(); System.out.print("Please enter a Y coordinate to place a ship: "); col = stdin.nextInt(); System.out.print("Do you want the ship to be vertical or horizontal? (V or H)?: "); dir = stdin.next().toUpperCase().charAt(0); //problem here if (args[1] == "verbose") { System.out.println("Updated Board"); p.print(); } else if (args[1] != "verbose") { System.out.println("Initial and Final Game Board"); p.print(); } p.hide(ship, len, row, col, dir); System.out.println("Updated Board "); p.print(); } } } 

My if else statement isn't executing properly. The second value I have

stored in the args array is "verbose". However, I need it to

COL StOIn.nextINT I External Libraries 88 89 90 System.out.print("Do you want the ship to be vertical or horizontal? (V or H)7:") dir stdin.next().toUpperCase().charAt(0) if (args [1]"verbose") 92 93 System.out.println( "Updated Board") p.print(); 95 96 else if (args [1] "verbose") System.out.println("Initial and Final Game Board") 98 p.print)l Run:Main Main Main Main Main [-Main [-Main [-Main [-Main [-Main [ Main [-Main [ Main vo you want tne snip to be verticaL or norizonta Lr iv or nir Initial Board and Final Game Board 4] Updated Board Compilation completed successfully in 2s 961ms (17 minutes ago) 98:31 LF# UTF-8 'b

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!