java中 将数字转换成字符

2025-03-10 16:53:02
推荐回答(5个)
回答1:

方法一:直接强制转换。如:String str= (String)123;
方法二:直接通过空字符串+数字的形式转换为字符串(前后都可以用)。如:String str= ""+123;
方法三:直接通过包装类来实现。如:String str = String.valueOf(1231);

回答2:

//import java.util.Scanner;
public class welcome {
public static void main(String[] args) {
// Scanner input=new Scanner(System.in);
int count = 0;
for (int i = 33; i <= 126; i++) {
char c = (char) i;//i强制转换成char类型之后为什么不使用?
{
// System.out.print(i + " ");//写错了 是c不是i,i是int类型的当然直接就输出数字了
System.out.print(c + " ");
count++;
if (count % 10 == 0)
System.out.println();
}
}
}
}

回答3:

i+" "会把‘i’转换成字符串的,而“++”只适用于数字类型的,所以会报错

回答4:

public class welcome {
public static void main(String[] args){
// Scanner input=new Scanner(System.in);
int count=1;
for(int i=33;i<=126;i++)
{
char c=(char)i;
System.out.print(c+"\t");
count++;
if(count%10==0)
System.out.println();
}
}
}

回答5:

public static void main(String[] args){
// Scanner input=new Scanner(System.in);
int count=0;
for(int i=33;i<=126;i++)
{
char c=(char)i;
{
System.out.print(c+" ");
count++;
if(count%10==0)
System.out.println();
}
}
}