Michał Gwóźdź
Michał Gwóźdź

Reputation: 105

strange behaviour with if statement

I'm trying to do simple, one dimension "ships game". I'm stuck on randomly generating ships. I have array with 10 cells. First ship will take 3 cells, second 2 and third 1. So I made ship as an object with constructor d = dlugosc (their length). Now i'm writing a method that will randomly place them in my array. Here is my whole, short code:

package statki1;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Statki1 {


static int[] array = new int[10];
static int dlugosc;
static Random r = new Random();

//constructor for ships
public Statki1(int d) {
    dlugosc = d;
}

//ships as objects
static Statki1 xxx = new Statki1(3);
static Statki1 xx = new Statki1(2);
static Statki1 x = new Statki1(1);

//method which will randomly place ships
public static void losowanie3() {
    int s = r.nextInt(array.length);
    array[s] = 2;
    if (array[0] == 2) {
        array[s+1] = 2;
        array[s+2] = 2;
        array[s+3] = 1;
    }
    System.out.println(s);
}

public static void main(String[] args) `enter code here`{

    Scanner input = new Scanner(System.in);
    int choose;
    xxx.losowanie3();
    System.out.println(Arrays.toString(array));

    }
}

and now it works fine and my array looks like this:

[2, 2, 2, 1, 0, 0, 0, 0, 0, 0]

But in main I will do sth like this

xxx.losowanie3;
xx.losowanie3;
x.losowanie3

so I need to add a condition to my method, so it should look like this:

if (array[0] == 2 & dlugosc == 3) {
        array[s+1] = 2;
        array[s+2] = 2;
        array[s+3] = 1;
    }

But it doesn't work. Now if array[0] == 2 my array looks like this:

[2, 0, 0, 0, 0, 0, 0, 0, 0, 0]

and should be like this: [2, 2, 2, 1, 0, 0, 0, 0, 0, 0]

Can anyone help me solve this problem? Regards

Upvotes: 0

Views: 107

Answers (2)

jfigueroa
jfigueroa

Reputation: 16

The if statement never actually executes when you add the statement:

if (array[0] == 2 & dlugosc == 3) {
    array[s+1] = 2;
    array[s+2] = 2;
    array[s+3] = 1;
}
System.out.println("Value of dlugosc is " + xxx.dlugosc);

This prints out the output:

The value of dlugosc is 1
[2, 0, 0, 0, 0, 0, 0, 0, 0, 0]

For the if statement to execute, the variable "s" has to equal 0 and the variable "dlugosc" has to equal 3. Even when the random number assigned to the variable "s" is 0 (which I had to run the program multiple times to get 0), the if statement will not execute because the value of the variable "dlugosc" is 1.

You marked the variable "dlugosc" as a static variable. This means there is only one copy of the variable in the class.

The problem seems to be in the code block:

//ships as objects
static Statki1 xxx = new Statki1(3);
static Statki1 xx = new Statki1(2);
static Statki1 x = new Statki1(1);

The last line in this code block assigns the value of 1 to the variable "dlugosc" and since there is only one copy of the variable, the if statement will fail to execute. When I commented out the second and third line as:

//ships as objects
static Statki1 xxx = new Statki1(3);
// static Statki1 xx = new Statki1(2);
// static Statki1 x = new Statki1(1); 

Then ran the program multiple times until the variable "s" was equal to 0. This prints the desired output:

The value of dlugosc is 3
[2, 2, 2, 1, 0, 0, 0, 0, 0, 0]

Maybe you should modify the program so that every object has its own copy of that variable.

Upvotes: 0

Stephen Sugumar
Stephen Sugumar

Reputation: 545

It's because your s value can be anything from 0-10.

since s can be any number in the size of the array, only when s == 0 will your code behave the way you want it to.

Maybe you should reconsider choosing s instead of using a random class to generate the value of s

Upvotes: 4

Related Questions