0

Ok i created a board that can have two types of ships, S and M, however, I want java to show me different messages whenever the board either contains only S but no M or only M but no S or when both are present. I am not able to do that. Please do help. This is what i have tried so far

import java.util.ArrayList;
import java.util.Scanner;


public class bord {
    public static void main(String[] args) {
        String Board[][]= new String[4][4];
        int n=0;
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                Board[i][j]="-";

            }
        }
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                System.out.print(Board[i][j]);
            }
            System.out.println();
        }
        System.out.println("_______________");

             //from here!
        Board[1][1]="S";
        Board[3][2]="M";

        int a=0;
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                if(Board[i][j].equalsIgnoreCase("S") && !Board[i][j].equalsIgnoreCase("M"))
                {

                    a=1;

                }
                else if(Board[i][j].equalsIgnoreCase("M") &&  !Board[i][j].equalsIgnoreCase("S"))
                    {
                        a=2;

                    }
                else if(Board[i][j].equalsIgnoreCase("S") || Board[i][j].equalsIgnoreCase("M") )
                        a=3;

            }

        }
        if(a==0)
        {
            System.out.println("No ship found");
        }
        else if(a==1)
            System.out.println("Found it");

    else if(a==2)
        {
        System.out.println("Found it yet again");
        }
    else
        System.out.println("keep playing again");
    }
}
1
  • Okay well what i would do is have two booleans and set them to true if an S and M are found in the board... Then construct if statements of what you want based off of those boolean values. Commented May 11, 2017 at 3:18

1 Answer 1

2

May be you can try this:

    boolean foundS= false;
    boolean foundM = false;
    for (int i = 0;i < 4;i ++) {
        for (int j = 0;j < 4;j ++) {
            if (Board[i][j].equalsIgnoreCase("S")) {
                foundS = true;
                continue;
            }
            if (Board[i][j].equalsIgnoreCase("M")) {
                foundM = true;
                continue;
            }
        }
    }
    if (foundS && foundM) {
        System.out.println("both");
    } else if (foundS) {
        System.out.println("found S");
    } else if (foundM) {
        System.out.println("found M");
    } else {
        System.out.println("none");
    }
Sign up to request clarification or add additional context in comments.

1 Comment

I'd prefer the names foundS and foundM for the booleans, but the general idea is right on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.