gpt4 book ai didi

java - 程序未正确读取 if/else 语句

转载 作者:行者123 更新时间:2023-11-30 02:00:33 25 4
gpt4 key购买 nike

这是一个列出有关来自 Scanner 对象的整数输入的几个事实的程序。但是,我在最后的 if/else 语句方面遇到了一些麻烦。

问题是如果输入是0以外的正整数,程序总是读取这样的语句: System.out.println("j) 它比它的反转值小:"+ reverse);。如果它是一个负int,它总是打印 System.out.println("j) 它比它的反向值大:"+ reverse);

我认为这是因为 reverse 中存储的数据是 0,因为 intverse = 0; 是在 while 循环之前声明的。但是,程序可以正确打印输入的相反内容。

import java.util.Scanner;

public class Integer {

public static void main(String[] args) {
System.out.println("This program will:");
System.out.println("1) Prompt you for an integer then \n2) List several facts about that integer");
Scanner keyboard = new Scanner (System.in); // define a Scanner object attached to a keyboard
System.out.print("\nEnter an integer: "); // prompt the user to enter an integer

while ( ! keyboard.hasNextInt()) { // is the first input value an int?
String badInput; // assign non-integer inputs to badInput
badInput = keyboard.next();
System.out.println("Error: expected an integer, encountered: " + badInput);
System.out.print("Please enter an integer: ");
}

int integer = keyboard.nextInt(); // assign the input to the integer variable
System.out.println("A list of several facts about the number: " + integer); // safe to read first input value

System.out.println("================================================================");

// print the input with space betweeen each digits
System.out.print("a) The digit(s) in it is/are: ");
String number = String.valueOf(integer);
for ( int count = 0; count < number.length(); count++) {
char counted = number.charAt(count); // read each digit in the input
System.out.print(counted + " ");
}

System.out.println(); // skip a line

// determine whether the input is negative or positive
if ( integer >= 0 ) {
System.out.println("b) It is positive");
}
else {
System.out.println("b) It is negative");
}

// determine whether the input is even or odd
if (integer % 2 == 0) {
System.out.println("c) It is even");
}
else {
System.out.println("c) It is odd");
}
int countOdd = 0;
int countEven = 0;
int countDigit = 0;
int countZero = 0;
int reverse = 0;
int sum = 0;
int product = 1;
int readRightMost;

while(integer != 0) {
readRightMost = integer % 10; // read rightmost digit in the input
reverse = reverse * 10 + readRightMost;
integer /= 10; // drop the rightmost digit in the input
++countDigit;
sum += readRightMost;
product *= readRightMost;

if (readRightMost % 2 == 0){ // count how many even digits are in the input
++countEven;
}
else { // count how many odd digits are in the input
++countOdd;
}
if (readRightMost == 0) { // count how many zero digits are in the input
++countZero;
}
}
System.out.println("d) It has " + countDigit + " digit(s)");
System.out.println("e) It has " + countOdd + " odd digit(s)");
System.out.println("f) It has " + countEven + " even digit(s)");
System.out.println("g) It has " + countZero + " zero digit(s)");
System.out.println("h) The sum of the digits in it is " + sum);
System.out.println("i) The product of the digits in it is " + product);

if (integer < reverse) { // if the reverse value of an int is greater than its original value
System.out.println("j) It is smaller than its reverse value: " + reverse);
}
else { // if the reverse value of an int is lesser than its original value
System.out.println("j) It is bigger than its reverse value: " + reverse);
}

System.out.println("================================================================");
}

}

最佳答案

在你的 while(integer != 0) 的末尾循环,你知道integer必须为 0,并且在 if (integer < reverse) 之前您永远不会再更改它,所以也可能是 if (0 < reverse) ,它的行为正是您所看到的。要修复此问题,请让您的循环在与稍后测试的变量不同的变量上运行。

关于java - 程序未正确读取 if/else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53000510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com