我遇到一些问题,需要打印我的最小值和最大值。也是新的 因为我编程我不明白为什么我的所有值都是小数。我将发布代码和结果。
import java.util.Random;
import java.util.Scanner;
import java.util.*;
public class RandomArray1
extends ArrayList<Double>
{
private static final long serialVersionUID = 1L;
public RandomArray1()
{
super();
}
public static RandomArray1 getInstance(int size)
{
Random randomNumberGenerator = new Random();
RandomArray1 randomArray = new RandomArray1();
for (int i = 0; i < size; i++)
{
randomArray.add(randomNumberGenerator.nextDouble());
}
return randomArray;
}
public Double getAverage()
{
if (this.size() == 0)
{
return 0d;
}
Double sum = 0d;
for (Double element : this)
{
sum = sum + element;
}
return sum / this.size();
}
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter how many numbers you would like to Generate: ");
RandomArray1 randomArray = RandomArray1.getInstance(scan.nextInt());
System.out.println(randomArray);
System.out.println("Average" +randomArray.getAverage());
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE;
while (true) {
if ( !scan.hasNextDouble())
break;
Double num = scan.nextDouble();
min = Math.min(min, num);
max = Math.max(max, num);
System.out.println("Max is: " + max);
System.out.println("Min is: " + min);
}
}
}
以下是我的结果......
输入您想要生成的数量:5 [0.8630040934474159,0.12949667753808425,0.5751777190226718,0.18492672539115063,0.7508377917335503] 平均值:0.5006886014265746
总是小数点,它甚至不打印&#34; Max是:&#34;或者&#34;敏是:&#34;这让我觉得这部分有问题。但我不知道它是什么......任何想法。只是想法......
while (true) {
if ( !scan.hasNextDouble())
break;
Double num = scan.nextDouble();
min = Math.min(min, num);
max = Math.max(max, num);
System.out.println("Max is: " + max);
System.out.println("Min is: " + min);
答案 0 :(得分:1)
我认为你用nextDouble()
填充数组,documented为你提供0到1之间的双精度浮点数。
答案 1 :(得分:1)
一切都好,将Arraylist改为整数和其他一切。这解决了我讨论的问题。这是代码现在的样子......工作完美!
import java.util.*;
public class RandomArray1
extends ArrayList<Integer>
{
private static final long serialVersionUID = 1L;
public RandomArray1()
{
super();
}
public static RandomArray1 getInstance(int size)
{
Random randomNumberGenerator = new Random();
RandomArray1 randomArray = new RandomArray1();
for (int i = 0; i < size; i++)
{
randomArray.add((int) randomNumberGenerator.nextInt());
}
return randomArray;
}
public Double getAverage()
{
if (this.size() == 0)
{
return 0d;
}
Double sum = 0d;
for (Integer element : this)
{
sum = sum + element;
}
return sum / this.size();
}
public static void main(String[] args)
{
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
System.out.print("Enter how many numbers you would like to Generate: ");
RandomArray1 randomArray = RandomArray1.getInstance(scan.nextInt());
System.out.println(randomArray);
System.out.println("Average : " +randomArray.getAverage());
int max = Integer.MAX_VALUE;
int min = Integer.MIN_VALUE;
for (int num : randomArray)
{
min = Math.min(min, num);
max = Math.max(max, num);
}
System.out.println("Max is: " + max);
System.out.println("Min is: " + min);
}
}
非常感谢你!
答案 2 :(得分:0)
您的程序运行良好:
Enter how many numbers you would like to Generate: 3
[0.26063976207509887, 0.48867331377683443, 0.3864751544223266]
Average0.37859607675808665
4
Max is: 4.0
Min is: 4.0
5
Max is: 5.0
Min is: 4.0
3
Max is: 5.0
Min is: 3.0
end
但由于它希望得到一个输入,但并不是这样说的,它似乎可能已经死了#34;只需在等待下一个输入之前包含提示:
System.out.print("Please enter next number: ");
if ( !scan.hasNextDouble())
break;
如果您想要计算现有数组的最小值/最大值而不是新用户输入,请使用for
循环迭代现有数组:
for (double num : randomArray) {
min = Math.min(min, num);
max = Math.max(max, num);
System.out.println("Max is: " + max);
System.out.println("Min is: " + min);
}