| |

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

1

Posts

472.00

Credits

Full Member

Rank: 3Rank: 3

Credits
472.00

 China

Post time: 2020-3-27 20:05:35
| Show all posts
[quote] [size = 2] [url = forum.php? mod = redirect&goto = findpost&pid = 10315&ptid = 1673] [color = # 999999]daike1017Posted on 2020-3-27 12:45 [/ color] [/ url] [/ size]
I am the landlord
My program
Input: 10000004
[/ quote]

I sent a picture before, the code is too bulky and the level is limited, try it
[align = center] [color = # 336699] [font = Tahoma, Helvetica,&amp] [size = 12px] [url =] [img = 1,0] http://bbs.verysource.com/forum .php? mod = image&aid = 7&size = 300x300&key = 737841a268ab02e1&nocache = yes&type = fixnone [/ img] [/ url] [/ u] [/ size] [/ font] [/ color] [/ align]
import java.util.Scanner;
public class Txet {
 public static void main (String [] args) {
  Scanner input = new Scanner (System.in);
  long x = input.nextLong ();
  long y = 0;
  long z = x;
  int k = 0;
  int count = 0;
  int count1 = 0;
  int i = 0;
  int n = 0;
  boolean is = true;
  boolean wan = true;
  if (x <0)
  {
   x = -x;
   System.out.print ("negative");
  }
  do
  {
   y = y * 10 + x% 10;
   x / = 10;
   count ++;
  ) while (x> 0); // Data read
  
  int [] a = new int [count];
  int [] b = new int [count];
  for (i = 0; i <count; i ++)
     {
   k = (int) (z% 10);
   if (k == 0)
   {b = 1;}
   z / = 10;
     }
        count1 = count;
        if (count> = 9)
         if (b [4] == 1&&b [5] == 1&&b [6] == 1&&b [7] == 1) {
         wan = false;
         } // When there are more than 100 million, there will be 10,000-level all-zero judgment, and you can continue to judge 100,000-level
  z = y; // output ready
  for (i = 0; i <count; i ++)
     {
   k = (int) (y% 10);
   if (k == 0)
   {a = 1;}
   y / = 10;
     } // Order output in reverse order
         
     for (i = 0; i <count; i ++)
     {
      k = (int) (z% 10);
   if (k == 0) // Whether output is 0
      {
    if (count1 == 5&&wan)
          {
        System.out.print ("million");
          }
    if (count1 == 9) // Billion bit judgment
          {
        System.out.print ("Billion");
          }
    if (i == 0 || i == count-1) // One digit and the last digit, prevent the array below from crossing the boundary
    {
     System.out.print ("");
     }
    else {
    if (a [i-1] == 0&&a [i + 1] == 0&&count1! = 5&&count1! = 9) // Judge the individual 0 bits
     {
     System.out.print ("zero");
        is = false; // Single 0 mark
     }
    if (is) // continuous 0 entry
    {
     if (a [i-1] == 0 || i == count-1) {
      System.out.print ("zero"); // The next occurrence of non-zero output is a zero
     }
     else {
      System.out.print (""); // Otherwise no output
     }
    }}
    is = true;
   }
   else // If the output is not 0, output according to the rules
      {switch (k)
      {
            case 0: System.out.print ("zero"); break;
   case 1: System.out.print ("One"); break;
   case 2: System.out.print ("贰"); break;
   case 3: System.out.print ("Three"); break;
   case 4: System.out.print ("random"); break;
   case 5: System.out.print ("吴"); break;
   case 6: System.out.print ("陆"); break;
   case 7: System.out.print ("柒"); break;
   case 8: System.out.print ("捌"); break;
   case 9: System.out.print ("玖"); break;
      }
   switch (count1)
   {
   case 0: System.out.print (""); break;
   case 1: System.out.print (""); break;
   case 2: System.out.print ("pick up"); break;
   case 3: System.out.print ("百"); break;
   case 4: System.out.print ("仟"); break;
   case 5: System.out.print ("万"); break;
   case 6: System.out.print ("pick up"); break;
   case 7: System.out.print ("百"); break;
   case 8: System.out.print ("仟"); break;
   case 9: System.out.print ("Billion"); break;
   case 10: System.out.print ("pick up"); break;
   case 11: System.out.print ("百"); break;
   case 12: System.out.print ("仟"); break;
   }}
   count1--;
   z / = 10;
     }
 }
}

This post contains more resources

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

x
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 Invalid IP Address

Post time: 2020-3-28 19:00:01
| Show all posts
I just wrote a class without thinking about program structure and readability. But the tests seem to work.
public class Test3 {

/ **
* @param args
* /
public static void main (String [] args) {
// TODO Auto-generated method stub
Ranch
// For example, enter: 123456789
// Output: One hundred million, three hundred and ten thousand Wulu, one hundred thousand and ten thousand
Ranch
        
        String str = "1234567890123456789"; // The number to be converted
        String outString = "";
        
        int strLeanth = 0;
        int grade = 0;
        
        if (str! = null) {
        strLeanth = str.trim (). length ();
        }
        if (strLeanth> 0) {
        grade = strLeanth / 4;
        }
        Test3 t = new Test3 ();
        for (int i = 0; i <grade; i ++) {
        String temp = str.substring (strLeanth-4-i * 4, strLeanth-i * 4);
        System.out.println ("temp:" + temp);
        String unit = t.getUnit (i, "");
        outString = t.analysable (temp, unit) + outString;
        }
        if (strLeanth% 4> 0) {
        String temp = str.substring (0, strLeanth% 4);
        System.out.println ("temp:" + temp);
        String unit = t.getUnit (grade, "");
        outString = t.analysable (temp, unit) + outString;
    }
        System.out.println ("outString:" + outString);
}
Ranch
/ **
* Get unit
* @param thisUnit
* @param unit
* @return
* /
public String getUnit (int thisUnit, String unit) {
Ranch
if ((thisUnit-2) <= 0) {
if (thisUnit == 1) {
unit = "万" + unit;
} else if (thisUnit == 2) {
unit = "billion" + unit;
}
} else {
unit = getUnit (thisUnit-2, "100 million" + unit);
}
return unit;
}
Ranch
Ranch
Ranch
/ *
* Numeric string of up to 4 digits
* unit is only 3: "", "" million "or" billion "
* /
public String analysable (String umStr, String unit) {
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] = "pick up";
        d [2] = "百";
        d [3] = "仟";
        d [4] = "万";
        d [5] = "billion";
        
String reading = "";
for (int i = 0; i <umStr.length (); i ++) {
String temp = umStr.substring (umStr.length ()-1-i, umStr.length ()-i);
reading = n [new Integer (temp) .intValue ()] + d [i] + reading;
}
return reading + unit;
}

}
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-3-30 22:30:01
| Show all posts
After you have added your judgment, what else is wrong? For example, what numbers will cause problems when measured?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-31 09:00:01
| Show all posts
gosh. Quite a problem. I have seen such an algorithm before. The problem of bank amount conversion has not been figured out. The main problem is that the algorithm is not difficult to implement. . .
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 Invalid IP Address

Post time: 2020-4-3 11:45:01
| Show all posts
Sorry, I did n’t judge String str = "01000040"; // The number to be converted is now what should be changed after modification. The algorithm and program structure and readability were not considered in detail.

public class Test3 {

/ **
* @param args
* /
public static void main (String [] args) {
// TODO Auto-generated method stub
        String str = "01000040"; // The number to be converted
        String outString = "";
        
        int strLeanth = 0;
        int grade = 0;
        
        if (str! = null) {
        strLeanth = str.trim (). length ();
        }
        if (strLeanth> 0) {
        grade = strLeanth / 4;
        }
        Test3 t = new Test3 ();
        
        for (int i = 0; i <grade;) {
        String temp = str.substring (strLeanth-4-i * 4, strLeanth-i * 4);
        System.out.println ("temp:" + temp);
        String unit = t.getUnit (i, "");
        i ++;
        if (strLeanth% 4 == 0&&i == grade)
        {
        outString = t.analysable (temp, unit, false) + outString;
        } else {
        outString = t.analysable (temp, unit, true) + outString;
        }
        Ranch
        Ranch
        // System.out.println ("outString:" + outString);
        }
        if (strLeanth% 4> 0) {
        String temp = str.substring (0, strLeanth% 4);
        //System.out.println("temp: "+ temp);
        String unit = t.getUnit (grade, "");
        outString = t.analysable (temp, unit, false) + outString;
    }
        System.out.println ("outString:" + outString);
}
Ranch
/ **
* Get unit
* @param thisUnit
* @param unit
* @return
* /
public String getUnit (int thisUnit, String unit) {
Ranch
if ((thisUnit-2) <= 0) {
if (thisUnit == 1) {
unit = "万" + unit;
} else if (thisUnit == 2) {
unit = "billion" + unit;
}
} else {
unit = getUnit (thisUnit-2, "100 million" + unit);
}
return unit;
}
Ranch
/ *
* Numeric string of up to 4 digits
* unit is only 3: "", "" million "or" billion "
* lool is used to determine if there are other bits before: true
* /
public String analysable (String umStr, String unit, boolean lool) {
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] = "pick up";
        d [2] = "百";
        d [3] = "仟";
        d [4] = "万";
        d [5] = "billion";
        
String reading = "";
String tempReading = "";
for (int i = umStr.length ()-1; i> 0; i-) {
String temp = umStr.substring (umStr.length ()-i, umStr.length ()-i + 1);
//System.out.println("reading: "+ temp);
System.out.println ("===" + ((unit.length ()> 0 || i> 0)&&new Integer (temp) .intValue () == 0));
if ((unit.length ()> 0 || i> 0)&&new Integer (temp) .intValue () == 0) {
tempReading = n [new Integer (temp) .intValue ()];
//System.out.println("tempReading: "+ tempReading);
} else {
if (lool) {
reading = reading + tempReading + n [new Integer (temp) .intValue ()] + d [i-1];
} else
{
lool = true;
reading = n [new Integer (temp) .intValue ()] + d [i-1];
}
System.out.println ("reading:" + reading);
}
}
return reading + unit;
}

}
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 Invalid IP Address

Post time: 2020-4-4 16:30:02
| Show all posts
There is an error
Above
for (int i = umStr.length ()-1; i> 0; i--)
Change that line to:
for (int i = umStr.length (); i> 0; i--)
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 Invalid IP Address

Post time: 2020-4-4 22:15:01
| Show all posts
Ha ha, there is another mistake
The corresponding method is replaced by:
/ *
* umStr numeric string within 4 digits
* There are only 3 units: "", "10,000" or "100 million"
* lool is used to judge whether there are other bits before: true
* /
public String analysable (String umStr, String unit, boolean lool) {
String [] n = new String [10];
        n [0] = "zero";
        n [1] = "一";
        n [2] = "Two";
        n [3] = "Three";
        n [4] = "regarding";
        n [5] = "Wu";
        n [6] = "Lu";
        n [7] = "柒";
        n [8] = "捌";
        n [9] = "Jiu";
        
        String [] d = new String [10];
        d [0] = "";
        d [1] = "pick up";
        d [2] = "bai";
        d [3] = "仟";
        d [4] = "10,000";
        d [5] = "100 million";
        
String reading = "";
String tempReading = "";
for (int i = umStr.length (); i> 0; i-) {
String temp = umStr.substring (umStr.length ()-i, umStr.length ()-i + 1);
System.out.println ("reading:" + temp);
if ((unit.length ()> 0 || i> 0)&&new Integer (temp) .intValue () == 0) {
tempReading = "zero";
//System.out.println("tempReading: "+ tempReading);
} else {
if (lool) {
reading = reading + tempReading + n [new Integer (temp) .intValue ()] + d [i-1];
tempReading = "";
} else
{
lool = true;
reading = n [new Integer (temp) .intValue ()] + d [i-1];
}
System.out.println ("reading:" + reading);
}
}
return reading + unit;
}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-4-10 12:45:01
| Show all posts
I used Delphi to write a conversion between numbers and Chinese characters before. I think the "zero" process is error-prone; then it is easy to miss some special cases.
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-4-10 18:45:01
| Show all posts
I wrote
If you have any questions, let's discuss together :)

import java.io. *;
public class NumberConversion {

The
public static void main (String [] args) {
String s = null;
try {
s = getString ();
} catch (IOException e) {
The
e.printStackTrace ();
}
int a = Integer.parseInt (s);
The
The
System.out.println (saparateNumber (a));

}
// Separate data
public static String saparateNumber (int a) {
String str = "";
int i = 0;
boolean yi = false;
boolean wan = false;
if (a> = 100000000) {
str + = conversionCapitalization2 (a / 100000000) + "100 million";
if (a / 100000000 <1000)
str = str.substring (1);
a = a% 100000000;
yi = true;
The
}
if (a> = 10000) {
str + = conversionCapitalization2 (a / 10000) + "10,000";
if (a / 10000 <1000&&yi == false)
str = str.substring (1);
a = a% 10000;
wan = true;
The
}
if (a> 0) {
The
The
str + = conversionCapitalization2 (a / 1);
if (a <1000&&wan == false)
str = str.substring (1);
The
}
return str;
}
The
// Turn to upper case
public static String conversionCapitalization (int a) {
String cpzat = "";
switch (a) {
case 0:
cpzat = "zero";
break;
case 1:
cpzat = "一";
break;
case 2:
cpzat = "Two";
break;
case 3:
cpzat = "three";
break;
case 4:
cpzat = "Random";
break;
case 5:
cpzat = "Wu";
break;
case 6:
cpzat = "Lu";
break;
case 7:
cpzat = "柒";
break;
case 8:
cpzat = "捌";
break;
case 9:
cpzat = "jiu";
break;
The
The
The
The
}
The
return cpzat;
}
public static String conversionCapitalization2 (int a) {
String tr = "";
boolean q = false;
boolean b = false;
boolean s = false;
The
if (a> = 1000) {
The
tr + = conversionCapitalization (a / 1000) + "仟";
a = a% 1000;
q = true;
}
if (a> = 100) {
if (q == true)
tr + = conversionCapitalization (a / 100) + "百";
else
tr + = "zero" + conversionCapitalization (a / 100) + "百";
a = a% 100;
b = true;
}
if (a> = 10) {
if (b == true)
tr + = conversionCapitalization (a / 10) + "Pick up";
else
tr + = "zero" + conversionCapitalization (a / 10) + "pick up";
a = a% 10;
s = true;
}
if (a> 0) {
if (s == true)
tr + = conversionCapitalization (a / 1);
else
tr + = "zero" + conversionCapitalization (a / 1);
}
The
return tr;
The
}
public static String getString () throws IOException {
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader br = new BufferedReader (isr);
The
The
return br.readLine ();
The
}

}
Reply

Use magic Report

0

Threads

23

Posts

17.00

Credits

Newbie

Rank: 1

Credits
17.00

 China

Post time: 2020-4-24 09:30:01
| Show all posts
I also wrote it, interested in testing it. Ha ha.
import java.io. *;


public class a
{
   public static void main (String [] args)
   {
     String s = new String ();
     BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
     
     while (true)
     {
        try {s = br.readLine ();} catch (Exception e) {}
       Convert c = new Convert (s.toCharArray ());
       System.out.println (c);
     }
   }
}
class Convert
{
  final String [] str1 = {"", "ten", "hundred", "thousand"};
  final String [] str2 = {"100 million", "10,000"};
  final String [] number = {"", "One", "Two", "Three", "Wu", "Wu", "Lu", "柒", "捌", "Jiu"};
  char [] num_char;
  StringBuffer sb = new StringBuffer ();

  public Convert (char [] num)
  {
    num_char = num;
  }
  public String toString ()
  {
    int 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 ();
  }
  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&&i! = 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

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