The idea is similar to "not only but also how", but it seems that it is not convenient for him to submit the code.
At the same time, in order to help you understand, a string interception method is also provided to achieve the same effect.
Reference code
import? java.util.Scanner;
public? class? NumDemo? {
public? static? void? main(String[]? args)? {
while? (true)? {
int? num? =? getNumber();
System.out.println ("method 1? :The? converted? number? is:"? +? convert(num)); //? Method 1
System.out.println ("Method 2? :The? converted? number? is:"? +? convert2(num)); //? Method 2
System.out.println();
}
}
//? This method is used to prompt the user for input and return an integer.
public? static? int? getNumber()? {
Scanner? sc? =? new? Scanner(System.in);
while? (true)? {
System.out.print("Please? enter? an? integer:");
String? line? =? sc.nextLine();
try? {
int? num? =? Integer.parseInt(line.trim()); //From string to integer
return? num;
}? catch? (Exception? e)? {//If the input is not an integer, you will be prompted to enter again.
System.out.println("Sorry! ? Must? enter? an? integer"); //? Prompt the user to enter a mistake.
System.out.println();
continue;
}
}
}
//? What is this method used for? Convert numbers? :? Using the method of string interception.
public? static? int? convert(int? a)? {
//Because the input is negative, the absolute value is taken here.
String? str? =? String.valueOf(Math.abs(a)); //? Integers take absolute values and convert them into strings.
String? result? =? ""; //? A string that holds the result.
int? len? =? str.length(); //? Length of string
String? s 1? =? str.substring(len? /? 2? +? len? %? 2); //? Front part
String? s2? =? len? %? 2? ==? 0""? :? str.charAt(len? /? 2)? +? ""; //? If the string length is odd and the middle is unchanged.
String? s3? =? str.substring(0,? len? /? 2); //? The back part
result? =? s 1? +? s2? +? s3; //? All connected.
int? num? =? Integer.parseInt(result); //? String to integer
if? (a? > =? 0)? {//If a positive number is entered, it will be returned.
return? num;
}? else? {
return? num? *? - 1; //If a negative number is entered, it will be returned.
}
}
//? What is this method used for? Convert numbers? Did you clean it up? "Not only but also what"? The train of thought
public? static? int? convert2(int? a)? {
String? str? =? String.valueOf(Math.abs(a)); //? Integers take absolute values and convert them into strings.
char[]? cs? =? str.toCharArray(); //? Convert to a character array
for? (int? i? =? 0; ? i? < ? cs.length? /? 2; ? i++)? {
char? temp? =? cs[i];
cs[i]? =? cs[cs.length? -? cs.length? /? 2? +? i];
cs[cs.length? -? cs.length? /? 2? +? i]? =? temp;
}
int? num? =? Integer.parseInt(new? String(cs)); //? String to integer
if? (a? > =? 0)? {
return? num;
}? else? {
return? num? *? - 1;
}
}
} test effect
Please? enter? an? integer: 123456
Method 1? :The? converted? number? is:456 123
Method 2? :The? converted? number? is:456 123
Please? enter? an? integer:56789
Method 1? :The? converted? number? is:89756
Method 2? :The? converted? number? is:89756
Please? enter? an? integer:abc
Sorry! ? Must? enter? an? integer