2013-07-17 74 views
0

我正在處理的任務要求我創建一個Sudoku遊戲,而不使用任何類,方法,封裝等。我對Java很陌生,對我來說是新手。我無法驗證我的用戶輸入到「fourArray」或「nineArray」中的值是否包含重複值。到目前爲止,我一直試圖使用嵌套的for-loop來遍歷任一數組的行和列。例如,我一直在努力,包括下面的一段在我的程序結束代碼,以確定是否有任何重複的值:在二維數組中尋找重複值

for (int i = 0; i < fourArray.length; i++) { 
    for (int j = i + 1; j < fourArray.length; j++) 
     if (fourArray[i] == fourArray[j]) { 
      System.out.println("No Sudoku"); 
     } else { 
      System.out.println("Sudoku!); 
     } 
} 

但是這是行不通的。我想遍歷數組以找到任何重複的任何值,如果沒有,則打印出「Sudoku!」。如果有任何重複的值,那麼我想打印出「Sudoku!」我是否需要對數組進行排序?還是有一些我不知道的方法?我已經包含了我的程序。感謝您的時間。

import java.util.Scanner; 


public class Sudoku { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     int boardSize = -1; 
     int[][] fourArray = { {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} }; 
     int[][] nineArray = { {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0} }; 
     while (true) 
     { 
      Scanner boardsizeOption = new Scanner(System.in); 
      System.out.println("Please select a board size:" + "\n" + "1) 4x4" + "\n" + "2) 9x9"); 
      boardSize = boardsizeOption.nextInt(); 
      if (boardSize == 1 || boardSize == 2) { 
       break; 
      } 
     } 
     if (boardSize == 1) { //still need to build complete board 
      int i, j = 0; 
      for (i = 0; i < fourArray.length; i++) 
      { 
       for (j = 0; j < fourArray.length; j++) 
        System.out.print(fourArray[i][j] + " "); 
       System.out.println(); 
      } 
     } else if (boardSize == 2) { 
      int i, j = 0; 
      for (i = 0; i < nineArray.length; i++) 
      { 
       for (j = 0; j < nineArray.length; j++) 
        System.out.print(nineArray[i][j] + " "); 
       System.out.println(); 
      } 
    } 
     int dataSelection = -1;  
     while (true) 
     { 
      Scanner rowColumn = new Scanner(System.in); 
      System.out.println("Please select which way you would like to enter the values:" + "\n" + "1) row" + "\n" + "2) columnn"); 
      dataSelection = rowColumn.nextInt(); 
      if (dataSelection == 1 || dataSelection == 2) { 
       break; 
      } 
     } 
     //Entering by ROWS 
     //This is for a 4x4 board size using rows 
     if (dataSelection == 1) { 
      if (boardSize == 1) { 
       int row = 1; 
       while (row < 5) { 
        String row1Values4x4 = "-1"; 
        while (true) { 
         Scanner firstRow4x4 = new Scanner(System.in); 
         System.out.println("Please enter four values using commas for row " + row); //this needs to loop 
         row1Values4x4 = firstRow4x4.next(); 
         row1Values4x4 = row1Values4x4.replaceAll(" ",""); //this is in case user enters numbers with spaces 
         if (row1Values4x4.length() == 7) { 
          break; 
         } 
        } 
        String strArray[] = row1Values4x4.split(","); 
        int arraySidesInteger[] = new int[strArray.length]; 
        for (int i = 0; i < strArray.length; i++) { 
         arraySidesInteger[i] = Integer.parseInt(strArray[i]); 
        } 
        fourArray[row-1] = arraySidesInteger; 
        for (int i = 0; i < fourArray.length; i++) { 
         for (int j = 0; j < fourArray.length; j++) 
          System.out.print(fourArray[i][j] + " "); 
         System.out.println(); 
        } 
        row++; 
       } 
       //This is for a 9x9 board size using rows 
       } else { 
        int row = 1; 
        while (row < 10) { 
         String row1Values9x9 = "-1"; 
         while (true) { 
          Scanner firstRow9x9 = new Scanner(System.in); 
          System.out.println("Please enter nine values using commas for row " + row); //this needs to loop 
          row1Values9x9 = firstRow9x9.next(); 
          row1Values9x9 = row1Values9x9.replaceAll(" ",""); //this is in case user enters numbers with spaces 
          if (row1Values9x9.length() == 17) { 
           break; 
          } 
         } 
         String strArray[] = row1Values9x9.split(","); 
         int arraySidesInteger[] = new int[strArray.length]; 
         for (int i = 0; i < strArray.length; i++) { 
          arraySidesInteger[i] = Integer.parseInt(strArray[i]); 
         } 
         nineArray[row-1] = arraySidesInteger; 
         for (int i = 0; i < nineArray.length; i++) { 
          for (int j = 0; j < nineArray.length; j++) 
           System.out.print(nineArray[i][j] + " "); 
          System.out.println(); 
         } 
         row++; 
        } 
       } 
      //Entering by COLUMNS 
      //This is for 4x4 board size using columns 
      } else { 
       if (boardSize == 1) { 
        int column = 1; 
        while (column < 5) { 
         String column1Values4x4 = "-1"; 
         while (true) { 
          Scanner firstColumn4x4 = new Scanner(System.in); 
          System.out.println("Please enter four values using commas for column " + column); 
          column1Values4x4 = firstColumn4x4.next(); 
          column1Values4x4 = column1Values4x4.replaceAll(" ",""); 
          if (column1Values4x4.length() == 7) { 
           break; 
          } 
         } 
         String strArray[] = column1Values4x4.split(","); 
         int arraySidesInteger[] = new int[strArray.length]; 
         for (int i = 0; i < strArray.length; i++) { 
          arraySidesInteger[i] = Integer.parseInt(strArray[i]); 
         } 
         for (int i = 0; i < arraySidesInteger.length; i++) { 
          fourArray[i][column-1] = arraySidesInteger[i]; 
         } 
         for (int i = 0; i < fourArray.length; i++) { 
          for (int j = 0; j < fourArray.length; j++) 
           System.out.print(fourArray[i][j] + " "); 
          System.out.println(); 
         } 
         column++; 
        } 
       //This is for a 9x9 board size using columns 
       } else { 
        int column = 1; 
        while (column < 10) { 
         String column1Values9x9 = "-1"; 
         while (true) { 
          Scanner firstColumn9x9 = new Scanner(System.in); 
          System.out.println("Please enter nine values using commas for column " + column); 
          column1Values9x9 = firstColumn9x9.next(); 
          column1Values9x9 = column1Values9x9.replaceAll(" ",""); 
          //row1Values4x4 = row1Values4x4.replaceAll(",",""); 
          if (column1Values9x9.length() == 17) { 
           break; 
          } 
         } 
         String strArray[] = column1Values9x9.split(","); 
         int arraySidesInteger[] = new int[strArray.length]; 
         for (int i = 0; i < strArray.length; i++) { 
          arraySidesInteger[i] = Integer.parseInt(strArray[i]); 
         } 
         for (int i = 0; i < arraySidesInteger.length; i++) { 
          nineArray[i][column-1] = arraySidesInteger[i]; 
         } 
         for (int i = 0; i < nineArray.length; i++) { 
          for (int j = 0; j < nineArray.length; j++) 
           System.out.print(nineArray[i][j] + " "); 


        System.out.println(); 
        } 
        column++; 
       } 
      } 
      for (int i = 0; i < fourArray.length; i++) { 
       for(int j = i + 1; j < fourArray.length; j++) { 
        if(fourArray[i] == fourArray[j]) { 
         System.out.println("No Sudoku"); 
        } else { 
         System.out.println("Sudoku!"); 
        } 
      } 
     } 
    } 
} 
+2

「不工作」是什麼意思?它不運行嗎?給出錯誤的輸出?拋出錯誤? – thegrinner

+0

因此,你的老師給你在java中的任務...並不希望你使用類? – aaronman

+3

我很高興看到你的工作Java程序沒有任何類。 – Kon

回答

0

首先,在您的整個類的代碼,你需要將你的數獨檢查下跌1 }(應該只有2之後,電源及類)。

第二件事是你認爲的,你的雙循環是錯誤的,假設我正確地理解了這個問題。如果您想對網格中的所有其他值檢查每一個值,這是我會怎麼做:

boolean sudoku = true; 
    for (int i = 0; i < fourArray.length; i++) { 
     for (int j = 0; j < fourArray[i].length; j++) { 
      if (fourArray[i] == fourArray[j]) { 
       sudoku = false; 
       break; 
      } 
      if (!sudoku){ 
       break; 
      } 
     } 
    } 
    if (sudoku){ 
     System.out.println("Sudoku!"); 
    } else { 
     System.out.println("No Sudoku!"); 
    } 
+0

你永遠不會索引到第二維,所以至少有一點缺失。 :) –

2

由於它的功課,我要儘量減少代碼,但我認爲你會精細的數字出來,如果您有任何關於二維數組一些更多的信息,其中有一些是相當棘手:

  • 由於fourArray是一個數組的數組,fourArray[i]指的是陣列(你可以把它作爲我你的二維數組的第二行)。
  • 要訪問陣列陣列中的單個整數,請使用fourArray[i][j]
  • 如果你這樣做myArray1 == myArray2(因爲你的代碼基本上在此刻做),它不會比較內容;相反,它會檢查它們是否實際上是相同的陣列(如果您先說myArray1 = myArray2會發生這種情況)。
  • 如果您確實想比較兩個陣列的內容,則可以使用Arrays.equals(myArray1, myArray2)
  • 從以上幾點可以看出,fourArray.length是一維的大小; fourArray[x].length是其他維度中的大小(其中x只要在0fourArray.length - 1之間就沒有關係)。

新增迴應評論:我的理解和假設是,你正試圖避免任何包含在2-d fourArray的值之間的任何重複的值。有很多解決方案。

可能被稱爲幼稚的解決方案是首先使用一對嵌套for循環來遍歷fourArray中的每個值。對於每個值,將其與其他每個值進行比較。您的中間代碼可能如下所示:

for (int i = 0; i < fourArray.length; i++) { 
    for (int j = 0; j < fourArray[i].length; j++) { 
     // TODO: Compare value at (i,j) to every other point by nesting 
     // two more for loops with new iterators (called, e.g., m and n) 
     // TODO: If a duplicate is found, either stop searching, or at 
     // least mark that a duplicate has been found somehow. 
    } 
} 

一方面,這有點低效。另一方面,對於小型二維陣列來說,它在計算上仍然是無足輕重的,所以如果它對您有意義,那就去做,然後繼續解決其他問題。

但是,如果您有興趣並且假設允許的值是連續集合的一部分(即在典型的數獨遊戲中您有3x3框,其中的允許值總是1-9,永遠不會更高)。如果你有一個數組count[]記錄這些已知值已經發生了多少次?所以它中的所有值都會被初始化爲零。在迭代表格中的每個點時(如上面的代碼示例所示),可以使用找到的值 - 稱爲v - 增加count[v]count[]中任何大於1的值表示重複。

+0

感謝您的回覆。我很難理解如何遍歷行和列。例如,如果我迭代行來查找重複項,我必須保持四個數組[i] = 0並且四個數組[] [k]將繼續迭代以查找重複項。有什麼建議麼? – Brandon