zoukankan      html  css  js  c++  java
  • 数组课后作业

    课后作业:

    随机生成10个数,填充一个数组,然后用消息框显示数组,接着计算数组元素的和,将结果也显示在消息框中。

    设计思路:首先使用random随机产生10个数,存储在数组中,然后使用for循环把数字存储在一个String对象中,再使用for循环求出数组中所有数字的和,最后用对话框输出内容。

    程序流程图:

     源程序代码:

    import javax.swing.*;

    public class Test1{

    public static void main( String args[] ){

    String output = "";

    int num=0;

    int n[] = new int[10];

    for(int i=0;i<n.length;i++){

    n[i]=(int)(Math.random()*100+1);

    }

    output+="随机生成的十个数为:"+" ";

    for(int i=0;i<n.length;i++)

    output+=n[i]+" ";

    output+=" ";

    for(int i=0;i<n.length;i++)

    num+=n[i];

    output+="十个数相加等于:"+" "+num;

    JOptionPane.showMessageDialog(null, output,"输出",JOptionPane.INFORMATION_MESSAGE);

    }

    }

    程序结果截图:

  • 相关阅读:
    1. 加载文件的方法
    9. 位运算符
    8. 条件(条目,三元)运算符
    3. PHP比较运算符
    hdu3336 Count the string
    Codeforces Round #228 (Div. 2)
    hdu4288 Coder(线段树单点更新)
    hdu2852 KiKi's K-Number
    poj1195
    poj2299
  • 原文地址:https://www.cnblogs.com/sunmei20142925/p/4924270.html
Copyright © 2011-2022 走看看