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

  • Home
  • SEARCH
  • 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 8671985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:05:25+00:00 2026-06-12T19:05:25+00:00

I am writing a battleship program. Right now I am testing a couple lines

  • 0

I am writing a battleship program. Right now I am testing a couple lines of code to see if it will place the boat going in the up direction. How my program is set up is that if, for example, the user clicks on the aircraft carrier button to set his aircraft carrier, the program should also set the ai’s aircraft carrier. The boats are placed on a button array, called tlba. aifirstclicki is set by a random generator so that it will choose a random row. aifirstclickj chooses a random column, in conjunction the two pinpoint a spot on the button array (which is 10×10). I wrote the following code to try to make it so that if the program has an outofboundsexception error,or in other words if the program chooses a first spot that will eventually cause an outofbounds exception error because the for loop will keep adding spots until aiclickcount = 5, it should start over and pick a different spot until it finds a spot that will allow it to place all 5 spots. I keep getting stuck in an infinite loop though.

    int aiclickcount = 0;
    while (directiondecider == 0)
    {//up
        aifirstclicki = generator.nextInt(10);
        aifirstclickj = generator.nextInt(10);
        while (aifirstclicki != 3 &&
               aifirstclicki != 2 &&
               aifirstclicki != 1 &&
               aifirstclicki != 0)
        {
            for(int k=0; k<shiplength; k++)
            {
                tlba[aifirstclicki - k][aifirstclickj].setBackground(Color.RED);
                aistringarray[aifirstclicki - k][aifirstclickj] = "aircraftcarrier";
                aioccupied2d[aifirstclicki - k][aifirstclickj] = true;
                aiclickcount++;
            }

            if (aiclickcount == 5)
            {
                shipset = true;
                break;
            } 
        }
        System.out.println(shipset);
    }

Does anyone know what’s wrong or have a different solution to my problem?

  • 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-12T19:05:27+00:00Added an answer on June 12, 2026 at 7:05 pm

    You never have aiclickcount == 5 if your shiplength is not 5. Put if into your for loop. You don’t need the second while at all, you don’t break out of it as well. Just generate number greater than 3 by nextInt(6) + 4.

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

Sidebar

Related Questions

Writing Classic ASP code in JScript has a lot going for it: more humane
When writing code, I often place debug messages in the code. The debug messages
Writing a simple program that will find exact duplicate files on my computer, but
Writing if/else statements in the middle of jQuery code doesn't feel right: if ($(#test).length)
Writing a test app to emulate PIO lines, I have a very simple Python/Tk
Writing documentation in html requires some code examples. What to do with characters that
Writing a python program, and I came up with this error while using the
Writing a program in which I need to split strings from a struct linked
Writing an application in Cocoa that will take input from the user, format it
Writing a code that does the following: Adds a mouse listener to a web-page

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.