我有这个程序可以在解析的 int 中找到奇数和偶数的总和,如下所示。我现在的程序是找到从右到左的总和。我如何使它从最后一个数字(即从左到右)开始?
out.print("Please enter a number: ");
String s = in.nextLine();
int x = Integer.parseInt(s);
int y = 0;
int e = 0;
int o = 0;
while (x != 0) {
y = x % 10;
out.print(y + " ");
if (y % 2 == 0) {
e = e + y;
out.print(e + " ");
} else {
o = o + y;
out.print(o + " ");
}
x = x / 10;
}
out.println("sum of odd: " + o);
out.println("sum of even: " + e);
我有点在无限循环中运行
out.print("Please enter a number: ");
String s = in.nextLine();
double x = Integer.parseInt(s);
double y = 0;
double e = 0;
double o = 0;
double length = Math.pow(10, s.length() - 1);
while (x != 0) {
y = x / length;
if (y % 2 == 0) {
e = e + y;
} else {
o = o + y;
}
x = x % length;
}
out.println("sum of odd: " + o);
out.println("sum of even: " + e);