如何运行多个类的程序?

时间:2014-11-14 04:56:29

标签: java

我正在为学校做一个项目,我已经获得了一些我需要修改的代码。但问题是我无法让代码首先运行。我知道我遇到了类路径和包的问题,​​因为我以前从未使用它们。这些是我的课程:

import java.util.Scanner;
import java.io.*;

public class SudSolve {
    public static void main(String[] args) {
        try {
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter Sudoku file name");
            String name = scan.next();
            ReadBoard r = new ReadBoard(name);
            r.readLines();
            SudBoard b = new SudBoard(r.getBoardNums());
            b.display();
            b.solve();
        } catch (IOException e) {
            System.out.println(e);
        }
    }
}

import java.util.Scanner;
import java.io.*;

public class Echo {
String fileName; // external file name
Scanner scan; // Scanner object for reading from external file

public Echo(String f) throws IOException{
    fileName = f;
    scan = new Scanner(new FileReader(fileName));
}

public void readLines() { // reads lines, hands each to processLine
    while (scan.hasNext()) {
        processLine(scan.nextLine());
    }
    scan.close();
}

public void processLine(String line) { // does the real processing work
    System.out.println(line);
    }
}

package sudoku;

public class MalformedBoardException extends Exception {
    public MalformedBoardException() {
    super();
}

public MalformedBoardException(String comment) {
    super(comment);
    }
}

import java.io.IOException;
import java.util.StringTokenizer;

public class ReadBoard extends Echo {
// assumes board entries are of the form: 9 rows of 9 nums
private int[] boardNums = new int[81];
private int curRow = 0;

public ReadBoard(String filename) throws IOException {
    super(filename);
}

public void processLine(String line) {
    StringTokenizer s = new StringTokenizer(line);
    int pos = 0;
    int n;
    while (s.hasMoreTokens()) {
        n = Integer.parseInt(s.nextToken());
        boardNums[9 * curRow + pos] = n;
        pos++;
    }
    curRow++;
}

public void checkLegality() throws MalformedBoardException {
    for (int i = 0; i < boardNums.length; i++) {
        if (boardNums[i] < 0 || boardNums[i] > 9) {
            throw new MalformedBoardException("Board incorrect " + i + " "
                    + boardNums[i]);
        }
    }
}

public int[] getBoardNums() {
    return boardNums;
    }
}


/* The Sudoku board has 9 rows and 9 columns = 81
 cells. Cells either have a row/col designation
 e.g., row 4, col 7, or (4,7), or a single int 
 designation, numbered 0-80. Latter numbering works
 left to right, top to bottom. Sudoku divides
 the board into nine zones, and the array zoneKeys
 gives the cell numbers that correspond to the NW
 corner cell of each zone
 */
public class SudBoard {
    final static int size = 9;
    final static int maxVal = 9;
    final int[] zoneKeys = { 0, 3, 6, 27, 30, 33, 54, 57, 60 };
    private int[][] cells = new int[size][size];
    int count = 0;

public SudBoard(int[] nums) {
    // passed length 81 array - the start board
    int j = 0; // counts though nums
    for (int r = 0; r < size; r++) {
        for (int c = 0; c < size; c++) {
            cells[r][c] = nums[j];
            j++;
        }
    }
}

public SudBoard install(int where, int whatVal) {
    // param where is a 0-80 val; r,c = row/col version
    int r = where / size;
    int c = where % size;
    this.cells[r][c] = whatVal;
    return this;
}

public boolean impossible() {
    // is current board impossible?
    boolean ans = false;
    ans = rowImpossible() || colImpossible() || zoneImpossible();
    return ans;
}

public boolean possible() {
    return !impossible();
}

public boolean rowImpossible() {
    // is one of the rows impossible?
    boolean[] boo = new boolean[maxVal + 1];
    for (int k = 0; k < boo.length; k++)
        boo[k] = false;
    for (int r = 0; r < maxVal; r++) {
        for (int c = 0; c < maxVal; c++) {
            int v = cells[r][c];
            if ((v > 0) && (boo[v] == true))
                return true;
            else
            {
                boo[v] = true;
            }
        }
        for (int k = 0; k < boo.length; k++)
            boo[k] = false;
    }
    return false;
}

public boolean colImpossible() {
    // is one of columns impossible?
    boolean[] boo = new boolean[maxVal + 1];
    for (int k = 0; k < boo.length; k++)
        boo[k] = false;
    for (int c = 0; c < maxVal; c++) {
        for (int r = 0; r < maxVal; r++) {
            int v = cells[r][c];
            if ((v > 0) && (boo[v] == true))
                return true;
            else
            {
                boo[v] = true;
            }
        }
        for (int k = 0; k < boo.length; k++)
            boo[k] = false;
    }
    return false;
}

public boolean zoneImpossible() {
    // is one of the zones impossible?
    boolean[] boo = new boolean[maxVal + 1]; // 0 - 9 for Sudoku
    boolean ans = false; // true means: a zonal conflict, so impossible
    for (int j = 0; j < zoneKeys.length; j++) {
        for (int k = 0; k < boo.length; k++)
            boo[k] = false;
        ans = zoneTest(zoneKeys[j], boo);
        if (ans)
            return true; // there IS a zone clash
    }
    return false;
}

private boolean zoneTest(int j, boolean[] boo) {
    // tests 3x3 zone with cell# j as NW corner
    int r = j / size;
    int c = j % size; // row/col version of j
    for (int i = r; i < r + 3; i++)
        for (int m = c; m < c + 3; m++) {
            int v = cells[i][m];
            if (v > 0) {
                if (boo[v] == true)
                    return true;
                else
                    boo[v] = true;
            }
        }
    return false;
}

public int firstEmpty() {
    // visits cells in English reading order L to R, top to bot
    for (int j = 0; j < size * size; j++) {
        int r = j / size;
        int c = j % size;
        if (cells[r][c] == 0)
            return j;
    }
    return -1; // -1 means: all full
}

public int wiggle(int r, int c) {
    // how many ints 1-9 are possible at cell (r,c)
    if (cells[r][c] != 0)
        return 0;
    else {
        boolean[] covered = new boolean[10]; // awkward to avoid 0
        covered[0] = true;
        rowCovered(r, covered);
        colCovered(c, covered);
        zoneCovered(r, c, covered);
        int ct = 0;
        for (boolean b : covered)
            if (!b)
                ct++;
        return ct;
    }
}

private void zoneCovered(int r1, int c1, boolean[] b) {
    int z = getZoneKey(r1, c1);
    int r = z / size;
    int c = z % size;
    for (int i = r; i < r + 3; i++)
        for (int m = c; m < c + 3; m++) {
            int v = cells[i][m];
            if (v > 0)
                b[v] = true;
        }
}

private int getZoneKey(int r, int c) {
    // given a cell, what zone is it in (id'd by key)
    return (zoneKeys[3 * (r / 3) + c / 3]);
}

private void rowCovered(int r, boolean[] b) {
    for (int c = 0; c < 9; c++) {
        int k = cells[r][c];
        b[k] = true;
    }
}

private void colCovered(int c, boolean[] b) {
    for (int r = 0; r < 9; r++) {
        int k = cells[r][c];
        b[k] = true;
    }
}

private boolean filled(int i, int j) {
    return (cells[i][j] > 0);
}

public void display() { // prints board
    for (int r = 0; r < size; r++) {
        System.out.println();
        for (int c = 0; c < size; c++) {
            System.out.print(cells[r][c] + " ");
        }
    }
    System.out.println();
}

public boolean solve() {
    count++;
    int w = firstEmpty();
    if ((w == -1) && possible()) // -1 -> no empties
    {
        System.out.println("legal -- recur count: " + count);
        display();
        return true;
    }
    else if (impossible())
        return false;
    else
    {
        boolean ans = false;
        for (int k = 1; k <= 9; k++) { // try all possible values
            install(w, k);
            ans = solve();
            if (ans)
                break; // found a solution, so break out
        }
        if (!ans)
            install(w, 0); // no soln - put zero back
        return ans;
        }
    }
}

正如你所看到的,这个程序应该可以解决数独难题。正确设置它的任何帮助都会很棒!

0 个答案:

没有答案