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

    1.上述代码可以顺利通过编译,并且输出一个“很奇怪”的结果:Ljava.lang.Object;@ba8a1dc,这是为什么?

    由于java的Object数组不能转换成为String[]数组,这就说明你要转换的数组它本身是Object类型的数组,但是你却非要把它转换为String类的数组,这当然是错误的。在遇到类型转换出错的时候,首先要观察被转换的对象的原来类型是什么,这个就需要首先把它转换成为自己本来的类型的对象,然后根据这个对象再去操作里面的元素,再一次的转换类型,而且有的时候被分析的对象可能有多层的包装,在转换的过程中需要多层的解开它本来的类型,直到获取到对象的最终类型,然后把不能再分解的类型转换成为自己目标类型的对象。

    2.随机生成10个数,填充一个数组,然后用消息框显示数组内容,接着计算数组元素的和,将结果也显示在消息框中。 要求将设计思路、程序流程图、源程序代码、结果截图、编程总结等发表到博客园。

    设计思想

    申请一个长度为10的数组,随机生成10个数,用for循环控制,填充一个数组,计算元素的和,消息框显示数组内容

    程序流程图

    源程序代码

    import javax.swing.JOptionPane;  // import class JOptionPane

    import java.util.Random;

    public class RandomNumber {

     public static void main(String[] args) {

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

    int a[];

    int sum=0;

    String output=" ";

    a=new int[10];

    Random rand=new Random();

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

    {

    a[i]=rand.nextInt(100);

    sum+=a[i];

    output+=String.valueOf(a[i])+" ";

    }

    JOptionPane.showMessageDialog(null,output,"random number is"+" " , JOptionPane.PLAIN_MESSAGE);

    JOptionPane.showMessageDialog(null,sum,"The sum is"+“ ”, JOptionPane.PLAIN_MESSAGE);

    System.exit( 0 );   // terminate the program

       }

    }

    结果截图

    编程总结

    善于总结知识,多思考。

  • 相关阅读:
    HearthBuddy投降插件2019-11-01的使用
    正则表达式在线分析 regex online analyzer
    Tips to write better Conditionals in JavaScript
    The fileSyncDll.ps1 is not digitally signed. You cannot run this script on the current system.
    Cannot capture jmeter traffic in fiddler
    JMETER + POST + anti-forgery token
    input type color
    HearthBuddy修改系统时间
    What are all the possible values for HTTP “Content-Type” header?
    UDK性能优化
  • 原文地址:https://www.cnblogs.com/qianxia/p/4921322.html
Copyright © 2011-2022 走看看