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

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

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

    2、请编写一个程序将一个整数转换为汉字读法字符串。比如“1123”转换为“一千一百二十三”。

    package demo;

     

    import java.io.*;

    import javax.swing.*;

    public class NumberChange

    {

    public static void main(String args[]) throws IOException

    {

    int num;

    int w1,w2,w3,w4,w5;

    String [] zi ={"","","","","","","","",""};

    System.out.println("请输入金额。");

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    String a=reader.readLine();

    num = Integer.parseInt(a);

    w5=num/10000;

    w4=num%10000/1000;

            w3=num%1000/100;

        w2=num%100/10;

        w1=num%10;

        if(w5 != 0)

        {

         System.out.print(zi[w5 - 1] + "");

        }

        else

        {

         System.out.print("");

        }

        if(w4 != 0)

        {

         System.out.print(zi[w4 - 1] + "");

        }

        else

        {

         if(w4 == 0&&w3 == 0&&w2 == 0&&w1 == 0)

         {

         System.out.print("");

         }

         else if(w3 != 0||w2 != 0||w1 != 0)

         {

         System.out.print("");

         }

         else

         {

         System.out.print("");

         }

        

        }

        if(w3 != 0)

        {

         System.out.print(zi[w3 - 1] + "");

        }

        else

        {

         if(w4 == 0)

         {

         System.out.print("");

         }

         else

         {

         System.out.print("");

         }

        }

        if(w2 != 0)

        {

         System.out.print(zi[w2 - 1] + "");

        }

        else

        {

         if(w4 == 0 && w3 == 0)

         {

         System.out.print("");

         }

         else

         {

         System.out.print("");

         }

        }

        if(w1 != 0)

        {

         System.out.print(zi[w1 - 1] + "");

        }

        else

        {

         if(w4 == 0 || w3 == 0 || w2 == 0)

         {

         System.out.print("");

         }

         else

         {

         System.out.print("");

         }

        }

    }

    }

     

     

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

    package demo;

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

    import java.util.Random;

    public class numbersuiji {

     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,"Result "+" " , JOptionPane.PLAIN_MESSAGE);

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

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

       }

    }

     

    4、大数

    package demo;

    import java.util.Scanner;

    public class Bignumber{

            public static int[]add(int []a,int []b){

                int wei=0;

                int[]c=new int[a.length];

                for(int i=a.length-1;i>=0;i--)

                {

                    c[i]=a[i]+b[i]+wei;

                    if(c[i]<10)

                        wei=0;

                    else

                    {

                        c[i]=c[i]-10;

                        wei=1;

                    }

                }

                return c;

        }

            public static int []sub(int []a,int[]b,int w)

            {

                int wei=0;

                int[]c=new int[a.length];

                for(int i=a.length-1;i>=0;i--)

                {

                    if(w<=0)

                    {

                        c[i]=b[i]-a[i]-wei;

                        if(c[i]>=0)

                        {   wei=0;}

                        else

                        {

                            c[i]=c[i]+10;

                            wei=1;

                        }

                    }

                        else

                        {

                            c[i]=a[i]-b[i]-wei;

                            if(c[i]>=0)

                            {wei=0;}

                            else

                            {

                                c[i]=c[i]+10;

                                wei=1;

                            }

                     }

                 }

                    return c;

                }

                public static void main(String[]args){

                    int a[]=new int[100];

                    int b[]=new int[100];

                    int m=0;

                    int n=0;

                    int s=0;

                    int t=0;int u=0;

                    Scanner in=new Scanner(System.in);

                    System.out.println("请输入第一个数:");

                    String aa=in.next();

                    System.out.println("请输入第二个数:");

                    String bb=in.next();

                    m=a.length-aa.length();

                    n=b.length-bb.length();

                    if(aa.length()>bb.length())

                    {

                        u=1;

                    }

                    else if (aa.length()<bb.length())

                    {

                        u=-1;

                    }

                    else

                        {u = aa.compareTo(bb);}

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

                    {

                        a[m++] = aa.charAt(i) - 48;

                    }

                    for (int j = 0; j < bb.length(); j++)

                    {

                        b[n++] = bb.charAt(j) - 48;

                    }

                        int[] c = Bignumber.add(a, b);

                        for (int k = 0; k < c.length; k++)

                        {

                            if (c[k] > 0)

                            {

                            s = k;

                            break;

                            }

                        }

                        System.out.print("相加的结果为:");

                        for (int i = s; i < c.length; i++) {

                        System.out.print(c[i]);

                        }

                        System.out.println();

                        int[] d = Bignumber.sub(a, b, u);

                        for (int k = 0; k < d.length; k++)

                        {

                            if (d[k] > 0)

                            {

                            t = k;

                            break;

                            }

                        }

                        System.out.print("相减的结果为:");

                        if (u < 0)

                        System.out.print("-");

                        for (int i = t; i < d.length; i++)

                        {

                            System.out.print(d[i]);

                        }

                        System.out.println();

                        System.out.println();

                        System.out.println();

                        System.out.println();

                        }

    }

     

  • 相关阅读:
    极验验证(滑动验证)的使用
    from close /destory
    tmeminifile and tinifile
    Delphi的OverRide、OverLoad和Virtual方法
    XE6 FMX之控件绘制与显示
    Delphi Android程序启动过程
    Delphi中的容器类
    接口
    集合
    class methed
  • 原文地址:https://www.cnblogs.com/jiandanqinxin/p/6036079.html
Copyright © 2011-2022 走看看