feat(01-totwodigits): Добавлено задание
This commit is contained in:
@ -0,0 +1,38 @@
|
||||
package ru.mrqiz.twodigits;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class App {
|
||||
private static int convertTo2Digit(int n) {
|
||||
int res = 0;
|
||||
n = Math.abs(n);
|
||||
while (n > 0) {
|
||||
res += n % 10;
|
||||
n /= 10;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner in = new Scanner(System.in);
|
||||
int n = 0;
|
||||
boolean validInput = false;
|
||||
|
||||
while (!validInput) {
|
||||
System.out.print("can i get an int?\n> ");
|
||||
if (in.hasNextInt()) {
|
||||
n = in.nextInt();
|
||||
validInput = true;
|
||||
} else {
|
||||
System.out.println("NaN, wrong.");
|
||||
in.next();
|
||||
}
|
||||
}
|
||||
|
||||
while (Math.abs(n) > 9) {
|
||||
n = convertTo2Digit(n);
|
||||
}
|
||||
System.out.println(n);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user