2013-11-27 63 views
-7
#include <stdio.h> 
#include <conio.h> 
#define MAXROW 10 
#define MAXCOL 10 

int main() 
{ 
    int arr2d[MAXROW][MAXCOL]; 
    int i, j, row, col; 
    int sumRow[MAXROW] = {0,0,0,0,0,0,0,0,0,0}; 
    int sumCol[MAXCOL] = {0,0,0,0,0,0,0,0,0,0}; 
    int totalRow = 0; 
    int totalCol = 0; 
    system("cls");  
    puts ("Enter the number of rows: "); 
    scanf ("%d", &row); 
    puts ("Enter the number of cols: "); 
    scanf ("%d", &col); 
    if ((row <= MAXROW) && (col <= MAXCOL)) 
    { 
     printf("Enter %d values: \n", (row * col)); 
     for (i = 0; i < row; i++) 
     { 
      for (j = 0; j < col; j++) 
      { 
       scanf("%d", &arr2d[i][j]); 
       totalRow += arr2d[i][j]; 
      } 
      sumRow[i] = totalRow; 
      totalRow = 0; 
     } 
     // getting the sum of cols 
     for (j = 0; j < col; j++) 
     { 
      for (i = 0; i < row; i++) 
      { 
       totalCol += arr2d[i][j]; 
      } 
      sumCol[j] = totalCol; 
      totalCol = 0; 
     } 
     puts("Matrix: \n"); 
     // printing 
     for (i = 0; i < row; i++) 
     { 
      for (j = 0; j < col; j++) 
      { 
       printf("\t%d\t", arr2d[i][j]); 
      } 

      printf("\t=%d", sumRow[i]); 
      printf("\n"); 
     } 

     for (i = 0; i < col; i++) 
     { 

      printf("\t=%d\t", sumCol[i]); 
     }  
     puts ("\n"); 
    } 
    else 
    { 
     puts ("row and/or col has exceeded the maximum value.");  
    } 

    system("pause"); 
} 

================================================================== 
THIS IS MY CODE IN JAVA 

import java.io.*; 
public class arr2d 
{ 

    public static void main(String[] args)throws IOException 
    { 
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
     int maxr = 10, maxc = 10; 
     int[][] arr2d = new int[maxr][maxc]; 
     int x,y,row,col,trow= 0,tcol = 0; 
     int[] sumCol = {0,0,0,0,0,0,0,0,0,0}; 
     int[] sumRow = {0,0,0,0,0,0,0,0,0,0}; 
     System.out.print("Enter the number of rows: "); row = Integer.parseInt(br.readLine()); 
     System.out.print("Enter the number of cols: "); col = Integer.parseInt(br.readLine()); 
     //process 
     if((row <= maxr) && (col <= maxc)) 
     { 
      System.out.println("Enter "+ row*col+ " values"); 
      for(x=0; x<row; x++)// i think i have the problem here 
      { 
       for(y=0; y<col; y++) 
       { 
        arr2d[x][y] = Integer.parseInt(br.readLine()); 
        trow += arr2d[x][y]; 
       } 
       sumRow[x] = trow; 
       trow = 0; 

      } 
      for(y=0; y<col; y++) 
      { 
       for(x=0; x<row; x++) 
       { 
        tcol += arr2d[x][y]; 

       } 
       sumCol[y] = tcol; 
      } 

輸入我想我對過程有一個問題,我只能在二維數組中輸入2個整數,我不能填補所有的插槽數組中這是編譯器說我想這個C代碼的Java代碼,但有一個問題即時轉換在陣列

Enter the number of rows: 2 
Enter the number of cols: 2 
Enter 4 values 
1 
2 after i enter the 2nd value this what happens 

異常線程 「main」 java.lang.NumberFormatException:對於輸入 字符串: 「」 在 java.lang.NumberFormatException.forInputString(NumberFormatException.java: 65) at java.lang.Integer.parseInt(Integer.java:504)at java.lang.Integer.parseInt(Integer.java:527)在 arr2d.main(arr2d.java:31)

+0

[Creating Two-Dimensional Array]的可能重複(http://stackoverflow.com/questions/12231453/creating-two-dimensional-array) – duffymo

+0

你知道'NumberFormatException'是什麼嗎?這可能有助於查找它。 – keyser

+2

'java.lang.NumberFormatException:對於輸入字符串:「」'該消息非常明確:您試圖將空字符串轉換爲數字,這無法成功。檢查字符串是否爲空,捕獲異常並進行相應處理,或者檢查您的代碼爲什麼字符串是空的(如果它不是用戶輸入的)。 – Thomas

回答

0

試試看,當你通過輸入字符串empty使用Scanner類代替BufferedReader的某個時候那麼你就從Integer.parseInt得到NumberFormatException
實施例,

Scanner s = new Scanner(System.in); 
System.out.println("Enter input :"); 
int text= s.nextInt(); 

Scanner類具有內置功能,可以等待,直到輸入一些所需的原語類型值。
注意nextInt,nextByte等內置功能可能會拋出InputMismatchException如果您輸入非數字值時需要數字值。

0

你得到了一個N​​umberFormatException,因爲你鍵入了一個空字符串「」而不是整數,並且這個空字符串不能被解析。

你的程序運行良好。當我按下Enter鍵而沒有輸入任何輸入時(換句話說,作爲輸入傳遞一個空字符串),我只會得到異常。

+0

感謝您的信息先生,我應該學習NumberFormatException – Trick

+0

@Trick不客氣!如果你相信我的回答對你有所幫助,你應該加倍努力。 :)乾杯。 –

0

我沒有看到任何錯誤我只是跑過去,一切正常。我也使用了你的確切測試數據。我假設你只是輸入了錯誤的數據。