数组

要求将设计思路、程序流程图、源程序代码、结果截图、编程总结等发表到博客园。

设计思想:定义一个整型的数组,长度为十, Random random=new Random();

                     for(int i=0;i<10;i++){

           array[i]=random.nextInt(10);

           }

在用以上函数将数组中的十个数,随机数出,在定义一个整型的sum,用来将数组中的十个数相加求和输出, 用JOptionPane.showMessageDialog( null, output,

                  "Demonstrating String Method concat",

                  JOptionPane.INFORMATION_MESSAGE );将数组和数组合用消息框输出。

流程图:

 

 

 

 

package demo;

import java.util.Random;

import javax.swing.JOptionPane;

public class ArrayNum {

 

public static void main(String[] args) {

// TODO 自动生成的方法存根

   int sum=0; 

           Random random=new Random();

           int[]array=new int[10];

           for(int i=0;i<10;i++){

           array[i]=random.nextInt(10);

           }

           String output = null;

           for(int i=0;i<10;i++){

            output+="\n数组值为:"+array[i];

           }

           JOptionPane.showMessageDialog( null, output,

                  "Demonstrating String Method concat",

                  JOptionPane.INFORMATION_MESSAGE );

           for(int i=0;i<10;i++){

            sum+=array[i];

           output="数组和为:"+sum;

              }

           JOptionPane.showMessageDialog( null, output,

                "Demonstrating String Method concat",

                JOptionPane.INFORMATION_MESSAGE );

           }

}

 

上述代码可以顺利通过编译,并且输出一个“很奇怪”的结果:

 

为什么会这样?学到后面就明白了,此处先不求甚解

 

java.lang包在使用的时候无需显示导入,编译时由编译器自动导入。Object类是类层次结构的根,Java中所有的类从根本上都继承自这个类。Object类是Java中唯一没有父类的类。其它所有的类,包括标准容器类,比如数组,都继承了Object类中的方法。

 

posted @ 2015-11-01 14:21  秋尘若水  阅读(116)  评论(0编辑  收藏  举报