码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA语法基础作业——动手动脑以及课后实验性问题 (五)

时间:2016-11-07 00:51:04      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:action   sage   egerp   return   [1]   add   stat   integer   技术分享   

一、请编写程序将整数转换为汉字读法字符串

 1 import java.util.Scanner;
 2 
 3 public class Test {
 4 
 5 public static void main(String[] args) {
 6     Scanner in=new Scanner(System.in);
 7     System.out.println("输入一个数:");
 8     double a;
 9     a=in.nextDouble();
10     System.out.println(digitUppercase(a));
11 }
12 public static String digitUppercase(double n) { 
13     String fraction[] = { "角", "分"}; 
14     String digit[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; 
15     String unit[][] = { { "元", "万", "亿"}, { "", "拾", "佰", "仟"}}; 
16 
17     String head = n < 0 ? "负" : ""; 
18     n = Math.abs(n); 
19 
20     String s = ""; 
21     for (int i = 0; i < fraction.length; i++) { 
22         s += (digit[(int) (Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", ""); 
23     } 
24     if (s.length() < 1) { 
25         s = "整"; 
26     } 
27     int integerPart = (int) Math.floor(n); 
28 
29     for (int i = 0; i < unit[0].length && integerPart > 0; i++) { 
30         String p = ""; 
31     for (int j = 0; j < unit[1].length && n > 0; j++) { 
32         p = digit[integerPart % 10] + unit[1][j] + p; 
33         integerPart = integerPart / 10; 
34         } 
35     s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s; 
36     } 
37     return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整"); 
38     } 
39 
40 }

 程序截图:

技术分享

、数组表示大数

  1 import java.util.Scanner;
  2 
  3 public class Dashu {
  4 
  5 public static void main(String[] args) {
  6     // TODO Auto-generated method stub
  7     String s1,s2;
  8     Scanner in=new Scanner(System.in);
  9     System.out.println("输入第一个大数:");
 10     s1=in.next();
 11     System.out.println("输入第二个大数:");
 12     s2=in.next();
 13     System.out.println(add(s1,s2));
 14     System.out.println(sub(s1,s2));
 15 
 16 }
 17 
 18 private static String add(String a, String b){ 
 19     System.out.print("加法:" + a+ "+" + b + "=");
 20     char[] aa = new StringBuffer(a).reverse().toString().toCharArray(); 
 21     char[] bb = new StringBuffer(b).reverse().toString().toCharArray(); 
 22     int aLen = aa.length; 
 23     int bLen = bb.length; 
 24 
 25     int len = aLen > bLen ? aLen : bLen; 
 26 
 27     int[] result = new int[len + 1]; 
 28     for (int i = 0; i < len + 1; ++i) { 
 29     int aint = i < aLen ? aa[i] - ‘0‘ : 0; 
 30     int bint = i < bLen ? bb[i] - ‘0‘ : 0; 
 31     result[i] = aint + bint; 
 32     } 
 33 
 34     for(int i=0;i<result.length-1;++i){ 
 35         if(result[i]>=10){
 36             result[i+1] += result[i]/10; 
 37             result[i] %= 10; 
 38             } 
 39     } 
 40 
 41     boolean flag = true; 
 42     StringBuffer sb = new StringBuffer(len); 
 43     for(int i=len;i>=0;--i){ 
 44         if(result[i]==0&&flag){ 
 45             continue; 
 46             }
 47         else{ 
 48             flag=false; 
 49             } 
 50         sb.append(result[i]); 
 51         } 
 52         return sb.toString(); 
 53         } 
 54     public static String sub(String f, String s){ 
 55         System.out.print("减法:" + f + "-" + s + "="); 
 56         // 将字符串翻转并转换成字符数组 
 57         char[] a = new StringBuffer(f).reverse().toString().toCharArray(); 
 58         char[] b = new StringBuffer(s).reverse().toString().toCharArray(); 
 59         int lenA = a.length; 
 60         int lenB = b.length; 
 61         int len = lenA > lenB ? lenA : lenB; 
 62         int[] result = new int[len];  
 63         char sign = ‘+‘;  
 64         if (lenA < lenB) { 
 65             sign = ‘-‘; 
 66         } 
 67         else if (lenA == lenB){
 68             int i = lenA - 1; 
 69             while (i > 0 && a[i] == b[i]) { 
 70                 i--; 
 71             } 
 72             if (a[i] < b[i]) { 
 73                 sign = ‘-‘; 
 74             } 
 75         } 
 76         for (int i = 0; i < len; i++) { 
 77             int aint = i < lenA ? (a[i] - ‘0‘) : 0; 
 78             int bint = i < lenB ? (b[i] - ‘0‘) : 0; 
 79             if (sign == ‘+‘) { 
 80                 result[i] = aint - bint; 
 81             } 
 82             else { 
 83                 result[i] = bint - aint; 
 84             } 
 85         } 
 86         for (int i = 0; i < result.length - 1; i++) { 
 87             if (result[i] < 0) { 
 88                 result[i + 1] -= 1; 
 89                 result[i] += 10; 
 90             } 
 91         } 
 92 
 93         StringBuffer sb = new StringBuffer(); 
 94         if (sign == ‘-‘) { 
 95             sb.append(‘-‘); 
 96         }  
 97         boolean flag = true; 
 98         for (int i = len - 1; i >= 0; i--) { 
 99             if (result[i] == 0 && flag) { 
100                 continue; 
101             } 
102             else { 
103                 flag = false; 
104             } 
105             sb.append(result[i]); 
106         } 
107         if (sb.toString().equals("")) { 
108             sb.append("0"); 
109         } 
110         System.out.println(sb.toString()); 
111         return sb.toString(); 
112     } 
113 }

程序截图:

技术分享

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

 1 import javax.swing.JOptionPane;
 2 
 3 public class Shuzu {
 4 
 5 public static void main(String[] args) {
 6     // TODO Auto-generated method stub
 7     int a[],sum=0;
 8     a=new int[10];
 9     String output=new String();
10     for(int i=0;i<a.length;i++){
11         a[i]=(int)(Math.random()*100);
12         }
13     output+="a[10]={";
14     for(int i=0;i<a.length;i++){
15         output+=a[i]+" ";
16         }
17     output+="}";
18     for(int i=0;i<a.length;i++){
19         sum+=a[i];
20         }
21     output+="\nsum:"+sum;
22     JOptionPane.showMessageDialog(null, output,"结果",
23     JOptionPane.PLAIN_MESSAGE);
24     }
25 }

程序截图:

 技术分享

设计思想:

随机产生10个相加数求和。

JAVA语法基础作业——动手动脑以及课后实验性问题 (五)

标签:action   sage   egerp   return   [1]   add   stat   integer   技术分享   

原文地址:http://www.cnblogs.com/XiaoPiHaiEr/p/6036598.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!