| |

VerySource

 Forgot password?
 Register
Search
View: 11020|Reply: 82

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]

2

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-2-2 08:00:02
| Show all posts |Read mode
Give an arabic number
For example: 123456789
Output: One hundred million, three hundred, five thousand, ten thousand, and ten thousand

I thought this function was very simple, but found that if there are multiple zeros in the middle of the number, there will be problems

Post my code, it's bad
    public static String daxie (int sum)
    {
        String [] n = new String [10];
        n [0] = "zero";
        n [1] = "One";
        n [2] = "贰";
        n [3] = "三";
        n [4] = "肆";
        n [5] = "伍";
        n [6] = "陆";
        n [7] = "柒";
        n [8] = "捌";
        n [9] = "玖";
        String [] d = new String [10];
        d [0] = "";
        d [1] = "";
        d [2] = "pick up";
        d [3] = "百";
        d [4] = "仟";
        d [5] = "万";
        d [6] = "pick up";
        d [7] = "百";
        d [8] = "仟";
        d [9] = "billion";
        // Calculate the number of digits
        int wei = (int) Math.floor
                (
                Math.log10 ((double) sum)
                ) +1;
        System.out.println (wei);
        String str = "";
        int digit = 0;
        for (int i = wei; i> 0; i--)
        {
            digit = sum /
            (int) Math.pow (10.0, (double) (i-1));
            str + = n [digit] .toString () + d [i] .toString ();
            sum = sum%
            (int) Math.pow (10.0, (double) (i-1));
        }

        str = str.replace ("pick up", "pick up");
        str = str.replace ("Zero Pickup", "Zero");
           str = str.replace ("零 百", "Zero");
           str = str.replace ("zero 仟", "zero");
           str = str.replace ("zero million", "million");
          for (int i = 1; i <= 6; i ++)
         str = str.replace ("zero zero", "zero");
         str = str.replace ("Zero million", "Zero");
         str = str.replace ("billion", "billion");
         str = str.replace ("zero zero", "zero");
        if (str.endsWith ("zero"))
        {
            str = str.substring (0, str.length ()-1);
        }
        System.out.println (str);
        return str;
    }
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-3-21 09:30:01
| Show all posts
String [] d = new String [10];
d [0] = "";
d [1] = "";
d [2] = "pick up";
d [3] = "百";
d [4] = "仟";
d [5] = "万";
d [6] = "pick up";
d [7] = "百";
d [8] = "仟";
d [9] = "billion";
If you define this to the ninth position, then if the number has 10 or more digits, it will be out of bounds.
In addition, in the judgment of thousands, I think it is necessary to add a judgment
if (wei <5) {
str = str.replace ("Zero million", "Zero");
} else {
str = str.replace ("zero million", "million");
}
Reply

Use magic Report

2

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-3-22 20:00:01
| Show all posts
cloudyangyanI changed it according to your method and still have problems
Reply

Use magic Report

0

Threads

2

Posts

428.00

Credits

Credits
428.00

 China

Post time: 2020-3-23 23:54:31
| Show all posts
[quote] [size = 2] [url = forum.php? mod = redirect&goto = findpost&pid = 9946&ptid = 1673] [color = # 999999]daike1017Posted on 2020-3-22 20:00 [/ color] [/ url] [/ size]
cloudyangyanI changed it according to your method, there are still problems [/ quote]

Is it like this?
[attach] 6 [/ attach]

This post contains more resources

You have to Login for download or view attachment(s). No Account? Register

x
Reply

Use magic Report

1

Threads

20

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-3-24 00:00:01
| Show all posts
class Test
{
    public static void main (String [] args)
    {
        String num = "零 一 贰 三 肆 伍 陆 柒 捌 玖";
        String dw = "Around a hundred trillion yuan";
        String m = "35602001.23";
        String mm [] = null;
        mm = m.split ("\\.");
        String money = mm [0];

        String result = num.charAt (Integer.parseInt ("" + mm [1] .charAt (0))) +
            "Corner" +
            num.charAt (Integer.parseInt ("" + mm [1] .charAt (1))) + "minute";

        for (int i = 0; i <money.length (); i ++)
        {
            String str = "";
            int n = Integer.parseInt (money.substring (money.length ()-i-1,
                money.length ()-i));
            str = str + num.charAt (n);
            if (i == 0)
            {
                str = str + dw.charAt (i);
            }
            else if ((i + 4)% 8 == 0)
            {
                str = str + dw.charAt (4);
            }
            else if (i% 8 == 0)
            {
                str = str + dw.charAt (5);
            }
            else
            {
                str = str + dw.charAt (i% 4);
            }
            result = str + result;
        }
        result = result.replaceAll ("零 万", "万");
        result = result.replaceAll ("zero ([^ 圆] {1})", "zero");
        result = result.replaceAll ("zero +", "zero");
        result = result.replaceAll ("zero circle", "circle");
        result = result.replaceAll ("零 万", "万");
        System.out.println (result);

    }
}

Roughly tested, lz verify to see if there is a problem
Reply

Use magic Report

0

Threads

9

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-3-24 18:30:01
| Show all posts
public class Rectangle
{
    public static String daxie (int sum)
    {
        String [] n = new String [10];
        n [0] = "";
        n [1] = "One";
        n [2] = "贰";
        n [3] = "三";
        n [4] = "肆";
        n [5] = "伍";
        n [6] = "陆";
        n [7] = "柒";
        n [8] = "捌";
        n [9] = "玖";
        String [] d = new String [10];
        d [0] = "";
        d [1] = "pick up";
        d [2] = "百";
        d [3] = "仟";
        String [] e = new String [10];
        e [0] = "万";
        e [1] = "billion";

        // Calculate the number of digits
// int wei = (int) Math.floor
// (
// Math.log10 ((double) sum)
//) +1;
        String old = "" + sum;
        int wei = (old) .length ();
System.out.println ("Calculated number of digits ==" + wei);
        
        ArrayList str = new ArrayList ();
        int digit = 0;
        int digit1 = 0;
        for (int i = wei-1; i> = 0; i-) {
            if (! "0" .equals ("" + old.charAt (i))) {
                str.add (0, d [digit]);
                str.add (0, n [Integer.parseInt ("" + old.charAt (i))]);
                            }
                digit ++;
                if (digit == 4) {
                    digit = 0;
                    str.add (0, e [digit1]);
                    digit1 ++;
                }
        }
        System.out.println (str);
        return str.toString ();
    }

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


Calculate the number of digits == 9
[One, billion, ten, ten, one, ten, ten, ten, three, three hundred, three, ten, four,]
[One, billion, ten, ten, one, ten, ten, ten, three, three hundred, three, ten, four,]

See for yourself
Reply

Use magic Report

2

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-3-25 18:45:01
| Show all posts
meisei
You enter
10000004
Try

Calculate the number of digits == 8
[Billion, one, one, ten thousand, wanton,]
its not right
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-26 11:00:02
| Show all posts
I tested it. I ca n’t find anything wrong. Please give pointers. Which numbers will be problematic ???
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-26 18:45:02
| Show all posts
lz the one,
Reply

Use magic Report

2

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-3-27 12:45:01
| Show all posts
I am the landlord
My program
Input: 10000004

Output: one

It should be:
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