题目:逆序数值
用户输入123456 展示654321
用户输入987654 展示456789
方法1:
import java.util.Scanner;
class Demo1{
public static void main (String[] args){
Scanner sc = new Scanner(System.in);
int input = 0;
//获得整数的位数
int count = 0;
//倍数
int multiple = 1;
//输出的结果
int output = 0;
int i = 0;
int j = 0;
System.out.print("请输入一个整数:");
//用户输入的整数
input = sc.nextInt();
//将输入的整数赋值给 i
i = input;
//记录整数的位数
while(i != 0){
i = i / 10;
count += 1;
}
//将位数赋值给j
j = count;
while(input != 0){
//获得个位数的值
i = input % 10;
while(j > 1){
multiple *= 10;
j -= 1;
}
output += i * multiple;
input = input / 10 ;
multiple = 1;
count -= 1;
j = count;
}
//打印结果
System.out.println(output);
}
}
方法2:
import java.util.Scanner;
class Practice12{
public static void main (String[] args){
// 用户输入的值
int input = 0;
int temp = 0;
// 判断逆序数最高位是否为0
boolean isZero = true;
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个正整数");
input = sc.nextInt();
if(input < 0 ){
System.out.print("-");
input = -input;
}
while(input != 0){
temp = input % 10;
input /= 10;
if(0 == temp && isZero == true){
}else{
System.out.print(temp);
isZero = false;
}
}
}
}
版权声明:本文为weixin_46933543原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。