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

go评论35阅读模式

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

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

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("请输入行数：");
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.

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;);
}
}
}

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

go 40

go 45

go 39

go 34