如何将整数数组转换为双精度数组Java?

huangapple go评论68阅读模式
英文:

How to cast an int array to a double array java?

问题

I've translated the code portion for you:

目前正在处理一个项目该项目请求学生剩余课程数量每学期上课数量并返回剩余毕业学期数我在强制将整数数组转换为双精度数组以计算所需学期数量方面遇到了一些困难此外结果还需要向上舍入我对此非常陌生所以非常感谢任何建议并对如何优化我的代码表示感谢提前感谢

import java.util.Scanner;

public class main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("输入行数:");
        int rows = input.nextInt();
        System.out.print("输入列数:");
        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("请输入学生 " + (i + 1) + " 剩余课程数:");
                    studentArray[i][j] = input.nextInt();
                } else if (j > 0) {
                    System.out.print("输入每学期上课数量:");
                    studentArray[i][j] = input.nextInt();

                    while (studentArray[i][j] >= 6) {
                        System.out.println("学生 " + (i + 1) + " 每学期上课数量无效!");
                        System.out.print("输入每学期上课数量:");
                        studentArray[i][j] = input.nextInt();
                    }
                }
            }
        }

        divide(studentArray);
    }

    public static void divide(int termsLeft[][]) {
        for (int k = 0; k < termsLeft.length; k++) {
            double result = (double) termsLeft[k][0] / termsLeft[k][1];
            if (k >= 0) {
                System.out.println("学生 " + (k + 1) + " 剩余 " + result + " 学期毕业。");
            }
        }
    }
}

Please note that I made a small modification to cast the division result to a double to handle decimal values and rounded up as specified in your original request.

英文:

Currently working on a project that requests the number of classes a student has left, the amount of classes taken per term, and returns the number of terms left to graduate. I'm having some trouble figuring out how to cast an integer array to a double array to figure out the amount of terms needed. Also the result needs to round up as well. I'm very new to this so any suggestions are much appreciated and critiques on how to clean up my code, thanks in advance.

import java.util.Scanner;
public class main {
public static void main (String[] args) {
Scanner input = new Scanner (System.in);
System.out.print(&quot;Enter the number of rows: &quot;);
int rows = input.nextInt();
System.out.print(&quot;Enter the number of columns: &quot;);
int columns = input.nextInt();
int [][] studentArray = new int [rows][columns];
for (int i = 0; i &lt;= rows-1; i++) {
for (int j = 0; j &lt;= columns-1; j++) {
if (j==0) {
System.out.print(&quot;Please enter the number of classes left for student &quot; +(i+1)+ &quot; : &quot;);
studentArray[i][j] = input.nextInt();
}
else if (j&gt;0) {
System.out.print(&quot;Enter the number of classes taken per term : &quot;);
studentArray[i][j] = input.nextInt();
while (studentArray[i][j] &gt;= 6) {
System.out.println(&quot;The number of classes per term for student &quot; +(i+1)+ &quot; is not valid!&quot;);
System.out.print(&quot;Enter the number of classes taken per term : &quot;);
studentArray[i][j] = input.nextInt();
}
}
}
}
divide(studentArray);
}
public static void divide(int termsLeft[][]) {
for (int k = 0; k &lt; termsLeft.length; k++) {
double result = termsLeft[k][0] / termsLeft[k][1];
if (k&gt;=0) {
System.out.println(&quot;Student &quot; +(k+1)+ &quot; has &quot; + result + &quot; terms left to graduate.&quot;);
}
}
}

}

答案1

得分: 1

以下是翻译好的部分:

首先,您的代码中存在一些问题,使其非常低效。

for (int i = 0; i <= rows-1; i++) {
    for (int j = 0; j <= columns-1; j++) {
    }

在您的内部和外部循环中,您不需要使用<=符号,并且不需要从右值减去1。您可以使用i < rowsj < columns

if (k >= 0) {
    System.out.println(...)
}

不需要使用if语句,因为它始终为真。

现在转向您的问题。

您可以使用Math.round方法将double值四舍五入为long(可以存储比int更大的值)。

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("请输入行数:");
        int rows = input.nextInt();
        System.out.print("请输入列数:");
        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("请输入学生 " + (i + 1) + " 剩余课程数:");
                    studentArray[i][j] = input.nextInt();
                } else if (j > 0) {
                    System.out.print("请输入每学期上课数:");
                    studentArray[i][j] = input.nextInt();

                    while (studentArray[i][j] >= 6) {
                        System.out.println("学生 " + (i + 1) + " 每学期上课数无效!");
                        System.out.print("请输入每学期上课数:");
                        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("学生 " + (k + 1) + " 还有 " + round_result + " 个学期可以毕业。");
        }
    }

}

请注意,由于您要求只返回翻译好的部分,我已经去除了原始代码中的注释。如果您需要完整代码,请让我知道。

英文:

First of all their are some problems in your code which make it very ineffecient.

1)

for (int i = 0; i &lt;= rows-1; i++) {
for (int j = 0; j &lt;= columns-1; j++) {
}

In your inner and outer loop you don't have to use <= sign and subtract 1 from right value. you can use i &lt; rows & j &lt; columns.

if (k&gt;=0) {
System.out.println(...)
}

Their is no need to use the if statement as it is always true.

Now coming to your question.

Your can use the Math.round method to round double values into long (can store larger values than int).

    double result = termsLeft[k][0]/termsLeft[k][1];
long round_result = Math.round(result);

So your final code is below :

import java.util.Scanner;
public class Main {
public static void main (String[] args) {
Scanner input = new Scanner (System.in);
System.out.print(&quot;Enter the number of rows: &quot;);
int rows = input.nextInt();
System.out.print(&quot;Enter the number of columns: &quot;);
int columns = input.nextInt();
int [][] studentArray = new int [rows][columns];
for (int i = 0; i &lt;= rows-1; i++) {
for (int j = 0; j &lt;= columns-1; j++) {
if (j==0) {
System.out.print(&quot;Please enter the number of classes left for student &quot; +(i+1)+ &quot; : &quot;);
studentArray[i][j] = input.nextInt();
}
else if (j&gt;0) {
System.out.print(&quot;Enter the number of classes taken per term : &quot;);
studentArray[i][j] = input.nextInt();
while (studentArray[i][j] &gt;= 6) {
System.out.println(&quot;The number of classes per term for student &quot; +(i+1)+ &quot; is not valid!&quot;);
System.out.print(&quot;Enter the number of classes taken per term : &quot;);
studentArray[i][j] = input.nextInt();
}
}
}
}
divide(studentArray);
}
public static void divide(int[][] termsLeft) {
for (int k = 0; k &lt; termsLeft.length; k++) {
double result = termsLeft[k][0]/termsLeft[k][1];
long round_result = Math.round(result);
System.out.println(&quot;Student &quot; +(k+1)+ &quot; has &quot; + round_result + &quot; terms left to graduate.&quot;);
}
}
}

huangapple
  • 本文由 发表于 2020年8月3日 10:48:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63223089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定