我尝试了一个程序,但它显示了第一位数字。我想要一个使用 while 循环的中位数程序。 我使用了以下内容-
public class MID {
public static void Indra(int n) {
int a = 0, c = 0, m = 0, r = 0;
a = n;
while (n != 0) {
c++;
n = n / 10;
}
n = a;
if (c % 2 == 0) {
System.out.println("No mid digit exsist!!");
} else {
m = (c + 1) / 2;
c = 0;
while (n != 0) {
c++;
r = n % 10;
if (c == r) {
System.out.println(r);
}
n = n / 10;
}
}
}
}
但它继续给出相同的输出 -
The mid digit of 345 is 3
请帮帮我!
最佳答案
如果你不介意使用不同的逻辑,你可以尝试这个..
int x = 354;
String num = Integer.toString(x);
if(num.length() % 2 != 0){
System.out.println("The mid digit of " + x + " is " + num.charAt(num.length()/2));
}else {
System.out.println("No middle number.");
}
关于java - 如果是的话如何打印数字的中间数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32722674/