2017-03-29 16 views
0

我想寫一個java程序,打印用戶輸入3x3多維數組是否是馬爾可夫矩陣;提示如下。檢查用戶輸入3x3多維數組是否是一個馬爾可夫矩陣

- 如果每個 -element爲正,且每列中的元素總和爲1,則將n×n矩陣稱爲正馬爾可夫矩陣。編寫以下方法以檢查矩陣是否爲馬爾可夫矩陣。

public static boolean isMarkovMatrix(double [][] m)

-Write的測試程序(主法),提示用戶輸入雙 - 值和測試是否它是一個馬爾可夫矩陣的3×3 --matrix。

我的代碼輸出打印出來,不管我輸入什麼,它都不是一個馬爾科夫矩陣,也不知道爲什麼它會這樣做。作爲一個方面說明,我不知道任何複雜的java,並且只在課上幾周,所以下面是我的java編程技巧的程度。任何幫助,將不勝感激!先謝謝你。

import java.util.Scanner; 

public class Problem3 { 

    public static void main(String[] args) { 
     //declarations 
     double[][] markov = new double[3][3]; 
     Scanner keyboard = new Scanner(System.in); 

     //input 
     System.out.print("Please enter numbers to fill up a 3x3 matrix: "); 
     for (int i = 0; i < markov.length; i++) { 
      for (int x = 0; x < markov.length; x++) { 
       markov[i][x] = keyboard.nextDouble(); 
      } 
     } 

     //output 
     if (isMarkov(markov) == true) { 
      System.out.print("Your matrix is a Markov Matrix!"); 
     } else if (isMarkov(markov) == false) { 
      System.out.print("Your matrix is not a Markov Matrix!"); 
     } 

     //close keyboard 
     keyboard.close(); 

    } 

    //method to check whether the matrix is positive 
    public static boolean isPositive(double[][] array) { 
     //declarations 
     boolean answer = true; 

     //processing 
     for (int i = 0; i < array.length; i++) { 
      for (int x = 0; x < array.length; x++) { 
       if (array[i][x] < 0) { 
        answer = false; 
       } 
      } 
     } 
     return answer; 
    } 

    //method to check whether the sum of the columns are 1 
    public static boolean columnSum(double[][] array) { 
     //declarations 
     double sum = 0; 
     boolean answer = true; 

     //processing 
     for (int i = 0; i < array.length; i++) { 
      for (int x = 0; x < array.length; x++) { 
       sum += array[i][x]; 
      } 
      if (sum != 1) { 
       answer = false; 
      } 
     } 
     return answer; 
    } 

    //method to check whether the matrix is a Markov matrix 
    public static boolean isMarkov(double[][] array) { 
     //declarations 
     boolean answer = false; 

     //processing 
     if ((isPositive(array) == true) && (columnSum(array) == true)) { 
      answer = true; 
     } 
     return answer; 

    } 

} 
+0

在你的columSum方法中,pb是sum!= 1。由於浮點,你不會比較那樣的雙倍。使用Java.lang.Double.compare().https://www.tutorialspoint.com/java/lang/double_compare.htm – Incognito

回答

0

您的問題是在columnSum(雙[] []數組)功能。 總和值不會在每列之後重置。這意味着您的代碼總計值總和中的所有列。只需將總和重置爲零,以防止出現這種情況。如果還有其他問題,請讓我知道,我會再看一遍。

//method to check whether the sum of the columns are 1 
public static boolean columnSum (double[][] array) 
{ 
    //declarations 
    double sum = 0; 
    boolean answer = true; 

    //processing 
    for (int i = 0; i < array.length; i++) 
    { 
     sum = 0; //-----------------added----------------- 
     for (int x = 0; x < array.length; x++) 
     { 
      sum += array[i][x]; 
     } 
     if (sum != 1) 
     { 
      answer = false; 
     } 
    } 
    return answer; 
} 

在另一個說明,只是一個小東西,因爲你對編碼有點新鮮。當評估布爾值時,您不需要添加== true== false。該值本身是布爾值。這裏給出的例子:

//instead of 
if (flag == false){} 
//do 
if (!flag){} 

如果你不知道,「!」否定表達。如果你想評估「真」,只需刪除「!」。

+0

感謝您的信息,不知道!但我已經將總數重置爲0,它仍然說所有內容都不是馬爾可夫矩陣:( –

+0

好吧,我只是在3行中對自己和(0.3 0.3 0.4)進行了測試,它工作正常,並且說它是真的是一個馬爾可夫矩陣,請給我一個它應該工作的測試用例,但不是。 – KehxD

+0

哦,別介意它的工作我不小心輸入了錯誤的數字哈哈感謝您的幫助讚賞它:) –