Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8961021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:47:28+00:00 2026-06-15T15:47:28+00:00

I am creating a very rudimentary battle ship program for fun and I have

  • 0

I am creating a very rudimentary battle ship program for fun and I
have come across a problem with changing values in my rectangular two dimensional array. Essentially, there are five “ships” placed in this two dimensional array. A user then inputs an integer to see if they hit or miss. I wanted the output to display the array with the guessed number represented with an X for a miss and a 0 for a hit. I am having trouble with changing the value at area[i][j] when it equals the value of t to the 0 or X. I am new to java so I am trying to learn. Any help would be appreciated. Thank you in advance.

import java.util.*;
public class battleship {

    //Rudimentary Battleship game
    public static void main(String[] args) {
        System.out.println(" Hello and welcome to a basic version of Battleship.");
        System.out.println(" The objective of this game is to sink all five ships located on the grid listed below.");
        System.out.println(" Follow the prompt located underneath the grid ");
        final int ROWS = 10;
        final int COLS = 10;
        int sum = 0;
        int [][] area = { {1,2,3,4,5,6,7,8,9,10},
                  {11,12,13,14,15,16,17,18,19,20},
                  {21,22,23,24,25,26,27,28,29,30},
                  {31,32,33,34,35,36,37,38,39,40},
                  {41,42,43,44,45,46,47,48,49,50},
                  {51,52,53,54,55,56,57,58,59,60},
                  {61,62,63,64,65,66,67,68,69,70},
                  {71,72,73,74,75,76,77,78,79,80},
                  {81,82,83,84,85,86,87,88,89,90},
                  {91,92,93,94,95,96,97,98,99,100} };

        for(int i=0; i < area.length; i++) {
            for (int j=0; j < area[i].length; j++) {


              if(area[i][j] < 10) 
                System.out.print("   "+(area[i][j])+" ");  
              else if(area[i][j] < 100) 
                System.out.print("  "+(area[i][j])+" ");
              else 
                System.out.print(" "+(area[i][j])+" ");
            }
          System.out.println("");
        }

    Scanner input = new Scanner(System.in);{
    System.out.println("Enter attack integer:");
    int t;
    while(true){
    t = input.nextInt();

    if ((t == 41) || (t == 42) || (t == 43)){
    System.out.println("Hit - Destroyer");}

    if ((t == 80) || (t == 90) || (t == 100)){
        System.out.println("Hit - Submarine");}


    if((t == 52) || (t == 62) || (t== 72) || (t == 82) || (t == 92)){
        System.out.println ("Hit - Aircraft Carrier");}

    if((t == 15) || (t == 16) || (t == 17) || (t == 18)){
        System.out.println ("Hit - Battleship");}

    if((t == 1) || (t == 2)){
        System.out.println ("Hit - PT Boat");}
    else{
        System.out.println ("Miss");
    }
    System.out.println("You have fired at:" + t);
    int w = 0;
    for(int i=0; i < area.length; i++) {
        for (int j=0; j < area[i].length; j++) {

            if (area[i][j] == t)

          if(area[i][j] < 10) 
            System.out.print("   "+(area[i][j])+" ");  
          else if(area[i][j] < 100) 
            System.out.print("  "+(area[i][j])+" ");
          else 
            System.out.print(" "+(area[i][j])+" ");
        }
      System.out.println("");
    }

        }
        }
    }
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:47:29+00:00Added an answer on June 15, 2026 at 3:47 pm

    This is simple, modifying your code:

    if ((t == 41) || (t == 42) || (t == 43)){
        System.out.println("Hit - Destroyer");
        area[i][j] = "X";
    }
    if ((t == 80) || (t == 90) || (t == 100)){
        System.out.println("Hit - Submarine");
        area[i][j] = "X";
    }
    if((t == 52) || (t == 62) || (t== 72) || (t == 82) || (t == 92)){
        System.out.println ("Hit - Aircraft Carrier");
        area[i][j] = "X";
    }
    
    if((t == 15) || (t == 16) || (t == 17) || (t == 18)){
        System.out.println ("Hit - Battleship");
        area[i][j] = "X";
    }
    if((t == 1) || (t == 2)){
        System.out.println ("Hit - PT Boat");
        area[i][j] = "X";
    }
    else{
        System.out.println ("Miss");
        area[i][j] = "O";
    }
    

    A note: be careful, arrays are 0 indexed, so your ship positions don’t exactly correspond to the way you set up the grid. i.e. area[4][1] is not “41”. You seemed to do this correctly (in the case of the destroyer) for the columns but not the rows. The if statements should check for 31, 32, 33 if the ship is at area[4][0], area[4][1], area [4][2]. In fact, you might be better off labeling the positions from 00, 01, 02, … , 97, 98, 99 so that the indices correspond to the numbers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a very large PHP MVC-based site that will have a large
I have a website which I have been working on creating very rapidly, and
I am busy creating my very first Facebook application, and although I have PHP
I'm creating a very simple Java chat program, using the Java TCP sockets. I'm
So I'm working on creating a very simple C program that just preforms shell
Imagine I'm creating a very simple bug tracking system. I have a list of
I am creating a very basic encryption program using XOR to encrypt a user
I'm creating a very simple Firebug extension to capture page load data. I have
I am creating a very simple to-do list application. Each user should have an
I am currently creating a very simple bank account program: The user enters the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.