zoukankan      html  css  js  c++  java
  • 课堂动手动脑

    (1)将数字转化为汉字输出

    实验代码:

    import java.util.*;
    public class Test_1 {
    public static void main(String args[])
    {
    int n[] = {1,1,2,3};
    String m[] = new String[4];
    for(int i = 0; i < n.length ; i++)
    {
    if(n[i] == 1)
    {
    m[i] = "一";
    }
    else if(n[i] == 2)
    {
    m[i] = "二";
    }
    else
    {
    m[i] = "三";
    }
    }
    System.out.print(m[0] + "千" + m[1] + "百" + m[2] + "十" + m[3]);
    }
    }

    运行截图:

                                                                     

    (2)使用string进行大数据运算

    public class BigNum
    {
    public static void main(String[] args)
    {
    String aa="";
    String bb="";
    char[] a={'1','2','0'};
    char[] b={'5','0'};
    int d=0;
    int e=0;
    for(int i=0;i<a.length;i++)
    {
    aa=aa+a[i];

    }

    d=Integer.parseInt(aa);
    System.out.println(d);
    for(int i=0;i<b.length;i++)
    {
    bb=bb+b[i];

    }

    e=Integer.parseInt(bb);
    System.out.println(e);
    int f=d+e;
    System.out.print(aa+" + "+bb+"="+f);
    }
    }

    运行截图:

                                                             

    BigInteger的用法:

    1.add(); 大整数相加

    BigInteger a=new BigInteger(“23”)

     BigInteger b=new BigInteger(“34”)

    2.subtract(); 相减

    3.multiply(); 相乘

    4.divide();    相除取整

    5.remainder(); 取余

    6.pow();   a.pow(b)=a^b

    7.gcd();   最大公约数

    8.abs(); 绝对值

    9.negate(); 取反数

    10.mod(); a.mod(b)=a%b=a.remainder(b);

    11.max(); min();

    12.punlic int comareTo();

    13.boolean equals(); 是否相等

    (3)随机数组输出以及加和:

    实验代码:

    import javax.swing.JOptionPane;
    import javax.swing.*;
    public class Test2 {
    public static void main(String args[]){
    String output= "10个1000以内的随机数为: ";
    int sum=0;
    int a []=new int [10];
    for(int i = 0;i<10;i++)
    {
    a[i]=(int) (Math.random()*1000);
    output += " "+a[i];
    sum += a[i];
    }
    output +=" 十个数的和是:"+sum;
    JOptionPane.showMessageDialog(null,output,"结果",JOptionPane.PLAIN_MESSAGE);
    }
    }

    实验截图:

                                                                  

    实验流程图:

                                                                          产生随机数

                                                                                  |

                                                                    将产生的随机数放入数组中

                                                                                  |

                                                                       对数组进行遍历加和

                                                                                  |

                                                                  输出数组元素和数组元素的加和

                         

  • 相关阅读:
    最小的K个数
    堆排序
    归并排序
    希尔排序
    快速排序
    二分查找
    数组中出现次数超过一半的数字
    包含min函数的栈
    栈的压入、弹出序列
    中缀表达式转后缀表达式
  • 原文地址:https://www.cnblogs.com/overs/p/6029898.html
Copyright © 2011-2022 走看看