请各位高手帮帮忙啊,关于java循环的
大家能不能帮帮忙告诉我,就是将新得到的数字reverse再相减,直到0结束importjava.io.*;publicclassassignment_5{/***@par...
大家能不能帮帮忙告诉我 ,就是将新得到的数字reverse 再相减 ,直到0结束
import java.io.*;
public class assignment_5 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.print("enter a 5 dight number: ");
String input=in.readLine();
int nums=Integer.parseInt(input);
String text="";
for (int i=input.length()-1;i>=0;i--) {
text=text+input.charAt(i);
}
int b=Integer.parseInt(text);
if (b>nums ) {
System.out.println(text+"-"+input+"="+(b-nums));
}
if (nums>b) {
System.out.println(input+"-"+text+"="+(b-nums));
}
} 展开
import java.io.*;
public class assignment_5 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.print("enter a 5 dight number: ");
String input=in.readLine();
int nums=Integer.parseInt(input);
String text="";
for (int i=input.length()-1;i>=0;i--) {
text=text+input.charAt(i);
}
int b=Integer.parseInt(text);
if (b>nums ) {
System.out.println(text+"-"+input+"="+(b-nums));
}
if (nums>b) {
System.out.println(input+"-"+text+"="+(b-nums));
}
} 展开
2个回答
展开全部
按你的思路我写了一下 发敏大现完全没有办法算到0,是个死循环。。颤塌
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Assignment_5 {
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("enter a 5 dight number: ");
String input = in.readLine();
int nums = Integer.parseInt(input);
String text = "";
for (int i = input.length() - 1; i >= 0; i--) {
text = text + input.charAt(i);
}
int b = Integer.parseInt(text);
String rezult = String.valueOf(Math.abs(b - nums));
if (b > nums) {
System.out.println(text + "-" + input + "=" + rezult);
}
if (nums > b) {
System.out.println(input + "-" + text + "=" + rezult);
}
System.out.println(rezult);
while (!rezult.equals("0"茄拿圆)) {
String temp = "";
for (int i = rezult.length() - 1; i >= 0; i--) {
temp = temp + rezult.charAt(i);
}
if (Integer.parseInt(rezult) > Integer.parseInt(temp)) {
System.out
.println(rezult
+ "-"
+ temp
+ "="
+ (Integer.parseInt(rezult) - Integer
.parseInt(temp)));
} else if (Integer.parseInt(rezult) < Integer.parseInt(temp)) {
System.out
.println(temp
+ "-"
+ rezult
+ "="
+ (Integer.parseInt(temp) - Integer
.parseInt(rezult)));
}
rezult = String
.valueOf(Math.abs((Integer.parseInt(temp) - Integer
.parseInt(rezult))));
}
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Assignment_5 {
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("enter a 5 dight number: ");
String input = in.readLine();
int nums = Integer.parseInt(input);
String text = "";
for (int i = input.length() - 1; i >= 0; i--) {
text = text + input.charAt(i);
}
int b = Integer.parseInt(text);
String rezult = String.valueOf(Math.abs(b - nums));
if (b > nums) {
System.out.println(text + "-" + input + "=" + rezult);
}
if (nums > b) {
System.out.println(input + "-" + text + "=" + rezult);
}
System.out.println(rezult);
while (!rezult.equals("0"茄拿圆)) {
String temp = "";
for (int i = rezult.length() - 1; i >= 0; i--) {
temp = temp + rezult.charAt(i);
}
if (Integer.parseInt(rezult) > Integer.parseInt(temp)) {
System.out
.println(rezult
+ "-"
+ temp
+ "="
+ (Integer.parseInt(rezult) - Integer
.parseInt(temp)));
} else if (Integer.parseInt(rezult) < Integer.parseInt(temp)) {
System.out
.println(temp
+ "-"
+ rezult
+ "="
+ (Integer.parseInt(temp) - Integer
.parseInt(rezult)));
}
rezult = String
.valueOf(Math.abs((Integer.parseInt(temp) - Integer
.parseInt(rezult))));
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询