Java Action Brain 06

Source: Internet
Author: User

1. Write a program to convert an integer to a kanji reading string.

For example, "1123" is converted to "1123".        Further, can you change the amount represented in the numbers to "Chinese character expression?" For example, the "¥123.52" converted to "one Bai San Yuan Wu angle of the three points."

Source:


Import Java.util.Scanner;
public class Test {

public static void Main (string[] args) {
TODO auto-generated Method Stub

Scanner in=new Scanner (system.in);
System.out.println ("Enter a number:");
double A;
a=in.nextdouble ();
System.out.println (Digituppercase (a));
}
Public static String digituppercase (double n) {
String fraction[] = {"Angle", "Minute"};
String digit[] = {"0", "one", "II", "three", "premises", "WU", "Lu", "Qi", "ba", "JIU"};
String unit[][] = {{"Yuan", "million", "billion"}, {"", "Pick Up", "Bai", "Thousand"}};

String head = N < 0? "Negative": "";
n = math.abs (n);

String s = "";
for (int i = 0; i < fraction.length; i++) {
s + = (digit[(int) (Math.floor (n * * MATH.POW (ten, I)%)] + fraction[i]). ReplaceAll ("(0.) +", ""); 

if (s.length () < 1) {
s = "whole";

int integerpart = (int) math.floor (n);

for (int i = 0; i < unit[0].length && integerpart > 0; i++) {
String p = "";
For (int j = 0; J < unit[1].length && n > 0; j + +) {
p = digit[integerpart%] + unit[1][j] + p;
Integerpart = INTEGERPART/10;

s = P.replaceall ("(0.) * 0 $ "," "). ReplaceAll (" ^$ "," 0 ") + unit[0][i] + s;

return head + s.replaceall ("(0.) * 0 Yuan "," Yuan "). Replacefirst (" (0.) + "," "). ReplaceAll (" (0.) + "," 0 "). ReplaceAll (" ^ Full $ "," 0 yuan whole ");
}

}

2.

The previous introduction of the JDK provided by the BigInteger can complete the large number of calculations, if not use it, directly using the array to express large numbers, you can achieve the same function? Requirements: (1) Use your large number of classes to achieve plus and minus two functions (2) Read the BigInteger class source code, figuring out what algorithm is used to achieve subtraction four kinds of operations? (3) through the Internet to find the large number of relevant data, to your large number of classes to add multiply, divide, and other functions to seek factorial.

Import Java.util.Scanner;

public class Dashu {

Public static void Main (string[] args) {
//TODO auto-generated method stub
String s1,s2;
Scanner in=new Scanner (system.in);
System.out.println ("Enter the first large number:");
S1=in.next ();
System.out.println ("Enter a second large number:");
S2=in.next ();
System.out.println (Add (S1,S2));
System.out.println (Sub (S1,S2));

}

private static string Add (string A, string b)

System.out.print ("addition:" + + "+" + + b + "=");
char[] aa = new StringBuffer (a). Reverse (). ToString (). ToCharArray ();
char[] bb = new StringBuffer (b). reverse (). ToString (). ToCharArray ();
int aLen = aa.length;
int blen = bb.length;

int len = aLen > Blen? alen:blen;

int[] result = new Int[len + 1];
for (int i = 0; i < len + 1; ++i) {
int aint = i < ALen aa[i]-' 0 ': 0;
int bint = i < Blen bb[i]-' 0 ': 0;
Result[i] = aint + bint;


for (int i=0;i<result.length-1;++i) {
if (result[i]>=10) {
result[i+1] + = RESULT[I]/10;
Result[i]%=;



Boolean flag = true;
stringbuffer sb = new StringBuffer (len);
for (int i=len;i>=0;--i) {
if (result[i]==0&&flag) {
continue;
}else{
Flag=false;

sb.append (Result[i]);

return sb.tostring ();

Public static string sub (string F, string s) {
System.out.print ("subtraction:" + F + "-" + S + "=");

char[] A = new StringBuffer (f). Reverse (). ToString (). ToCharArray ();
char[] B = new StringBuffer (s). Reverse (). ToString (). ToCharArray ();
int lenA = a.length;
int lenB = b.length;

int len = lenA > LenB? lena:lenb;
int[] result = new Int[len];

char sign = ' + ';
//Determine the positive or negative of the final result
if (LenA < LenB) {
Sign = '-';
} else if (LenA = = LenB) {
int i = lenA-1;
While (i > 0 && a[i] = = B[i]) {
i--;

if (A[i] < B[i]) {
Sign = '-';


//Calculate the result set, if the final result is positive, then a-b otherwise b-a
for (int i = 0; i < len; i++) {
int aint = i < LenA? (A[i]-' 0 '): 0;
int bint = i < LenB? (B[i]-' 0 '): 0;
if (sign = = ' + ') {
Result[i] = aint-bint;
} else {
Result[i] = bint-aint;



for (int i = 0; i < result.length-1; i++) {
if (Result[i] < 0) {
Result[i + 1]-= 1;
Result[i] + = ten;



stringbuffer sb = new StringBuffer ();

if (sign = = '-') {
sb.append ('-');


Boolean flag = true;
for (int i = len-1; I >= 0; i--) {
if (result[i] = = 0 && flag) {
continue;
} else {
flag = false;

sb.append (Result[i]);


if (sb.tostring (). Equals ("")) {
Sb.append ("0");

//return value
System.out.println (sb.tostring ());
return sb.tostring ();

}

3.

Randomly generates 10 numbers, fills an array, then displays the array contents with a message box, computes the array element's and displays the result in a message box.

Design idea: The system randomly generates 10 number sum

Source:

Import Javax.swing.JOptionPane;
public class Shuzu {

Public static void Main (string[] args) {
//TODO auto-generated method stub
int a[],sum=0;
a=new int[10];
string Output=new string ();
for (int i=0;i<a.length;i++)
{
a[i]= (int) (Math.random () *100);
}
output+= "a[10]={";
for (int i=0;i<a.length;i++)
{
output+=a[i]+ "";
}
output+= "}";
for (int i=0;i<a.length;i++)
{
Sum+=a[i];
}
output+= "\nsum:" +sum;
joptionpane.showmessagedialog (null, output, "result",
joptionpane.plain_message);
}
}

Java Action Brain 06

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.