| |

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-24 13:00:01
| Show all posts
The above program dropped a sentence, removing the sentence with the trailing zero:
...
Cvalue=Cvalue.replaceFirst("hundreds of millions","hundreds of millions of");//Remove the illegal situation of hundreds of millions of connections
if(Cvalue.endsWith("zero")) Cvalue=Cvalue.substring(0,Cvalue.length()-1);//Remove the trailing zero
System.out.println(Cvalue);
...
An example is:
1002003090: One hundred million two hundred and three thousand ninety
Reply

Use magic Report

0

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-7-24 13:45:02
| Show all posts
import java.util.Hashtable;
import java.util.Vector;
class Converter {
// The count in China is a unit of four digits. For example, 1 5487 4584 corresponds to a unit of hundreds of millions. Default
// In each unit of four digits, the processing method is the same, the maximum absolute value digits that can be processed currently is UNIT_NAME.length * 4
private final static String[] UNIT_NAME = {"100 million", "10,000", ""};
private final static String[] SUB_UNIT_NAME = {"千", "百", "take"};
private final static String[] NUMBER_NAME = {"zero", "一", "贰", "三", "四", "Wu", "Lu", "柒", "捌", "玖"};
private static Hashtable<Integer, String> nums = new Hashtable<Integer, String>();
static {
for (int i = 0; i <NUMBER_NAME.length; i++) {
nums.put(i, NUMBER_NAME[i]);
}
}
public static String convert(long value) throws TooBigNumberException {
boolean isMinus = value <0;
if (isMinus) {
value = Math.abs(value);
}
String result = "";
String valueString = String.valueOf(value);
char[] chars = valueString.toCharArray();
if (chars.length> UNIT_NAME.length * 4) {
throw new TooBigNumberException();
} else {
// Currently it can be processed, divided into four sections, each section is processed the same, and then the unit of each section is added
String[] segments = splitValue2Segments(chars, 4);
for (int i = 0, j = UNIT_NAME.length-segments.length; i <segments.length; i++, j++) {
if (segments.length> 1&&i == segments.length-1) {
if (Integer.parseInt(segments[i]) <100) {
result += "zero";
}
}
if (Integer.parseInt(segments[i])> 0) {
result += translate(segments[i]) + UNIT_NAME[j];
}

}
}
if (isMinus) {
result = "negative" + result;
}
return result;
}
/**
* Convert a segment of up to four numbers into natural language representation. Each segment consists of up to four elements, and the maximum number is 9999
*
* @param value
* @return
*/
private static String translate(String value) {
String result = "";
int intValue = Integer.parseInt(value);
int thousandBit = intValue / 1000;
int hundredBit = (intValue-thousandBit * 1000) / 100;
int tenthBit = (intValue-thousandBit * 1000-hundredBit * 100) / 10;
int oneBit = intValue-thousandBit * 1000-hundredBit * 100-tenthBit * 10;
if (thousandBit> 0) {
result += nums.get(thousandBit) + "千";
if (hundredBit> 0) {
result += nums.get(hundredBit) + "百";
if (tenthBit> 0) {
if (tenthBit> 1) {
result += nums.get(tenthBit) + "lift";
} else {
result += "lift";
}

if (oneBit> 0) {
result += nums.get(oneBit);
}
} else {
if (oneBit> 0) {
result += "zero";
result += nums.get(oneBit);
}
}
} else {
if (tenthBit> 0) {
result += "zero";
if (tenthBit> 1) {
result += nums.get(tenthBit) + "lift";
} else {
result += "lift";
}

if (oneBit> 0) {
result += nums.get(oneBit);
}
} else {
if (oneBit> 0) {
result += nums.get(oneBit);
}

}

}
} else {
if (hundredBit> 0) {
result += nums.get(hundredBit) + "百";
if (tenthBit> 0) {
if (tenthBit> 1) {
result += nums.get(tenthBit) + "lift";
} else {
result += "lift";
}

if (oneBit> 0) {
result += nums.get(oneBit);
}
} else {
if (oneBit> 0) {
result += "zero";
result += nums.get(oneBit);
}
}
} else {
if (tenthBit> 0) {
if (tenthBit> 1) {
result += nums.get(tenthBit) + "lift";
} else {
result += "lift";
}
if (oneBit> 0) {
result += nums.get(oneBit);
}
} else {
if (oneBit> 0) {
result += nums.get(oneBit);
}
}
}
}
// System.out.println("Translating from "+ value +" To :" + result);
return result;
}
public static void main(String[] args) {
try {
System.out.println(convert(1000000009));
} catch (TooBigNumberException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Divide the character array into the specified number of segments
*
* @param chars
* @return
*/
public static String[] splitValue2Segments(char[] chars, int segments) {
int count = 0;
Vector<String> v = new Vector<String>();
StringBuffer sb = new StringBuffer();
for (int i = chars.length-1; i >= 0; i--) {
if (count >= segments) {
v.add(sb.toString());
sb = new StringBuffer();
count = 0;
}
sb.insert(0, chars[i]);
count++;
}
if (sb.length()> 0) {
v.add(sb.toString());
}
String[] result = new String[v.size()];
for (int i = v.size()-1, j = 0; i >= 0; i--) {
result[j++] = v.elementAt(i);
System.out.println(v.elementAt(i));
}
return result;
}
}
class TooBigNumberException extends Exception {
public String toString() {
return "The value is too large and the current system cannot handle it.";
}
}
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-7-25 21:15:01
| Show all posts
import java.util.ArrayList;
import java.util.regex.*;
public class MoneyTranslater {
public static final String [] unitStrs={"","10,000","100 million"," trillion"};
public static final String [] numUnitStrs={"","take","百","千"};
public static final String [] numStrs={"zero","一","贰","三","四","Wu","Lu","柒","捌","玖"};
public static String translate(String num){
StringBuffer returnStr=new StringBuffer();
//String num=String.valueOf(number);
String [] strs1=splitByDigit(num, 4);
boolean f=true;//Similar to flag
boolean f2=true;//Similar to flag2
boolean flag2=true;//It can be filled with zero, then it is true
for (int i = 0; i <strs1.length; i++) {
int m=Integer.valueOf(strs1[i]);
if(f){
if(m==0) continue;
else{
f=false;
}
}
To
if(m==0){
if(f2&&flag2){//If the zero-fillable flag behind it is true and the area can be zero-filled, then zero-fill
returnStr.insert(0, numStrs[0]);
f2=false;
}
continue;
}else{
f2=true;//If there is a non-zero area, set the area zero padding flag to true
}
returnStr.insert(0, unitStrs[i]);
boolean flag=true;//If the first non-zero number appears from the first place, it is false;
To
String [] strs2=splitByDigit(strs1[i],1);
for (int j = 0; j <strs2.length; j++) {
int n=Integer.valueOf(strs2[j]);
if(flag){
if(n==0) continue;//If zero appears when the first non-zero bit has not yet appeared, skip
else{
flag=false; //A number that is not zero has appeared since the ones digit and can start counting
}
}
if(n==0){
if(flag2){
returnStr.insert(0,numStrs[0]);
flag2=false;//A zero has been added. If 0 appears again, skip it unless there is a non-zero bit
}
continue;
}else{
flag2=true;//A non-zero bit appears and the flag is set to true to indicate that zeros can be added
}
To
returnStr.insert(0, numUnitStrs[j]);
returnStr.insert(0, numStrs[n]);
}
}
return returnStr.toString();
}
To

/**
* This method implements the source operation string src, divided according to the given number of divisions. For example
* <code>String[] ss=splitByDigit("123456889abcdefg",4)</code> The result is an array
* @param src
* @param digit
* @return
*/
public static String[] splitByDigit(String src,int digit){
String destStr=src;
ArrayList<String> list=new ArrayList<String>();
while(destStr.length()>0){
if(destStr.length()>digit){
list.add(destStr.substring(destStr.length()-digit));
destStr=destStr.substring(0,destStr.length()-digit);
}else{
list.add(destStr);
destStr="";
}
}
String [] returnS=new String [list.size()];
return list.toArray(returnS);
}
/**
* @param args
*/
public static void main(String[] args) {

System.out.println(translate("12000001000001"));
To
To
}
}
The above unitStrs can modify the unit by yourself
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-7-26 13:30:02
| Show all posts
import java.util.ArrayList;
import java.util.regex.*;
public class MoneyTranslater {
public static final String [] unitStrs={"","10,000","100 million"," trillion"};
public static final String [] numUnitStrs={"","take","百","千"};
public static final String [] numStrs={"zero","一","贰","三","四","Wu","Lu","柒","捌","玖"};
public static String translate(String num){
StringBuffer returnStr=new StringBuffer();
//String num=String.valueOf(number);
String [] strs1=splitByDigit(num, 4);
boolean f=true;//Similar to flag
boolean f2=true;//Similar to flag2
boolean flag2=true;//It can be filled with zero, then it is true
boolean f3=true;//Can you add zero after each large unit appears
for (int i = 0; i <strs1.length; i++) {
int m=Integer.valueOf(strs1[i]);
if(f){
if(m==0) continue;
else{
f=false;
}
}
To
if(m==0){
if(f2&&flag2){//If the zero-fillable flag behind it is true and the area can be zero-filled, then zero-fill
returnStr.insert(0, numStrs[0]);
f2=false;
}
continue;
}else{
f2=true;//If there is a non-zero area, set the area zero padding flag to true
}
returnStr.insert(0, unitStrs[i]);
boolean flag=true;//If the first non-zero number appears from the first place, it is false;
To
String [] strs2=splitByDigit(strs1[i],1);
for (int j = 0; j <strs2.length; j++) {
int n=Integer.valueOf(strs2[j]);
if(flag){
if(n==0){
if(f3&&i!=0){
returnStr.insert(1, numStrs[0]);
f3=false;
}
continue;//If zero appears when the first non-zero bit has not yet appeared, skip it
To
}
else{
flag=false; //A number that is not zero has appeared since the ones digit and can start counting
}
}
if(n==0){
if(flag2){
returnStr.insert(0,numStrs[0]);
flag2=false;//A zero has been added. If 0 appears again, skip it unless there is a non-zero bit
}
continue;
}else{
flag2=true;//A non-zero bit appears and the flag is set to true to indicate that zeros can be added
}
To
returnStr.insert(0, numUnitStrs[j]);
returnStr.insert(0, numStrs[n]);
}
f3=true;
}
return returnStr.toString();
}
To

/**
* This method implements the source operation string src, divided according to the given number of divisions. For example
* <code>String[] ss=splitByDigit("123456889abcdefg",4)</code> The result is an array
* @param src
* @param digit
* @return
*/
public static String[] splitByDigit(String src,int digit){
String destStr=src;
ArrayList<String> list=new ArrayList<String>();
while(destStr.length()>0){
if(destStr.length()>digit){
list.add(destStr.substring(destStr.length()-digit));
destStr=destStr.substring(0,destStr.length()-digit);
}else{
list.add(destStr);
destStr="";
}
}
String [] returnS=new String [list.size()];
return list.toArray(returnS);
}
/**
* @param args
*/
public static void main(String[] args) {

System.out.println(translate("10200101001001"));
To
To
}
}
Made a correction, the zeros between units can appear
Reply

Use magic Report

0

Threads

9

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-7-26 17:15:01
| Show all posts
Fix your own program
package jm.util;

import java.util.ArrayList;

/**
* <p>Title: JM integrates Swing controls and uses configuration information</p>
*
*/
public class JMRectangle
{
    @SuppressWarnings("unchecked")
public static String daxie(String sum)
    {
        String [] n=new String[10];
        n[0]="";
        n[1]="一";
        n[2]="贰";
        n[3]="Three";
        n[4]="4";
        n[5]="W";
        n[6]="Lu";
        n[7]="7";
        n[8]="捌";
        n[9]="玖";
        String [] d=new String[10];
        d[0]="";
        d[1]=" Pick up";
        d[2]="百";
        d[3]="1,000";
        String [] e=new String[10];
        e[0]="10,000";
        e[1]=" billion";

        //Calculate the number of digits
        int wei = sum.length();
System.out.println("Calculate the number of digits=="+wei);

        ArrayList str = new ArrayList();
        int digit = 0;
        int digit1 = 0;
        for (int i = wei-1; i >= 0; i--) {
            if (digit == 4) {
                digit = 0;
                str.add(0, e[digit1]);
                digit1++;
                if(digit1==2)digit1=0;
            }
            if (!"0".equals("" + sum.charAt(i))) {
                str.add(0, d[digit]);
                str.add(0, n[Integer.parseInt("" + sum.charAt(i))]);
            }
            digit++;
        }
        System.out.println(str);
        return str.toString();
    }

    public static void main(String[] args)
    {
        System.out.println(daxie("1234567890001"));
    }
}

Easy to use
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-7-26 19:45:01
| Show all posts
The writing upstairs is not standard
(!"0".equals("" + sum.charAt(i))) Why not write (!'0'==sum.charAt(i))
ArrayList str = new ArrayList(); Why not write List str=new ArrayList(); The naming is not standard
Why is the array not written as private static String[] CHINESE_NUMBER = {"zero", "one", "two", "three", "4", "wu",
"Lu", "柒", "捌", "玖" };
According to what you mean ArrayList
It's better to change to StringBuffer
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-7-26 20:15:01
| Show all posts
Correct it: ('0'!=sum.charAt(i))
Reply

Use magic Report

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-7-27 07:45:01
| Show all posts
I also join in the fun:)
-----------------------------------------
public final class RMB{
   private RMB(){
   }

   /**
     * Convert the given Arabic numerals into Chinese capital money
     *
     * @param input given Arabic numerals
     * @return Chinese capitalized money
     */
    public static String toRMB(String input) {

        if (input == null || input.equals("")) {
            return "zero";
        }

        // Determine whether the input is a number (the method is not posted, first comment)
        //if (!isNumber(input)) {
        // throw new IllegalArgumentException(
        // "The money input must be a number!");
        //}

        // Determine whether there is a decimal point (round up the given string. The method is not posted, first comment)
        //input = format(round(toDouble(input).doubleValue()));
        int dotIndex = input.indexOf(".");
        if (dotIndex> 0) {
            if (dotIndex == input.length()-2) {
                input = input + "0";
            }
            else if (dotIndex == input.length()-1) {
                input = input + "00";
            }
        }
        else {
            input = input + ".00";
        }

        // Maximum number of digits: 15 digits
        if (dotIndex> 14) {
            throw new IllegalArgumentException(
                "The money input is too large to convert!");
        }

        final String[] numbers = new String[] {"zero", "one", "two", "three", "4", "wu",
            "Lu", "柒", "捌", "玖"};
        final String[] units = new String[] {"分", "角", "元", "take", "百", "千", "万",
            "Pick up", "Hundred", "Thousand", "One Hundred", "One Hundred", "One Hundred", "Thousand", "Million"};
        final String[] invalids = new String[] {"Zero Yuan", "Zero[Thousand Hundred Ten Yuan Jiao Min]", "Zero+", "Zero Billion",
            "Zero million", "Zero Yuan", "100 million*+10,000"};
        final String[] valids = new String[] {"Pick up yuan", "zero", "zero", "billion", "million", "yuan",
            "Billion"};

        // Reverse the given number
        StringBuffer inputReversed = new StringBuffer(input).reverse();
        inputReversed.deleteCharAt(inputReversed.indexOf("."));

        // Convert by bit and add units
        StringBuffer resultBuffer = new StringBuffer();
        int numberLength = inputReversed.length();
        for (int index = 0; index <numberLength; index++) {
            resultBuffer.append(units[index]);
            resultBuffer.append(numbers[Integer.parseInt(inputReversed.charAt(index)
                + "")]);
        }

        // Replace illegal expression
        String result = resultBuffer.reverse().toString();
        for (int i = 0; i <invalids.length; i++) {
            result = result.replaceAll(invalids[i], valids[i]);
        }

        // If it starts with a zero, remove the zero
        if (result.startsWith("zero")) {
            result = result.substring(1, result.length());
        }
        // If it ends with a zero, remove the zero
        if (result.endsWith("zero")) {
            result = result.substring(0, result.length()-1);
        }
        // If there is no corner, add the whole word
        if (result.indexOf("angle") <0&&result.indexOf("分") <0) {
            result = result + "full";
        }

        return result;
    }
   
    public static void main(String[] args) {
        String money = "100000000010.12645";
        System.out.println(RMB.toRMB(money));

        String moneyString = "10.01";
        System.out.println(RMB.toRMB(moneyString));
    }
}
Reply

Use magic Report

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-7-27 10:45:01
| Show all posts
hehe:)
String money = "100000000010.12645";
Change to
String money = "100000000010.12";
Annotated
I can’t keep two decimal places and then round it up:),
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-29 15:30:01
| Show all posts
Funny, ACCP software training S1 test questions
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