整数到双变量
Integer to Double variable
我需要创建一个程序,提示用户输入薪水并获得最高和最低薪水。我已经为此工作了 4 天了。我终于使用一些教程创建了我的程序在互联网上,但我只有一个问题......我无法将 INT 转换为 Double @@ 这让我很头疼......我哪里出错了?有人能帮我吗?我需要通过 java class ;;
代码如下:
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;
}
}
任何人都可以帮助我并教我如何将其转换为双倍?提前谢谢你..
您可以像这样将一个 int 值赋给 double:
int n = 1;
double j = n;
System.out.println(j);
Output:
1.0
注意:您可以使用 nextDouble
api 而不是 nextInt
来要求薪水为双精度类型
呃……要将 int
转换为 double
,您只需分配它即可。该赋值将导致 a "primitive widening conversion" 发生;参见 JLS 5.1.2。
int myInt = 42;
double myDouble = myInt; // converts to a double.
(原始扩展转换不需要类型转换......虽然添加一个没有害处。)
将 int 数组转换为 double 数组....
int[] myInts = ....
double[] myDoubles = new double[myInts.length];
for (int i = 0; i < myInts.length; i++) {
myDoubles[i] = myInts[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;
}
}
我需要创建一个程序,提示用户输入薪水并获得最高和最低薪水。我已经为此工作了 4 天了。我终于使用一些教程创建了我的程序在互联网上,但我只有一个问题......我无法将 INT 转换为 Double @@ 这让我很头疼......我哪里出错了?有人能帮我吗?我需要通过 java class ;;
代码如下:
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;
}
}
任何人都可以帮助我并教我如何将其转换为双倍?提前谢谢你..
您可以像这样将一个 int 值赋给 double:
int n = 1;
double j = n;
System.out.println(j);
Output:
1.0
注意:您可以使用 nextDouble
api 而不是 nextInt
呃……要将 int
转换为 double
,您只需分配它即可。该赋值将导致 a "primitive widening conversion" 发生;参见 JLS 5.1.2。
int myInt = 42;
double myDouble = myInt; // converts to a double.
(原始扩展转换不需要类型转换......虽然添加一个没有害处。)
将 int 数组转换为 double 数组....
int[] myInts = ....
double[] myDoubles = new double[myInts.length];
for (int i = 0; i < myInts.length; i++) {
myDoubles[i] = myInts[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;
}
}