# java中对随机生成的浮点数数组进行操作

###### 用代码块功能插入代码，请勿粘贴截图
``````package lab2;

import java.util.Random;
import java.util.Scanner;
public class Part1 {
public static double readSize(String message) {
//output the greeting to make clear to the user what's happening
System.out.print(message);
Scanner scanner = new Scanner(System.in);
double size = scanner.nextDouble();
return size;
}
public static void printArray(double[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
public static void fillArrayByRandom(double[] array) {
Random random = new Random();
for (int i = 0; i < array.length; i++) {
array[i] = random.nextDouble(20);
}
}
public static void fillArrayByUser(double[] array) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < array.length; i++) {
System.out.print("Enter the " + (i + 1) + " element of " +
array.length);
array[i] = scanner.nextDouble();
}
}
public static void main(String[] args) {
//read the size of the vector (linear array)
double size = readSize("Count of elements: ");
double[] array = new double[(int) size];
fillArrayByRandom(array);
System.out.println("Array:");
printArray(array);
System.out.println();

//we need to count zero elements.
//计数数组中零元素的个数
double amount = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] == 0) {
amount = amount + array[i]*0 + 1;
}
}
System.out.println("The number of zero elements is = " +  amount);

//we need to find the index of min value in the array
//to do that we will save value for the "current minimal value"
//(current means that for the current i value)
double min = array[0];
//and minIndex - because we must find the sum before the min element
//it is the way to store it's position
double minIndex = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] < min) {
min = array[i];
minIndex = i;
}
}

//sum of elements after the min value
//求最小元素之后的元素和
double sum = 0;
for (int i = 0; i > minIndex; i++) {
sum = array[i];
}
//print the result
System.out.println("The sum after the minimal elements is " + sum);

//将数组按照递增顺序重新排列
for (int i = 0; i < array.length / 2; i++) {
double tmp = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = tmp;
}
System.out.println("Reversed array:");
for (int i = 0; i < array.length; i++)
System.out.print(array[i] + " ");
}
}

``````
###### 运行结果及报错内容

Count of elements: 5
Array:
5.416085002789357 9.13728160490736 16.674225755360524 6.5628141875893125 18.38307664064794
The number of zero elements is = 0.0
The sum after the minimal elements is 0.0
Reversed array:
18.38307664064794 6.5628141875893125 16.674225755360524 9.13728160490736 5.416085002789357

###### 我想要达到的结果

1.计数随机生成的浮点数数组中零元素的个数
2.对最小元素之后的元素进行求和
3.将数组按照递增顺序重新排列

• 写回答

#### 3条回答默认 最新

• CSDN专家-sinJack 2022-11-07 16:32
关注
``````import java.util.Random;
import java.util.Scanner;
public class Part1 {
public static double readSize(String message) {
System.out.print(message);
Scanner scanner = new Scanner(System.in);
double size = scanner.nextDouble();
return size;
}
public static void printArray(double[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
public static void fillArrayByRandom(double[] array) {
Random random = new Random();
for (int i = 0; i < array.length; i++) {
array[i] = random.nextDouble()*20;
}
}
public static void fillArrayByUser(double[] array) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < array.length; i++) {
System.out.print("Enter the " + (i + 1) + " element of " +
array.length);
array[i] = scanner.nextDouble();
}
}
public static void main(String[] args) {
double size = readSize("Count of elements: ");
double[] array = new double[(int) size];
fillArrayByRandom(array);
System.out.println("Array:");
printArray(array);
System.out.println();

double amount = 0;
for (int i = 0; i < array.length; i++) {
if ((int) array[i] == 0) {
amount = amount+ 1;
}
}
System.out.println("The number of zero elements is = " +  amount);
double min = array[0];
int minIndex = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] < min) {
min = array[i];
minIndex = i;
}
}

double sum = 0;
for (int i = minIndex+1; i < array.length; i++) {
sum += array[i];
}

System.out.println("The sum after the minimal elements is " + sum);

//将数组按照递增顺序重新排列
for(int i=1;i<array.length;i++)
{
for(int j=0;j<array.length-i;j++)
{
if(array[j]>array[j+1])
{
double temp=array[j+1];
array[j+1]=array[j];
array[j]=temp;
}
}
}
System.out.println("Reversed array:");
for (int i = 0; i < array.length; i++)
System.out.print(array[i] + " ");
}
}
``````
本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论 编辑记录
1人已打赏

• 系统已结题 11月15日
• 已采纳回答 11月7日
• 创建了问题 11月7日

#### 悬赏问题

• ¥40 如果update 一个列名为参数的value
• ¥15 基于51单片机的水位检测系统设计中LCD1602一直不显示
• ¥15 OCS2安装出现问题，请大家给点意见
• ¥15 ros小车启动launch文件报错
• ¥15 vs2015到期想登陆但是登陆不上
• ¥15 IPQ5018制作烧录固件，boot运行失败(操作系统-linux)（相关搜索：操作系统）（相关搜索：操作系统）
• ¥20 icefall在librispeech基础上加入个人数据集
• ¥30 keepalive高可用故障运维配置询问
• ¥15 求帮助！国家电网内网u盘突然识别不出来了。
• ¥15 matlab语音变速变调同时实现