Java倒序输出数字

要求用户输入一个10位数的数字,然后倒序输出java完成
2024-12-29 07:50:25
推荐回答(3个)
回答1:

import java.util.Scanner;

public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[] ch = null;
System.out.println("请输入一个十位数的数字");
String str = sc.next();
if (str.trim().length() != 10) {
System.out.println("你输入的数字格式不正确,清检查后重新输入");
}else {
ch = str.toCharArray();
}
for (int i = ch.length; i > 0 ; i--) {
System.out.print(ch[i-1]);
}
}
}

回答2:

StringBuffer sb =new StringBuffer();
sb.append("1234567890");
System.out.println(sb.reverse().toString());

回答3:

public class Test{

public StringBuilder Write() throws IOException {
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
String content;
StringBuilder sb = new StringBuilder();

sb.append(buff.readLine());

return sb.reverse();
}

public static void main(String[] args) throws ClassNotFoundException, IOException {
System.out.println(new Test().Write());
}
}