本文目錄一覽:
- 1、如何用java編一個程序將輸入的阿拉伯數字轉換成大寫的漢字??
- 2、java中如何把字元串中的數字轉換為漢字?
- 3、使用Java程序如何讓阿拉伯數字轉換成中文?
- 4、java數字轉換漢字
- 5、java編程 數字轉換成漢字
- 6、Java字元串或數字怎麼轉換成字元?
如何用java編一個程序將輸入的阿拉伯數字轉換成大寫的漢字??
import java.util.HashMap;
import java.util.Scanner;
public class Convert {
public static void main(String[] args) {
HashMapInteger, String hashMap = new HashMapInteger, String();
hashMap.put(0, “零”);
hashMap.put(1, “壹”);
hashMap.put(2, “貳”);
hashMap.put(3, “叄”);
hashMap.put(4, “肆”);
hashMap.put(5, “伍”);
hashMap.put(6, “陸”);
hashMap.put(7, “柒”);
hashMap.put(8, “捌”);
hashMap.put(9, “玖”);
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
for (int i = 0; i input.length(); i++) {
System.out.print(hashMap.get(input.charAt(i) – 48));
}
}
}
java中如何把字元串中的數字轉換為漢字?
public class Test {
public static void main(String[] args) {
System.out.println(format(“登記編號123456正在審批過程中。”));
}
public static String format(String text) {
for (int i = 0; i 10; i++) {
text = text.replace((char) (‘0’ + i),
“零一二三四五六七八九”.charAt(i));
}
return text;
}
}
使用Java程序如何讓阿拉伯數字轉換成中文?
import java.util.Scanner;
public class Num
{
public Num() {}
static String []bigNum={“零”,”一”,”二”,”三”,”四”,”五”,”六”,”七”,”八”,”九”};
static String getNUM(String str)
{
int t=Integer.parseInt(str);
return bigNum[t];
}
public static void main(String[] args)
{
try
{
String strbig=new String(“”);
System.out.print(“請輸入數字:”);
Scanner sc=new Scanner(System.in);
long num=sc.nextLong();
String temp=String.valueOf(num);
int b=temp.indexOf(“.”);
int s=temp.length()-(b+1);
int j=b;
for (int i =0; ib;i++) {
strbig+=getNUM(temp.substring(i,i+1));
j–;
}
temp=temp.substring(b+1,temp.length());
for (int i = 0; i s; i++)
{
strbig+=getNUM(temp.substring(i,i+1));
}
System.out.println(“轉換結果:”+strbig);
}
catch(Exception ex)
{
System.out.println(“請輸入整數”);
}
}
}
java數字轉換漢字
25187是漢字『扣』的字元編碼。
每一個漢字,字母,標點符號都有自己的字元編碼。
你看這個地址里的表就明白了。鍵盤上你按住alt鍵,在先鍵盤(數字區)輸入對應的數字,顯示出來的也是數字代碼對應的字元。
比如:在能輸入文字的地方:按住alt,在小鍵盤(數字區)上依次按下3,5放開所有鍵,你看到游標處顯示的是『#』
java編程 數字轉換成漢字
我自己寫的,匆忙寫的。我有時間再改進改進。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Stack;
/*
* 本程序
*/
public class Transfer {
public StackInteger transfer(int n){
StackInteger st = new StackInteger();
int division = 0; //餘數
while(n=10){
division = n%10;
st.push(division);
n = n/10;
}
st.push(n); //將最高位壓棧
return st;
}
public static void main(String[]args){
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String in = “”;
try {
in = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
int n = 0;
try{
n = Integer.parseInt(in);
} catch(NumberFormatException e){
e.printStackTrace();
}
Transfer tf = new Transfer();
StackInteger s = tf.transfer(n);
/*
while(!s.empty()){
System.out.print(s.pop()); //測試語句
}
*/
HashMapInteger, String hp1 = new HashMapInteger, String(); //第一個映射表
hp1.put(0, “零”); //根據所在位的數值與中文對應
hp1.put(1, “一”);
hp1.put(2, “二”);
hp1.put(3, “三”);
hp1.put(4, “四”);
hp1.put(5, “五”);
hp1.put(6, “六”);
hp1.put(7, “七”);
hp1.put(8, “八”);
hp1.put(9, “九”);
HashMapInteger, String hp2 = new HashMapInteger, String(); //第二個映射表
hp2.put(2, “十”); //根據所在位數,與中文對應
hp2.put(3, “百”);
hp2.put(4, “千”);
hp2.put(5, “萬”);
hp2.put(6, “十萬”);
hp2.put(7, “百萬”);
hp2.put(8, “千萬”);
hp2.put(9, “億”);
//System.out.println(s.size());
String out = “”;
while(!s.isEmpty()){
int temp = s.pop();
if(s.size()==0){
if(temp !=0){
out = out + hp1.get(temp);
}
}
else{
if(temp==0){
out = out + hp1.get(temp);
}
else{
out = out + hp1.get(temp) + hp2.get(s.size()+1);
}
}
}
System.out.println(out);
}
}
對於如2008之類的數,輸出的是二千零零八,還需要加以判斷,我再去處理下。
還有涉及萬以上的數,比如
123456
輸出的是一十萬二萬三千四百五十六,也必須增加判斷。
Java字元串或數字怎麼轉換成字元?
String str = “123”;
int num = 12;
//字元串轉換為數字
int tranToNum = Integer.parseInt(str, 16);//這裡的16表示十六進位,也可以是十進位或是其他進位(如果不寫,這裡默認是10進位)
//數字串轉化為字元串
//方法一:通過包裝類來實現
String tranToStr = String.valueOf(num);
//方法二:直接通過空字元串+數字的形式轉換為字元串
String str= “”+num;
//方法三:強制類型轉換
String str= (String)num;
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/251853.html