2016-05-06 20 views
2

我試圖做一個Java程序,它使用用戶的輸入值來計算並列出兩個數字的產品,直到輸入的數字。就像用戶輸入2一樣,程序應該計算兩個數字之間的產品(1 * 1,1 * 2,2 * 1,2 * 2)將產品存儲在二維數組中,並列出產品。我不確定自己完全理解了數組,所以我覺得好像我的代碼在許多情況下都不是正確的,有人可以告訴我應該如何使我的當前代碼正常工作。提前致謝! :)我需要對當前的代碼做些什麼來使程序運行乘法表(使用二維數組)?

import java.util.Scanner; 

public class ProductTable { 
    public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 
    String inputString; 
    char letter = 'y'; 


    // Prompt the user to enter an integer 
    while(letter != 'q') { 
    System.out.print("Enter a positive integer: "); 
    int integer = input.nextInt(); 

    // Create an two-dimensional array to store products 
    int[][] m = new int[integer][3]; 
    for (int j = 1; j <= m.length; j++) { 
     m[j][0] = input.nextInt(); 
     m[j][1] = input.nextInt(); 
     m[j][2] = input.nextInt(); 
    } 

    // Display the number title 
    System.out.print(" "); 
    for (int j = 1; j <= m.length; j++) 
     System.out.print(" " + j); 

    System.out.println("\n--- "); 

    // Display table body 
    for (int i = 1; i <= m.length + 1; i++) { 
     System.out.print(i); 
     for (int j = 1; j <= m.length + 1; i++) { 
      System.out.printf("%4d", i * j); 

     } 
     System.out.println(); 
    } 



     // Prompt the user to either continue or quit 
    System.out.print("Enter q to quit or any other key to continue: "); 
    String character = input.nextLine(); 
    inputString = input.nextLine(); 
    letter = inputString.charAt(0); 
    } 

    } 
}  
+0

根據你的描述,你應該只是要求用戶輸入一個數字,然後創建數組並計算所有的值,這意味着你應該只在代碼中有一個**調用input.nextInt() 。現在,如果您輸入'5'作爲第一個數字,則需要用戶輸入5 * 3 = 15 *多個*號碼。 – Andreas

+0

以及現在好我的代碼出於某種原因不會做任何事情過去要求用戶輸入一個整數我不知道如何讓它識別代碼的其餘部分? –

+0

「(1 * 1,1 * 2,2 * 1,2 * 2)」並不能說明您打算做什麼 - 特別是1 * 1。當用戶給出4時,會發生什麼?爲什麼在你的代碼中0,1,2硬編碼? –

回答

0

您可以通過遍歷2櫃檯變量實現自己的乘法表因爲你已經在你的輸出做

// Display table body 
// loops running out of bounds (until m.length + 1 instead of m.length-1) 
for (int i = 1; i <= m.length + 1; i++) { 
    System.out.print(i); 
    for (int j = 1; j <= m.length + 1; i++) { // missed to increment j here 
     System.out.printf("%4d", i * j); 

    } 
    System.out.println(); 
} 

陣列應基於0,這意味着有3個字段的數組索引0,1,2。所以最後一個索引是length-1。您的狀況<=m.length+1超出限制。 index<length會工作,因爲2小於3,而不是3小於3

你也有一個錯字:在內環你正在做的i代替j增量,因此該循環將運行無限。

嘗試按照以下方式創建外部和內部循環,但啓動索引0和結束條件index<inputValue。計算multiTable[index1][index2] = (1+index1)*(1+index2)

然後做一個類似的循環並打印數組字段。你不需要使用數組,你可以直接輸出。但是你想練習處理數組。


既然你想學習和理解,我不喜歡只寫代碼。想象一個陣列,具有索引0,1,2 3個字段:

index 0 | 1 | 2 
value 1 | 2 | 3 

現在你會檢查在第一的長度,這是3.您從0開始和計數,而計數器不到達長度因爲基於零的指數比我們的自然指數(0,1,2與1,2,3)低1。只要指數低於該長度,情況就是如此,這意味着index<length


試試這個邏輯(僞代碼)。 爲了簡化問題,我們在產品列表中加入了0。所以,我們得到0 * 0,0 * 1,0 * 2 ...這樣做,我們沒有忽略指數0或索引0之間計算應代表值1

maxNumber = userInput() 

outer loop idx1 from 0 to maxNumber 
    inner loop idx2 from 0 to maxNumber 
    array[idx1][idx2] = (idx1) * (idx2) 
    end loop 
end loop 

用同樣的方法將您生成的數組轉換爲屏幕。

第一個讓它運行之後您可以嘗試更改邏輯,以便它只顯示從1到最大的數字。

+0

嗯,我不能測試,如果這使得它的工作與否,因爲由於某種原因程序只會要求我輸入一個整數,並沒有真的做別的。不過謝謝,如果我能弄清楚爲什麼它會這樣做,我會試着修復你告訴我的。 –

+0

因爲我已經寫過答案,所以基本上存在索引超出限制的問題。首先解決這個問題。 –

+0

對不起,我不明白我應該如何重做外部和內部循環?我解決了輸入錯誤和條件超出範圍的問題,但它仍然不允許我在輸入整數之前進行任何操作。 –

相關問題