输入一个正整数,将该数的各位左右反转输出,即输入123,输出321;怎么用while语句实现
输入一个正整数,将该数的各位左右反转输出,即输入123,输出321;怎么用while语句实现
输入一个正整数,将该数的各位左右反转输出,即输入123,输出321;怎么用while语句实现
2016-12-05
public static void main(String[] args){
int num=123;
int i;
int num1=0;
if(num<=0){
System.out.println("请输入正数!");
}else if(num<10){
System.out.println("结果: "+num);
}else{
while(num>0){
//取当前num值的个位数。
i = num % 10;
//取num去掉个位数后的数字,如123变成12.
num = num / 10;
num1 = num1 * 10 + i;
}
System.out.println("结果: "+num1);
}
}
public class SimpleDemo {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("请输入一个正整数:");
int abc=input.nextInt();
System.out.println("您输入的正整数是:"+abc);
if (abc>0) {
StringBuilder sBuilder=new StringBuilder(String.valueOf(abc));
System.out.println("结果:"+sBuilder.reverse());
}
}
}
import java.util.Scanner;
public class Test{
public static void main(String []args) {
Scanner in=new Scanner(System.in);
System.out.print("输入字符串:");
String s=in.nextLine();
System.out.println("字符串的逆序为:"+'\n'+reverse(s));
in.close();
}
public static String reverse(String s){
char[] chars=s.toCharArray();
int i;
s="";
for(i=chars.length-1;i>=0;i--)
s+=chars[i];
return s;
}
}
举报