用java实现整型反序
答案:4 悬赏:60 手机版
解决时间 2021-04-15 17:02
- 提问者网友:情歌越听越心酸
- 2021-04-15 07:38
用java实现整型反序
最佳答案
- 五星知识达人网友:迷人又混蛋
- 2021-04-15 08:12
public class NumberInverse
{
private int amount;
private String stringOutput;
private int inverseInt;
public NumberInverse(int input)
{
if (input > 99999 || input < -99999)
{
System.out.println("请输入一个小于5位的整数");
return;
}
// 位数
amount = Integer.toString(Math.abs(input)).length();
// 空格显示
for (char c : Integer.toString(Math.abs(input)).toCharArray())
{
if (stringOutput == null)
{
stringOutput = "";
}
stringOutput += String.format("%s%s", String.valueOf(c),
" ");
}
stringOutput = input >= 0 ? stringOutput : String.format(
"-%s", stringOutput);
// 逆序
String s = "";
for (int i = this.getAmount() - 1; i >= 0; i--)
{
s += String.valueOf((Integer.toString(Math.abs(input))
.toCharArray())[i]);
}
inverseInt = input >= 0 ? Integer.parseInt(s) : Integer
.parseInt(String.format("-%s", s));
}
public int getAmount()
{
return amount;
}
public String getStringOutput()
{
return stringOutput;
}
public int getInverseInt()
{
return inverseInt;
}
public static void main(String[] args)
{
NumberInverse n = new NumberInverse(-32345);
System.out.println(n.getAmount());// 打印位数
System.out.println(n.getStringOutput());// 打印格式化字符串
System.out.println(n.getInverseInt());// 打印逆序的数字
}
}
{
private int amount;
private String stringOutput;
private int inverseInt;
public NumberInverse(int input)
{
if (input > 99999 || input < -99999)
{
System.out.println("请输入一个小于5位的整数");
return;
}
// 位数
amount = Integer.toString(Math.abs(input)).length();
// 空格显示
for (char c : Integer.toString(Math.abs(input)).toCharArray())
{
if (stringOutput == null)
{
stringOutput = "";
}
stringOutput += String.format("%s%s", String.valueOf(c),
" ");
}
stringOutput = input >= 0 ? stringOutput : String.format(
"-%s", stringOutput);
// 逆序
String s = "";
for (int i = this.getAmount() - 1; i >= 0; i--)
{
s += String.valueOf((Integer.toString(Math.abs(input))
.toCharArray())[i]);
}
inverseInt = input >= 0 ? Integer.parseInt(s) : Integer
.parseInt(String.format("-%s", s));
}
public int getAmount()
{
return amount;
}
public String getStringOutput()
{
return stringOutput;
}
public int getInverseInt()
{
return inverseInt;
}
public static void main(String[] args)
{
NumberInverse n = new NumberInverse(-32345);
System.out.println(n.getAmount());// 打印位数
System.out.println(n.getStringOutput());// 打印格式化字符串
System.out.println(n.getInverseInt());// 打印逆序的数字
}
}
全部回答
- 1楼网友:夜余生
- 2021-04-15 10:32
一个财富都木有,要写那么多代码,唉!!!追问哭啊 没财富用了追答public class NumberInverse
{
private int amount=0;
private String stringOutput="";
private int inverseInt=0;
public NumberInverse(int input)
{ //to get the amout
String str = input + "";
if ("-".equals(str.substring(0, 1)))
{
this.amount = (str.substring(1)).length();
} else
{
this.amount = str.length();
}
//to get the stringOutput
for(int i=0;i {
this.stringOutput+=str.charAt(i)+" ";
}
//to inverse the input
String temp="";
if ("-".equals(str.substring(0, 1)))
{
temp="-";
for(int i=str.length()-1;i>0;i--)
{
temp+=str.charAt(i);
}
} else
{
for(int i=str.length()-1;i>=0;i--)
{
temp+=str.charAt(i);
}
}
this.inverseInt=Integer.parseInt(temp);
}
public int getAmount()
{
return amount;
}
public String getStringOutput()
{
return stringOutput;
}
public int getInverseInt()
{
return inverseInt;
}
public static void main(String[] args)
{
NumberInverse test = new NumberInverse(-
{
private int amount=0;
private String stringOutput="";
private int inverseInt=0;
public NumberInverse(int input)
{ //to get the amout
String str = input + "";
if ("-".equals(str.substring(0, 1)))
{
this.amount = (str.substring(1)).length();
} else
{
this.amount = str.length();
}
//to get the stringOutput
for(int i=0;i
this.stringOutput+=str.charAt(i)+" ";
}
//to inverse the input
String temp="";
if ("-".equals(str.substring(0, 1)))
{
temp="-";
for(int i=str.length()-1;i>0;i--)
{
temp+=str.charAt(i);
}
} else
{
for(int i=str.length()-1;i>=0;i--)
{
temp+=str.charAt(i);
}
}
this.inverseInt=Integer.parseInt(temp);
}
public int getAmount()
{
return amount;
}
public String getStringOutput()
{
return stringOutput;
}
public int getInverseInt()
{
return inverseInt;
}
public static void main(String[] args)
{
NumberInverse test = new NumberInverse(-
- 2楼网友:由着我着迷
- 2021-04-15 10:02
public static void main(String[] args) {
NumberInverse ni = new NumberInverse(-956174);
System.out.println("amount is " + ni.getAmount());
System.out.println("stringOutput is " + ni.getStringOutput());
System.out.println("inverseInt is " + ni.getInverseInt());
}
public NumberInverse(int input) {
inverseInt = input;
String s = Integer.toString(inverseInt);
String inverseStr="";
if (s.indexOf("-") >= 0) {
amount = s.length() - 1;
stringOutput = "-";
for (int i = 1; i < s.length(); i++) {
stringOutput += s.charAt(i) + " ";
}
stringOutput = stringOutput.trim();
inverseStr="-";
for (int i = s.length()-1; i >0 ; i--) {
inverseStr += s.charAt(i);
}
inverseInt=Integer.parseInt(inverseStr);
} else {
amount = s.length();
stringOutput = "";
for (int i = 0; i < s.length(); i++) {
stringOutput += s.charAt(i) + " ";
}
stringOutput = stringOutput.trim();
inverseStr="";
for (int i = s.length()-1; i >=0 ; i--) {
inverseStr += s.charAt(i);
}
inverseInt=Integer.parseInt(inverseStr);
}
}
NumberInverse ni = new NumberInverse(-956174);
System.out.println("amount is " + ni.getAmount());
System.out.println("stringOutput is " + ni.getStringOutput());
System.out.println("inverseInt is " + ni.getInverseInt());
}
public NumberInverse(int input) {
inverseInt = input;
String s = Integer.toString(inverseInt);
String inverseStr="";
if (s.indexOf("-") >= 0) {
amount = s.length() - 1;
stringOutput = "-";
for (int i = 1; i < s.length(); i++) {
stringOutput += s.charAt(i) + " ";
}
stringOutput = stringOutput.trim();
inverseStr="-";
for (int i = s.length()-1; i >0 ; i--) {
inverseStr += s.charAt(i);
}
inverseInt=Integer.parseInt(inverseStr);
} else {
amount = s.length();
stringOutput = "";
for (int i = 0; i < s.length(); i++) {
stringOutput += s.charAt(i) + " ";
}
stringOutput = stringOutput.trim();
inverseStr="";
for (int i = s.length()-1; i >=0 ; i--) {
inverseStr += s.charAt(i);
}
inverseInt=Integer.parseInt(inverseStr);
}
}
- 3楼网友:上分大魔王
- 2021-04-15 09:39
public NumberInverse(int input) {
StringBuffer sb = new StringBuffer();
int intTemp = input;
if(intTemp<0){
input = Math.abs(input);
}
char[] strInput = (input+"").toCharArray();
amount = strInput.length;
for(int i=strInput.length-1,j=0;j<=i;i--){
sb = sb.append(" ").append(strInput[i]);
}
stringOutput = (sb.toString()).trim();
inverseInt = Integer.parseInt(stringOutput.replaceAll(" ",""));
if(intTemp<0){
inverseInt= inverseInt*(-1);
stringOutput = "-"+stringOutput;
}
System.out.println(inverseInt);
System.out.println(stringOutput);
System.out.println(amount);
}
StringBuffer sb = new StringBuffer();
int intTemp = input;
if(intTemp<0){
input = Math.abs(input);
}
char[] strInput = (input+"").toCharArray();
amount = strInput.length;
for(int i=strInput.length-1,j=0;j<=i;i--){
sb = sb.append(" ").append(strInput[i]);
}
stringOutput = (sb.toString()).trim();
inverseInt = Integer.parseInt(stringOutput.replaceAll(" ",""));
if(intTemp<0){
inverseInt= inverseInt*(-1);
stringOutput = "-"+stringOutput;
}
System.out.println(inverseInt);
System.out.println(stringOutput);
System.out.println(amount);
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯