Java 整数到双变量
我需要创建一个程序,提示用户输入工资并获得最高和最低工资。。我已经做了4天了。。最后,我使用互联网上的一些教程创建了我的程序,但我只有一个问题。。。我就是不能把INT转换成Double@这让我头疼。。我哪里出错了?有人能帮我吗?我需要通过java类 代码如下:Java 整数到双变量,java,arrays,variables,integer,double,Java,Arrays,Variables,Integer,Double,我需要创建一个程序,提示用户输入工资并获得最高和最低工资。。我已经做了4天了。。最后,我使用互联网上的一些教程创建了我的程序,但我只有一个问题。。。我就是不能把INT转换成Double@这让我头疼。。我哪里出错了?有人能帮我吗?我需要通过java类 代码如下: import java.util.*; public class HighestLowestSalary { public static void main(String[] args) { Scanne
import java.util.*;
public class HighestLowestSalary
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("How many salary do you want to enter?: ");
int sal = input.nextInt();
//starting here should be double already..
System.out.println("Enter the "+sal +" salaries");
int[]salary = new int[sal];
for (int i=0; i<sal; i++)
{
salary[i]=input.nextInt();
}
System.out.println("The Highest Salary is: " +high(salary));
System.out.println("The Lowest Salary is: " +low(salary));
}
public static int high(int[] numbers)
{
int highsal = numbers[0];
for (int i=1; i<numbers.length;i++){
if (numbers[i] > highsal){
highsal = numbers[i];
}
}
return highsal;
}
public static int low(int[] numbers){
int lowsal = numbers[0];
for (int i=1;i<numbers.length;i++){
if (numbers[i] < lowsal){
lowsal = numbers[i];
}
}
return lowsal;
}
}
import java.util.*;
公务舱最高最低工资
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
System.out.println(“您想输入多少工资?:”;
int sal=input.nextInt();
//从这里开始应该已经是双倍了。。
System.out.println(“输入“+sal+”工资”);
int[]工资=新int[sal];
对于(int i=0;i您可以将int值指定给double,如:
int n = 1;
double j = n;
System.out.println(j);
Output:
1.0
注意:您可以通过使用nextDouble
api而不是nextIntErm…来要求工资为双精度类型。要将int
转换为double
,您只需分配它。分配将导致发生“原始加宽转换”;请参阅
(原语加宽转换不需要类型转换…尽管添加一个类型转换并没有坏处。)
要将整型数组转换为双精度数组
int[] myInts = ....
double[] myDoubles = new double[myInts.length];
for (int i = 0; i < myInts.length; i++) {
myDoubles[i] = myInts[i];
}
int[]myInts=。。。。
double[]myDoubles=新的双精度[myInts.length];
for(int i=0;i
多亏了你的帮助,我才解决了这个问题!下面是我所做的……就像大家说的那样,将int转换为Double
//this is where I changed all the int to double
System.out.println("Enter the "+sal +" salaries");
double[]salary = new double[sal];
for (int i = 0; i<sal; i++){
salary[i] = input.nextDouble();
}
System.out.println("The Highest Salary is: " +high(salary));
System.out.println("The Lowest Salary is: " +low(salary));
}
public static double high(double[] numbers)
{
double highsal = numbers[0];
for (int i=1; i<numbers.length;i++){
if (numbers[i] > highsal){
highsal = numbers[i];
}
}
return highsal;
}
public static double low(double[] numbers){
double lowsal = numbers[0];
for (int i=1;i<numbers.length;i++){
if (numbers[i] < lowsal){
lowsal = numbers[i];
}
}
return lowsal;
}
}
//在这里,我将所有int都改为double
System.out.println(“输入“+sal+”工资”);
双倍[]工资=新的双倍[sal];
for(int i=0;当你表示“Double”时,我不会说“Double”,当你表示“int”时,我不会说“Integer”。大写形式是类名(原始包装类)…而且你的代码中没有任何地方使用它们。你为什么要将某人的工资存储为double
?如果是整数美元,那么int
就足够了。如果不是整数美元,那么BigDecimal
比double
更合适。它不是美元。如果我说双倍而不是双倍。。
//this is where I changed all the int to double
System.out.println("Enter the "+sal +" salaries");
double[]salary = new double[sal];
for (int i = 0; i<sal; i++){
salary[i] = input.nextDouble();
}
System.out.println("The Highest Salary is: " +high(salary));
System.out.println("The Lowest Salary is: " +low(salary));
}
public static double high(double[] numbers)
{
double highsal = numbers[0];
for (int i=1; i<numbers.length;i++){
if (numbers[i] > highsal){
highsal = numbers[i];
}
}
return highsal;
}
public static double low(double[] numbers){
double lowsal = numbers[0];
for (int i=1;i<numbers.length;i++){
if (numbers[i] < lowsal){
lowsal = numbers[i];
}
}
return lowsal;
}
}