zoukankan      html  css  js  c++  java
  • 随机数产生数组并求和





    import
    javax.swing.*; import java.math.*; public class Project { public static void main(String args[]) { String output="数组中的十个数为:"; int a[]; a=new int[10]; for(int i=0;i<10;i++) a[i]=(int)(Math.random()*100); for(int i=0;i<10;i++) output=output+" "+String.valueOf(a[i]); JOptionPane.showMessageDialog(null, output,"result",JOptionPane.PLAIN_MESSAGE); int sum=0; for(int i=0;i<10;i++) sum=a[i]+sum; output=output+' '+"数组中十个整数的和为:"+String.valueOf(sum); JOptionPane.showMessageDialog(null, output,"result",JOptionPane.PLAIN_MESSAGE); } }

    程序设计思想:  

      1.import 引用math和swing两个包,用来产生随机数和调用对话框

      2.首先定义一个String类型的output,使用对话框输出。  

      3.定义并申请一个整形包含十个数的数组。  

      4.利用for循环调用随机数函数对数组赋值  

      5.使用for循环将数组中的每个数转换成String型,并和output连接  

      6.对话框输出数组元素。  

      7.定义sum=0,利用for循环相加数组结果。  

      8.链接到output上,对话框输出。

      不知道的地方:   

        1.JOptionPane.showMessageDialog   

        2.Math.random()产生【0.0,1.0】之间的双精度类型的浮点数,需要使用强制类型转换成整形。

     

  • 相关阅读:
    【实战】如何实现滚轮时间的显示
    NSDate的常用用法
    UIDatePicker的简单用法
    NSDateFormatter相关整理
    UIPickerView
    回家任务
    addTarget:self 的意思是说,这个方法在本类中
    2020/2/25
    树上启发式合并
    题解
  • 原文地址:https://www.cnblogs.com/dotacai/p/4915613.html
Copyright © 2011-2022 走看看