java - 我想将此 C 代码转换为 java 代码,但我在输入数组时遇到问题

标签 java c arrays

#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 个整数,我无法填充数组中的所有插槽,这就是编译器所说的

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

Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:504) at java.lang.Integer.parseInt(Integer.java:527) at arr2d.main(arr2d.java:31)

最佳答案

您收到 NumberFormatException,因为您输入了空字符串“”而不是 Integer,并且无法解析该空字符串。

你的程序运行良好。仅当我按 Enter 键而不输入任何输入(换句话说,将空字符串作为输入传递)时,才会出现异常。

关于java - 我想将此 C 代码转换为 java 代码,但我在输入数组时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20243991/

相关文章:

java - 如何 move 棋盘?

c - 错误 : "invalid use of incomplete type ‘RSA {aka struct rsa_st}" in OpenSSL 1. 1.0

arrays - 在 Python 中乘以矩阵数组的最快方法(numpy)

python - 为什么 string.encode ('utf-8' ) != bytes(map(ord, string)) 是真的?

java - 奇怪的浮点错误?

java - 像这样的场景的生产者消费者模型不起作用

JavaFX 将超链接绑定(bind)到标签

c - strtof() 在 C 中产生奇怪的结果

c - 为什么 stdout 在重定向到文件时需要显式刷新?

javascript - javascript 中参数的范围和对象可变性