简体   繁体   中英

Sudoku style program in java

This is a Sudoku style program using only 'A, B, C' and like in Sudoku, there can not be two or more of the same letter in each row or column but there can be two or more of the same letter in a diagonal. This 'Sudoku game is played in a 3x3 square'. Each box in the square is numbered 1-9 [due to the location of that box] where one is the top left box and two is the top middle box... and nine is the right edge box. The user will input a line which will contain the number of letters in the grid at the start of the game followed by their location, and the first digit would be how many locked in letters. A locked letter is a letter that can not be moved.

This is my code:

 import java.util.Scanner; public class ABC { public static void main(String[] args) { Scanner S = new Scanner(System.in); System.out.println("Input:"); String IS = S.nextLine(); String[] SIS = IS.split(", "); int LOSIS = (SIS.length)-1; System.out.println(LOSIS); String[] location = new String[9]; // |_|_|_|_|_|_|_|_|_| for(int i = 1;i<=((SIS.length)-1)/2;i++){ System.out.println("In the loop"); int dummy = Integer.parseInt(SIS[i]); location[dummy] = SIS[i+1]; System.out.println(location[dummy]); i++; } String[] top = {location[0],location[1],location[2]}; String[] middle = {location[3],location[4],location[5]}; String[] bottom = {location[6],location[7],location[8]}; for(int i = 1; i>0; i++){ } } } 
I am able to get the user's input and add the locked alphbets in my array, but I do not know how to fill in everything else.

I cleaned up your code. Java variables start with a lower case letter. I changed some of your short variable names to longer, more descriptive names. I put blank lines in to group the code. I didn't know if you studied methods yet. Each code group should be a separate method.

import java.util.Scanner;

public class ABC {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Input:");
        String locationString = scanner.nextLine();
        String[] locationDigits = locationString.split(", ");
        int locationDigitsLength = (locationDigits.length) - 1;
        System.out.println(locationDigitsLength);
        String[] location = new String[9];

        // |_|_|_|_|_|_|_|_|_|
        for (int i = 1; i <= ((locationDigits.length) - 1) / 2; i++) {
            System.out.println("In the loop");
            int dummy = Integer.parseInt(locationDigits[i]);
            location[dummy] = locationDigits[i + 1];
            System.out.println(location[dummy]);
        }

        String[] top = { location[0], location[1], location[2] };
        String[] middle = { location[3], location[4], location[5] };
        String[] bottom = { location[6], location[7], location[8] };

        for (int i = 1; i < location.length; i++) {

        }

        scanner.close();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM