| |

VerySource

 Forgot password?
 Register
Search
Author: daike1017

A java written question, thought it was very simple, but I didn't write it right for a long time! Please prawn give a c

  [Copy link]

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-7-19 22:00:01
| Show all posts
public class test {
public static void main(String[] arg) throws IOException {
String d="Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands and Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands in
String s="Zero One Two Three Four Wu Lu Qi Ba Ji";
boolean zero=false;
int value=1101111001, count=0;
String Cvalue="";
while(value>0) {
int b=value-value/10*10;
if(!zero || b!=0) {
String t="";
if(b!=0) {
t=s.substring(b,b+1);
if(count>0) t+=d.substring(count-1,count);
zero=false;
}
else {
t="zero";
zero=true;
}
Cvalue=t+Cvalue;
}
count++;
value/=10;
}
To
System.out.println(Cvalue);
}
}
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-7-20 00:00:01
| Show all posts
There are still some problems, fix them:
public class test {
public static void main(String[] arg) throws IOException {
String d="Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands and Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands of Thousands in
String s="Zero One Two Three Four Wu Lu Qi Ba Ji";
boolean zero=false;
int value=2021000789, count=0;
String Cvalue="";
while(value>0) {
int b=value-value/10*10;
if(!zero || b!=0) {
String t="";
if(b!=0) {
t=s.substring(b,b+1);
if(count>0) t+=d.substring(count-1,count);
zero=false;
}
else if(count!=4&&count!=8){
t="zero";
zero=true;
}
Cvalue=t+Cvalue;
}
if(count==4&&!Cvalue.contains("万")) Cvalue="万"+Cvalue;
if(count==8&&!Cvalue.contains("billion")) Cvalue="billion"+Cvalue;
count++;
value/=10;
}
System.out.println(Cvalue);
}
}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-20 15:15:01
| Show all posts
I did a bit,
Under the test, the effect is good.
----------------------------------------------
package com;

import java.io.*;

/**
* @author cct
*/
public class a
{
public static void main(String[] args)
{
String s = new String();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

while (true)
{
try
{
System.out.println("Please enter the number: and press Enter");
s = br.readLine();

Convert c = new Convert(s.toCharArray());
System.out.println(c);
}
catch (Exception e)
{
}
}
}
}

/**
* @author cct
*/
class Convert
{
final String[] str1 = {"", "Pi", "Hundred", "Thousand" };

final String[] str2 = {"100 million", "Ten thousand" };

final String[] number = {"", "One", "Two", "Three", "Wu", "Wu", "Lu", "柒", "捌", "玖" };

char[] num_char;

StringBuffer sb = new StringBuffer();

int last;

public Convert(char[] num)
{
num_char = num;
}

public String toString()
{
last = num_char.length% 4;
int pos = 0;
add(sb, new String(num_char, 0, last));
pos += last;
for (int i = (int) (num_char.length / 4); i> 0; i--)
{
if (i% 2 == 0)
sb.append(str2[0]);
else
sb.append(str2[1]);
add(sb, new String(num_char, pos, 4));
pos += 4;
}
if (last == 0)
sb.deleteCharAt(0);
return sb.toString();
}

/**
* @param sb
* @param temp
*/
private void add(StringBuffer sb, String temp)
{
char[] temp2 = temp.toCharArray();
int length = temp.length();
for (int i = 0; i <length; i++)
{
if (temp2[i] == '0')
if (sb.lastIndexOf("zero") != sb.length()-1&&last != 0)
sb.append("zero");
else
;
else
{
sb.append(number[temp2[i]-'0']);
sb.append(str1[length-i-1]);
}
}
try
{
if (sb.lastIndexOf("zero") == sb.length()-1)
sb.deleteCharAt(sb.lastIndexOf("zero"));
}
catch (Exception e)
{
}
}
}
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-20 16:30:01
| Show all posts
Nothing to do at noon, so there should be no problem, if there is any please raise it, everyone encourages
import java.io.*;
import java.util.*;
/**
   Express the entered number in Chinese
*/

public class NumberTest
{
private char ch;
private String[] charOfNum = {"zero","一","贰","三","四","Wu","Lu","柒","捌","玖"};
private String[] shibq = {"","lift","百","千"};
private String[] wany = {"a","10,000","billion","10,000","billion","10,000","billion"};

String inputStringNum()
//Enter a valid number from the keyboard
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String string = null;
try
{
string = reader.readLine();
}catch(IOException ex)
{
System.err.println(ex.getMessage());
}
return stringReverse(disinfectZero(string));
}
To
private String disinfectZero(String string)
//Clear the zero before the valid digit of the input number
{
int length = string.length();
int i = 0;
String stringReturn = string;
if(Long.parseLong(string) == 0L)
{
stringReturn = "0";
}
else
{
while(string.charAt(i++) == '0')
{
stringReturn = string.substring(i,length);
}
}
return stringReturn;
}
To
private String stringReverse(String string)
//Arrange the entered numbers in reverse order
{
int length = string.length();
String stringReturn = "";
for(int i = 0;i<length;i++)
{
ch = string.charAt(i);
stringReturn = ch+stringReturn;
}
To
return stringReturn;
}
To
public String change()
{
String string = inputStringNum();//Get input data
String resultStr;
String resultString = "";//Save the result
To
if(Long.parseLong(string) == 0L)
{
resultString = "zero";
}
else
{
To
//Divide the data into four groups
int length = string.length();
int intoffour = length/4;
if((length%4) != 0)
{
intoffour+=1;
}
To
//Store the data in a string array, and then convert them separately
String stringnum[] = new String[intoffour];
for(int i = 0;i<intoffour-1;i++)
{
stringnum[i] = string.substring(i*4,(i+1)*4);
}
stringnum[intoffour-1] = string.substring((intoffour-1)*4,length);
To
//Get the complete expression
for(int i = 0;i<intoffour;i++)
{
resultStr = "";
for(int j = 0;j<stringnum[i].length();j++)
{
ch = stringnum[i].charAt(j);
String str = ""+ch;
int charAt = Integer.parseInt(str);
//if(charAt != 0)
//{
resultStr = charOfNum[charAt]+shibq[j]+resultStr;
//}
}
if(Integer.parseInt(stringnum[i]) != 0)
{
resultString = resultStr+wany[i]+resultString;
}
else
{
resultString = "zero"+resultString;
}
}
}
resultString = resultString.replaceAll("零 Pick", "零");
       resultString = resultString.replaceAll("零百", "零");
       resultString = resultString.replaceAll("零千", "零");
resultString = resultString.replaceAll("[零]+", "零");
resultString = resultString.replaceAll("zero","");
resultString = resultString.replaceAll("零万", "万");
       resultString = resultString.replaceAll("zero billion", "100 million");
return resultString;
}
To
public static void main(String[] args) throws Exception
{
NumberTest test = new NumberTest();
        System.out.println(test.change());
    }
}
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-20 18:00:02
| Show all posts
super541Two floors upstairs
You enter 0000002 to see what the result is?
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-20 19:15:01
| Show all posts
It's better now
import java.io.*;
import java.util.*;
/**
   Express the entered number in Chinese
*/

public class NumberTest
{
private char ch;
private String[] charOfNum = {"zero","一","贰","三","四","Wu","Lu","柒","捌","玖"};
private String[] shibq = {"","lift","百","千"};
private String[] wany = {"a","10,000","billion","10,000","billion","10,000","billion"};
public String exit = null;

String inputStringNum()
//Enter a valid number from the keyboard
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String string = null;
try
{
string = reader.readLine();
}catch(IOException ex)
{
System.err.println(ex.getMessage());
}
if(string.equals("exit"))
{
System.exit(0);
}
return stringReverse(disinfectZero(string));
}
To
private String disinfectZero(String string)
//Clear the zero before the valid digit of the input number
{
int length = string.length();
int i = 0;
String stringReturn = string;
if(Long.parseLong(string) == 0L)
{
stringReturn = "0";
}
else
{
while(string.charAt(i++) == '0')
{
stringReturn = string.substring(i,length);
}
}
return stringReturn;
}
To
private String stringReverse(String string)
//Arrange the entered numbers in reverse order
{
int length = string.length();
String stringReturn = "";
for(int i = 0;i<length;i++)
{
ch = string.charAt(i);
stringReturn = ch+stringReturn;
}
To
return stringReturn;
}
To
public String change()
//Convert numbers into text expression
{
String string = inputStringNum();//Get input data
String resultStr;
String resultString = "";//Save the result
To
if(Long.parseLong(string) == 0L)
{
resultString = "zero";
}
else
{
To
//Divide the data into four groups
int length = string.length();
int intoffour = length/4;
if((length%4) != 0)
{
intoffour+=1;
}
To
//Store the data in a string array, and then convert them separately
String stringnum[] = new String[intoffour];
for(int i = 0;i<intoffour-1;i++)
{
stringnum[i] = string.substring(i*4,(i+1)*4);
}
stringnum[intoffour-1] = string.substring((intoffour-1)*4,length);
To
//Get the complete expression
for(int i = 0;i<intoffour;i++)
{
resultStr = "";
for(int j = 0;j<stringnum[i].length();j++)
{
ch = stringnum[i].charAt(j);
String str = ""+ch;
int charAt = Integer.parseInt(str);
resultStr = charOfNum[charAt]+shibq[j]+resultStr;
}
if(Integer.parseInt(stringnum[i]) != 0)
{
resultString = resultStr+wany[i]+resultString;
}
else
{
resultString = "zero"+resultString;
}
}
}
resultString = resultString.replaceAll("零 Pick", "零");
       resultString = resultString.replaceAll("零百", "零");
       resultString = resultString.replaceAll("零千", "零");
resultString = resultString.replaceAll("[零]+", "零");
resultString = resultString.replaceAll("zero","");
resultString = resultString.replaceAll("个","");
resultString = resultString.replaceAll("零万", "万");
       resultString = resultString.replaceAll("zero billion", "100 million");
return resultString;
}
To
public static void main(String[] args) throws Exception
{
NumberTest test = new NumberTest();
System.out.println("Please enter a number and press Enter, if you want to exit, please enter exit");
while(true)
{
System.out.println(test.change());
}
        
    }
}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-20 21:30:01
| Show all posts
Use automata theory to do it. Divide the number into 4-digit sections (Section).
public class RMBConvert
{
public RMBConvert()
{
To
}

private static string[] faceVal = new String[] {"zero", "一", "贰", "三", "四",
"Wu", "Lu", "柒", "捌", "玖" };

private static string []unit = new string[]{"千","百","take",""};

private static string[] level = new String[] {"round", "take", "百", "1,000", "10,000", "100 million" };

public string Convert(string rmb)
{
int len ​​= 0;
string ret = "";
string tmpY = "";
string tmpW = "";
string tmpG = "";
bool bFlag = false;


if(rmb.Length> 8)
rmb = rmb.PadLeft(12, '0');
else if(rmb.Length> 4)
rmb = rmb.PadLeft(8, '0');
else rmb = rmb.PadLeft(4, '0');

if(rmb.Length> 8)
{
tmpY = ConvertSection(rmb, 0, 4, false);
if(tmpY != "zero")
{
ret += tmpY + "billion";
bFlag = true;
}
tmpW = ConvertSection(rmb, 4, 4, bFlag);
if(tmpW != "zero")
{
ret += tmpW + "10,000";
bFlag = true;
}
tmpG = ConvertSection(rmb, 8, 4, bFlag);
if(tmpG != "zero")
ret += tmpG;
else{
if(tmpY == "zero"&&tmpW == "zero"&&tmpG == "zero")
ret = "zero";
}
}
else if(rmb.Length> 4)
{
tmpW = ConvertSection(rmb, 0, 4, false);
if(tmpW != "zero")
{
ret += tmpW + "10,000";
bFlag = true;
}
tmpG = ConvertSection(rmb, 4, 4, bFlag);
if(tmpG != "zero")
{
ret += tmpG;
}
else
{
if(tmpW == "zero")
ret = "zero";
}
}
else{
ret = ConvertSection(rmb, 0, 4, false);
}
return ret;
}

public string ConvertSection(string value, int start, int len, bool headerZero)
{
return ConvertSection(value.Substring(start, len), headerZero);
}

public string ConvertSection(string value)
{
return ConvertSection(value, false);
}
/// <summary>
/// Mainly look at this
/// </summary>
/// <param name="value"></param>
/// <param name="headerZero"></param>
/// <returns></returns>
public string ConvertSection(string value, bool headerZero)
{
string regVal = value.PadLeft(4, '0');
string strOutput = "";

int state = 0;
for(int i=0; i<4;i++)
{
int crr = regVal[i]-0x30;
switch(state)
{
case 0:
if( crr == 0)
{
state = 1;
}
else{
state = 2;
strOutput += faceVal[crr] + unit[i];
}
break;
case 1:
if( crr == 0)
{
state = 3;
}
else
{
state = 5;
if( headerZero)
strOutput += "zero";
strOutput += faceVal[crr] + unit[i];
}
break;
case 2:
if( crr == 0)
{
state = 4;
}
else
{
state = 5;
strOutput += faceVal[crr] + unit[i];
}
break;
case 3:
if( crr == 0)
{
state = 11;
}
else
{
state = 6;
if( headerZero)
strOutput += "zero";
strOutput += faceVal[crr] + unit[i];
}
break;
case 4:
if( crr == 0)
{
state = 7;
}
else
{
state = 6;
strOutput += "zero" + faceVal[crr] + unit[i];
}
break;
case 5:
if( crr == 0)
{
state = 7;
}
else
{
state = 6;
strOutput += faceVal[crr] + unit[i];
}
break;
case 6:
if( crr == 0)
{
state = 8;
}
else
{
state = 9;
strOutput += faceVal[crr] + unit[i];
}
break;
case 7:
if( crr == 0)
{
state = 10;
}
else
{
state = 9;
strOutput += "zero" + faceVal[crr] + unit[i];
}
break;
case 11:
if( crr == 0)
{
state = 12;
strOutput = "zero";
}
else
{
state = 13;
if( headerZero)
strOutput += "zero";
strOutput += faceVal[crr] + unit[i];
}
break;
case 8:break;
case 9:break;
case 10:break;
case 12:break;
case 13:break;
} // end of switch
} // end of for
return strOutput;
}
}
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-7-20 22:30:01
| Show all posts
I have posted so many codes for the first time and I don’t want to read it. Why don’t you talk about the idea of ​​solving the problem? What method is used? I think this is much better than looking at various versions of the code
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-7-20 23:00:01
| Show all posts
How do we read a string of numbers? What are the rules?
1. Ignore leading zeros
2. Read from the most significant non-zero bit, add the title to the number, and ignore it if it is zero
   For example, 5 becomes "Five", and then according to the position, the third place corresponds to "Bai", adding together is "Wu Bai"

Did not consider too much, the wrong place was revised downstairs
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-7-21 12:00:01
| Show all posts
package test;

public class Money {
private static String[] UNIT = {"round", "take", "百", "千", "万", "take", "百", "千",
"Billion", "Pick up", "Bai", "Thousand" };

private static String[] CHINESE_NUMBER = {"zero", "一", "贰", "三", "四", "五",
"Lu", "柒", "捌", "玖" };

private static String NUMBER = "0123456789";

public static void main(String[] args) {
long amount = 100000006543L;
String result = formatMoney(amount);
System.out.print(result);
}

public static String formatMoney(long amount) {
String money = Long.toString(amount);
String result = "";
char letter;
boolean hasZero = false;
final int count = money.length();
for (int i = 0; i <count; i++) {
letter = money.charAt(i);
if (letter == '0') {
hasZero = true;
if (count-1-i == 0) {
result += "circle";
}
if (count-1-i == 4&&!result.endsWith("亿")) {
result += "10,000";
hasZero = false;
}
if (count-1-i == 8) {
result += "billion";
hasZero = false;
}
} else {
if (hasZero) {
result += "zero" + CHINESE_NUMBER[NUMBER.indexOf(letter)]
+ UNIT[count-1-i];
} else {
result += CHINESE_NUMBER[NUMBER.indexOf(letter)]
+ UNIT[count-1-i];
}
hasZero = false;
}
}
return result;
}
}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list