半日闲ovo 2022-11-07 16:26 采纳率: 100%
浏览 52
已结题

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人已打赏
查看更多回答(2条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?