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

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

    设计思路

     (1)创建并初始化一个长度为10的数组

     (2)使用Random类的相关方法给数组元素赋值,此处使用的是Random.nextInt()

     (3)计算出数组元素累加求和的结果

     (4)使用对话框输出

    程序流程图、

     

    源程序代码、

     1 import java.util.Random;
     2 import javax.swing.*;
     3 public class Test {
     4     public static void main(String args[])
     5     {
     6         Random random = new Random();
     7         String output = " ";
     8         int a[] = new int[10];
     9         for(int i = 0;i<a.length;i++)
    10         {
    11             a[i]=random.nextInt(1000);
    12             //a[i]=(int)(Math.random()*1000);
    13         }
    14         output += "Subscript	Value
    ";
    15         for(int i = 0;i<a.length;i++)
    16         {
    17             output += i+1 + "	" + a[i] + "
    ";
    18         }
    19         
    20         int sum = 0;
    21         for(int i = 0;i<a.length;i++)
    22         {
    23             sum += a[i];    
    24         }
    25         output += "
    sum" + "	" + sum;
    26         JTextArea outputArea = new JTextArea(12,11);
    27         outputArea.setText( output );
    28         JOptionPane.showMessageDialog(null, outputArea, "result", JOptionPane.INFORMATION_MESSAGE);
    29     }
    30 
    31 }

    结果截图、

    编程总结

     JTextArea类可以很方便的编辑输出框的格式。

  • 相关阅读:
    使用Power Shell 拉取项目源代码
    C# 读取excel数据到datatable
    C# 导出datatable数据到excel
    redis过一段时间连接不上
    windows10 docker volume
    通过端口查询到应用
    centos清理磁盘
    maven镜像加速
    IDEA常用插件
    java开发常用软件
  • 原文地址:https://www.cnblogs.com/weipinggong/p/4921903.html
Copyright © 2011-2022 走看看