如何将int数组转换为双数组java?

如何将int数组转换为双数组java?,java,arrays,Java,Arrays,目前正在进行一个项目,该项目要求学生剩余的课程数量、每学期的课程数量,并返回毕业剩余的学期数量。我在弄清楚如何将整数数组转换为双数组以计算所需的项数时遇到了一些困难。此外,还需要对结果进行汇总。我对这一点非常陌生,所以对于如何清理我的代码,任何建议都将受到高度赞赏和批评,提前感谢 import java.util.Scanner; public class main { public static void main (String[] args) { Scanner in

目前正在进行一个项目,该项目要求学生剩余的课程数量、每学期的课程数量,并返回毕业剩余的学期数量。我在弄清楚如何将整数数组转换为双数组以计算所需的项数时遇到了一些困难。此外,还需要对结果进行汇总。我对这一点非常陌生,所以对于如何清理我的代码,任何建议都将受到高度赞赏和批评,提前感谢

import java.util.Scanner;

public class main {

public static void main (String[] args) {
    
    Scanner input = new Scanner (System.in);

    System.out.print("Enter the number of rows: ");
    int rows = input.nextInt();
    System.out.print("Enter the number of columns: ");
    int columns = input.nextInt();
    
    int [][] studentArray = new int [rows][columns];
    
    
    for (int i = 0; i <= rows-1; i++) {
        for (int j = 0; j <= columns-1; j++) {
             
            if (j==0) {
                System.out.print("Please enter the number of classes left for student " +(i+1)+ " : ");
                studentArray[i][j] = input.nextInt();
                }
            
            else if (j>0) {
                System.out.print("Enter the number of classes taken per term : ");
                studentArray[i][j] = input.nextInt();
                
                while (studentArray[i][j] >= 6) {
                    System.out.println("The number of classes per term for student " +(i+1)+ " is not valid!");
                    System.out.print("Enter the number of classes taken per term : ");
                    studentArray[i][j] = input.nextInt();
                }
                }
            }
        }
    
    divide(studentArray);
}

public static void divide(int termsLeft[][]) {
for (int k = 0; k < termsLeft.length; k++) {
    double result = termsLeft[k][0] / termsLeft[k][1];
    if (k>=0) {
    System.out.println("Student " +(k+1)+ " has " + result + " terms left to graduate.");
    }
    }
}
import java.util.Scanner;
公共班机{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.print(“输入行数:”);
int rows=input.nextInt();
System.out.print(“输入列数:”);
int columns=input.nextInt();
int[][]studentArray=新的int[行][列];
对于(int i=0;i=6){
System.out.println(“学生“+(i+1)+”的每学期课程数无效!”);
System.out.print(“输入每个学期的课程数量:”);
studentArray[i][j]=input.nextInt();
}
}
}
}
划分(学生区);
}
公共静态无效分割(int termsLeft[]]{
对于(int k=0;k=0){
System.out.println(“学生”+(k+1)+“有”+result+“待毕业的术语”);
}
}
}

}

首先,它们是您的代码中的一些问题,这些问题使得代码非常低效

(一)

他们不需要使用if语句,因为它总是正确的

现在来回答你的问题

您可以使用Math.round方法将双精度值舍入为long(可以存储比int更大的值)

那么您的最终代码如下:

import java.util.Scanner;

public class Main {

    public static void main (String[] args) {

        Scanner input = new Scanner (System.in);

        System.out.print("Enter the number of rows: ");
        int rows = input.nextInt();
        System.out.print("Enter the number of columns: ");
        int columns = input.nextInt();

        int [][] studentArray = new int [rows][columns];


        for (int i = 0; i <= rows-1; i++) {
            for (int j = 0; j <= columns-1; j++) {
                if (j==0) {
                    System.out.print("Please enter the number of classes left for student " +(i+1)+ " : ");
                    studentArray[i][j] = input.nextInt();
                }

                else if (j>0) {
                    System.out.print("Enter the number of classes taken per term : ");
                    studentArray[i][j] = input.nextInt();

                    while (studentArray[i][j] >= 6) {
                        System.out.println("The number of classes per term for student " +(i+1)+ " is not valid!");
                        System.out.print("Enter the number of classes taken per term : ");
                        studentArray[i][j] = input.nextInt();
                    }
                }
            }
        }

        divide(studentArray);
    }

    public static void divide(int[][] termsLeft) {
        for (int k = 0; k < termsLeft.length; k++) {
            double result = termsLeft[k][0]/termsLeft[k][1];
            long round_result = Math.round(result);
            System.out.println("Student " +(k+1)+ " has " + round_result + " terms left to graduate.");
        }
    }

}
import java.util.Scanner;
公共班机{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.print(“输入行数:”);
int rows=input.nextInt();
System.out.print(“输入列数:”);
int columns=input.nextInt();
int[][]studentArray=新的int[行][列];
对于(int i=0;i=6){
System.out.println(“学生“+(i+1)+”的每学期课程数无效!”);
System.out.print(“输入每个学期的课程数量:”);
studentArray[i][j]=input.nextInt();
}
}
}
}
划分(学生区);
}
公共静态无效分割(int[][]termsLeft){
对于(int k=0;k
铸造不是转换;它根本不会更改基础数据值。
int[]
是一个
int[]
,而不是一个
double[]
。当你说“四舍五入”时,你的意思是,例如2.1、…、2.5、…、2.9将全部四舍五入到3吗?或者2.1。。。2.4会四舍五入到2,2.5,2.6,…,2.9会四舍五入到3?
if (k>=0) {
    System.out.println(...)
    }
    double result = termsLeft[k][0]/termsLeft[k][1];
    long round_result = Math.round(result);
import java.util.Scanner;

public class Main {

    public static void main (String[] args) {

        Scanner input = new Scanner (System.in);

        System.out.print("Enter the number of rows: ");
        int rows = input.nextInt();
        System.out.print("Enter the number of columns: ");
        int columns = input.nextInt();

        int [][] studentArray = new int [rows][columns];


        for (int i = 0; i <= rows-1; i++) {
            for (int j = 0; j <= columns-1; j++) {
                if (j==0) {
                    System.out.print("Please enter the number of classes left for student " +(i+1)+ " : ");
                    studentArray[i][j] = input.nextInt();
                }

                else if (j>0) {
                    System.out.print("Enter the number of classes taken per term : ");
                    studentArray[i][j] = input.nextInt();

                    while (studentArray[i][j] >= 6) {
                        System.out.println("The number of classes per term for student " +(i+1)+ " is not valid!");
                        System.out.print("Enter the number of classes taken per term : ");
                        studentArray[i][j] = input.nextInt();
                    }
                }
            }
        }

        divide(studentArray);
    }

    public static void divide(int[][] termsLeft) {
        for (int k = 0; k < termsLeft.length; k++) {
            double result = termsLeft[k][0]/termsLeft[k][1];
            long round_result = Math.round(result);
            System.out.println("Student " +(k+1)+ " has " + round_result + " terms left to graduate.");
        }
    }

}