Метод по подсчету цифр:
public static int getCountsOfDigits(Integer num) {
int count = 0;
while (num>0){
int a = (int) Math.pow(10, count);
count++;
num = num / a;
System.out.println(num);
}
return count;
}
Пробное число: 8956798
Вывод:
8956798
895679
8956
8
0
Почему он пропускает цифры: сначало пропусти одну "7", потом две "9 и 5" и далее в геометрической прогрессии. С тепень я возвожу на единицу жеpackage com.javarush.task.task06.task0606;
/*
Чётные и нечётные циферки
*/
import java.io.*;
public class Solution {
public static int even;
public static int odd;
public static void main(String[] args) throws IOException {
//напишите тут ваш код
int odd = 0;
int even = 0;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String txt = reader.readLine();
Integer num = Integer.parseInt(txt);
int countDigits = getCountsOfDigits(num);
System.out.println(countDigits);
int newCount = countDigits;
while (newCount>0){
int num1 = num - (num/((int)Math.pow(10, (newCount-1))));
int num2 = num - num1;
if(num2%2 == 0){
even++;
}
else{
odd++;
}
num1 = num - num2*((int)Math.pow(10, (newCount-1)));
newCount--;
}
System.out.println("Even: " + even + " Odd: " + odd);
}
public static int getCountsOfDigits(Integer num) {
int count = 0;
while (num>0){
int a = (int) Math.pow(10, count);
count++;
num = num / a;
System.out.println(num);
// System.out.println(count);
}
return count;
}
}