题解 | #数字颠倒#
数字颠倒
https://www.nowcoder.com/practice/ae809795fca34687a48b172186e3dafe
第一种方法:StringBuilder的reverse()功能
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case System.out.println(new StringBuilder(in.nextLine()).reverse().toString()); } } }
第二种方法:因为第一种方法太简单了,感觉不符合出题人的意图,所以有第二种,很好的利用了do。。。while循环中do至少执行一次的特征。
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case new Main().Nixu(in.nextInt()); } } void Nixu(int in) { do { System.out.printf("%d", in % 10); in = in / 10; } while (in > 0); } }