java编写一个程序段,输入一个3位整数,将它反向输出.如输入123,输出321.

2024-12-27 08:36:13
推荐回答(5个)
回答1:

import java.util.*;
public class KZTinputArray {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Pls input a string:");
String str = in.nextLine();
// int a[] = new int[str.length()];
/*
* for (int i = 0; i < str.length(); i++) { a[i] = str.charAt(i) - '0';
* }
*/
char[] a = str.toCharArray();
for (int i = a.length-1; i >=0; i--) {
System.out.print(a[i] + "\t");
}
}
}

采纳哈!!!

回答2:

在键盘上输入随意三位数得到值a

算法如下,反向输出的个位十位和百位

x=个位=a/100
y=十位=a%100/10
z=百位=((a%100)%10)
system.out.print(z*100+y*10+x)

回答3:

import java.util.Scanner;
public class Test{
public static void main(String[]args){
Scanner ss=new Scanner(System.in);
System.out.println("请输入3位整数");
String num=ss.next();
int numLength=num.length();
if(numLength!=3){
System.out.println("请输入正确的3位整数!");
}else{
char temp[]=new char[numLength];
for(int i=0;itemp[i]=num.charAt(i);
}
for(int i=0;ifor(int j=0;jint num1=(int)temp[i];
int num2=(int)temp[j];
if(num1>num2){
num1=num1+num2;

num2=num2-num1;

num2=num1+num2;
temp[i]=num1;
temp[j]=num2;
}
}
}
for(int i=0;iSystem.out.print(temp[i]);
}
}
}
}

回答4:

将3为整数变成一个string,然后从最后提取就行了

回答5:

public class ReverseDemo{

public static void main(String args[])
{
String a1=args[0];
StringBuffer na=new StringBuffer(a1);
System.out.println("The reversed string is: "+na.reverse());
}
}