Domanda

I have a question about a problem I'm having with a code i am writing. I need to create a Sudoku board checker. The goal is to extend the Sudoku class (to make the CheckableSudoku class) by using the the 5 specific methods I have to use public boolean checkAll( ) to check all of the methods and return true if they pass. Heres my code! Sorry if i explained this too confusingly :/

import java.util.Scanner;
public class CheckableSudoku extends Sudoku
{
    public int getCell(int xColumn, int yRow)
    {
        return this.board[xColumn][yRow];
    }

    public boolean checkRow (int yCoord)
    {
        int sum = 0;

        for ( int x = 0; x <9; x++)
        {
            sum = sum + getCell (x,yCoord);
        }
    return( true );
    }

    public boolean checkColumn (int xCoord)
    {
        int sum = 0;
        for (int y = 0; y < 9 ;y++)
        {
            sum = sum + getCell (xCoord, y);
        }
        return( true );
    }

    public boolean checkBlock (int col0to2, int row0to2)
    {
        int sum = 0;
        for (int x=0; x<3; x++)
        {
            for (int y=0; y<3;y++)
            {
                sum = sum + getCell (col0to2+x, row0to2+y);
            }
        }
        return( true);
    }

    public boolean checkAll()
    {
    // this is the method that checks all the other methods above
    return true;
    }

    public static void main(String[] a)
    {
        CheckableSudoku me = new CheckableSudoku();
        Scanner sc = new Scanner(System.in);
        me.read(sc);
        System.out.print(me);

        System.out.println(me.checkAll());
    }
}
È stato utile?

Soluzione

Here's an example on how you can check if they all return true or not:

public boolean checkAll() {
    return (method1() && method2() && method3() && method4() && method5());
}

OR:

// Same thing but more typing.
public boolean checkAll() {
    if (method1() && method2() && method3() && method4() && method5())
         return true;
    else 
         return false;
}

So far all your methods seem to be returning true anyway. Not sure if its just an example or not. If not, you need to check your logic.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top