Practical Tasks - Chess Square Color

Practical Tasks - Chess Square Color
0

Hi,

I wrote the following code for the exercise in the Practical Tasks - Chess Square Color (https://learn.telerikacademy.com/mod/page/view.php?id=5378)

Intellij shows me correct result when I run it, but the code is rejected with reason: [ Invalid Return]

Any tips?

import java.util.Scanner;
public class testing {
public static void main(String[] args) {
gets();
}

private static void gets() {
    Scanner sc = new Scanner(System.in);
    char L = sc.nextLine().charAt(0);
    int R = sc.nextInt();
    int number = 0;
    switch (L) {
        case 'a':
            number = 0;
            break;
        case 'b':
            number = 1;
            break;
        case 'c':
            number = 2;
            break;
        case 'd':
            number = 3;
            break;
        case 'e':
            number = 4;
            break;
        case 'f':
            number = 5;
            break;
        case 'g':
            number = 6;
            break;
        case 'h':
            number = 7;
            break;

    }

    int[][] board = {
            {0, 1, 0, 1, 0, 1, 0, 1},
            {1, 0, 1, 0, 1, 0, 1, 0},
            {0, 1, 0, 1, 0, 1, 0, 1},
            {1, 0, 1, 0, 1, 0, 1, 0},
            {0, 1, 0, 1, 0, 1, 0, 1},
            {1, 0, 1, 0, 1, 0, 1, 0},
            {0, 1, 0, 1, 0, 1, 0, 1},
            {1, 0, 1, 0, 1, 0, 1, 0},
    };
    if (R <=8){
        if (board[number][R] == 0) {

            System.out.println("light");
        } else {
            System.out.println("dark");
        }


    }
}

}

Hi,
element in your array is from 0 to 7. A chessboard ranks have values from 1 to 8. One posibility is to change if statement like this:
if (R <=8){
if (board[number][R - 1] == 0) {

                System.out.println("dark");
            } else {
                System.out.println("light");
            }
1 Like