| |

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

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-5-24 17:00:01
| Show all posts
import java.util.Scanner;

public class NumToChinese {

public static void ToChinese (Long num) {
String n [] = {"Zero", "One", "Two", "Three", "Wu", "Wu", "Lu", "柒", "捌", "Jiu"};
String unit [] = {"", "Pickup", "Bai", "仟"};
String unit1 [] = {"100 million", "Ten thousand"};
StringBuilder chi = new StringBuilder ();

for (int i = 0; i <num.toString (). length (); i ++) {
chi.append (n [Integer.parseInt (String.valueOf (num.toString (). charAt (i)))]);
chi.append (unit [(num.toString (). length ()-i-1)% 4]);
if ((num.toString (). length ()-i)% 4 == 1)
{
chi.append (unit1 [(int) Math.floor ((double) (num.toString (). length ()-i) / 4)% 2]);
}
}
String ch = chi.toString ();
ch = ch.replaceAll ("零 千", "zero");
ch = ch.replaceAll ("Zero Bai", "Zero");
ch = ch.replaceAll ("zero pick", "zero");
while (ch.indexOf ("Zero Zero")> 0)
{
ch = ch.replaceAll ("zero zero", "zero");
}
ch = ch.replaceAll ("Zero Ten Thousand", "Ten Thousand");
ch = ch.replaceAll ("Zero Billion", "Billion");
ch = ch.replaceAll ("Billion", "Billion");
System.out.println (ch.substring (0, ch.length ()-1));
}

public static void main (String [] args) {
Scanner in = new Scanner (System.in);
while (true)
{
long num = in.nextLong ();
if (num == 0)
{
System.exit (0);
}
ToChinese (new Long (num));
}
}

}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-5-27 20:30:01
| Show all posts
public class GetNumber {
  public static void main (String [] args) {
  // Get the number from the command line
    int num = Integer.parseInt (args [0]);
    int num1 = num;
    int n = 0;
    while (num1> = 10) {
    num1 = num1 / 10;
    n ++;
    }
    int [] nums = new int [n + 1];
    num1 = num;
    for (int i = 0; i <n + 1; i ++) {
    nums [i] = num1% 10;
    num1 = num1 / 10;
    }
    String [] chNum = {"One", "Two", "Three", "Ran", "Wu", "Lu", "柒", "捌", "Jiu", "Ji"};
    String [] unit = {"", "picking", "bai", "qian", "wan", "picking", "bai", "qian", "100 million", "picking", "bai", " Thousand "," Ten "};
    for (int i = n; i> = 0; i--) {
    System.out.print (chNum [nums [i] -1]);
    System.out.print (unit [i]);
   }
 }
}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-5-31 10:15:01
| Show all posts
Is the standard answer online for this question inaccurate?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-6-7 15:45:01
| Show all posts
I laughed.
if (str.indexOf("10,000")> 0)
            str = str.replace("Zero Ten Thousand", "Ten Thousand");
        else
            str = str.replace("Zero Thousand", "Zero");
        str = str.replace("Picking", "Picking");
        str = str.replace("Zero Pick", "Zero");
        str = str.replace("Zero Bai", "Zero");
        str = str.replace("零仟", "零");
        for (int i = 1; i <= 6; i++)
            str = str.replace("Zero Zero", "Zero");
        str = str.replace("Zero Ten Thousand", "Ten Thousand");
        str = str.replace("Zero billion", "Billion");
        str = str.replace("Zero Zero", "Zero");
        str = str.replace("Billion", "Billion");
        if (str.endsWith("zero")) {
            str = str.substring(0, str.length()-1);
        }
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-6-10 17:45:01
| Show all posts
f (str.indexOf("10,000")> 0)
            str = str.replace("Zero Ten Thousand", "Ten Thousand");
        else
            str = str.replace("Zero Thousand", "Zero");
Change to the following and try again.
if (str.indexOf("10,000")> 0)
            str = str.replace("Zero Ten Thousand", "Ten Thousand");
        else{
            
            if(str.substring(0,str.indexOf("10,000")).indexOf("零千") == -1 || str.substring(0,str.indexOf("10,000")).indexOf(" Zero Bai") == -1 || str.substring(0,str.indexOf("10,000")).indexOf("zero pick") == -1)
                str = str.replace("Zero Ten Thousand", "Ten Thousand");
            else
                str = str.replace("Zero Thousand", "Zero");
        }
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-6-15 20:45:02
| Show all posts
Enter 5 or more "0" in your program and see what is output


If you solve this problem, it is estimated that there is no problem with the program~

Many of the above programs have this problem
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-6-16 23:15:01
| Show all posts
Hello experts, if you enter more than 0, there will be problems, such as 000105.
Haha, thank you for your program
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-6-20 02:30:01
| Show all posts
Very simple, first convert to int to remove the previous 0, then calculate the unit according to the position of each bit, 0 does not add the unit, and consecutive 0s count only one, it seems that there is no difficult place, too simple, too lazy write the code
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 Invalid IP Address

Post time: 2020-6-23 16:00:01
| Show all posts
000123423400000000000130
It's OK to read this correctly.
Reply

Use magic Report

1

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-6-28 02:15:01
| Show all posts
as follows:
public class test{
  int number[];
  String bignum[] = {"zero","one","two","three","four","five","six","seven","eight","long"};
  String sign1[] = {"","Ten","Hundred","Thousand"};
  String sign2[] = {"圆","万","亿","兆"};
  test(String s){
    if(s.length()>sign2.length*4)
      System.exit(0);
    number = new int[s.length()];
    for(int i=0;i<s.length();i++)
      number[s.length()-1-i] = Integer.parseInt(i==s.length()-1?s.substring(i):s.substring(i,i+1));
    run();
  }
  void run(){
    String s="";
    for(int i=number.length-1;i>=0;i--){
      s+=bignum[number[i]];
      s+=i%4==0?sign2[i/4]:number[i]>0?sign1[i%4]:"";
    }
    while(s.indexOf("Zero Zero")!=-1)
      s=s.replaceFirst("zero zero","zero");
    for(int i=0;i<sign1.length;i++)
      while(s.indexOf("zero"+sign2[i])!=-1)
        s=s.replaceFirst("zero"+sign2[i],sign2[i]);
    for(int i=sign2.length-1;i>0;i--)
      while(s.indexOf(sign2[i]+sign2[i-1])!=-1)
        s=s.replaceFirst(sign2[i]+sign2[i-1],sign2[i]);
    System.out.println(s);
  }
  public static void main(String args[]){
    new test(args[0]);
  }
}
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