feat(01-totwodigits): Добавлено задание

This commit is contained in:
Mark Zheleznyakov
2024-11-07 10:54:15 +03:00
commit 39025d9397
13 changed files with 1175 additions and 0 deletions

View File

@ -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);
}
}