zoukankan      html  css  js  c++  java
  • 数组任务

    随机生成10个数,填充一个数组,然后用消息框显示数组内容,接着计算数组元素的和,将结果也显示在消息框中。
    设计思路:
    用random函数随机生成10个数存入数组中,再将10个数转化为字符串依次连接起来存在output中用消息框输出,再用循环求和输出
    源代码:

    package demo;
    import javax.swing.JOptionPane;
    public class RandomAdd {
    public static void main(String[] args){
    int random[]=new int[10];
    int sum=0;
    for(int i =0;i < random.length;i++)
    {
    int val=(int)(Math.random()*100+1);
    random[i] = val;
    }
    String output = new String();
    for(int i =0;i < random.length;i++)
    {
    output+=random[i]+'0';
    output+=" ";
    }
    for(int i = 0;i < random.length;i++)
    {
    sum += random[i];
    }
    output += "\n所有数的和为:" + sum;
    JOptionPane.showMessageDialog(
    null, output, "Results",
    JOptionPane.PLAIN_MESSAGE );
    }
    }

    结果:

  • 相关阅读:
    论文笔记4
    论文笔记3
    论文笔记2
    论文笔记1
    论文笔记
    AFG与AWG的比较
    Linux下“有线线缆被拔出”问题的解决
    python生成excel格式座位表
    PythonTip--一马当先--bfs
    python pygame--倒计时
  • 原文地址:https://www.cnblogs.com/chengez/p/4924777.html
Copyright © 2011-2022 走看看