如果程序未正确读取if / else语句

时间:2018-10-26 02:17:12

标签: java if-statement

这是一个程序,列出了有关从Scanner对象输入的整数的几个事实。但是,最后的if / else语句有点麻烦。

问题是,如果输入是非0的正整数,则程序将始终读取以下语句:System.out.println("j) It is smaller than its reverse value: " + reverse);。如果它是负整数,则始终会打印System.out.println("j) It is bigger than its reverse value: " + reverse);

我认为是因为reverseint reverse = 0;循环之前声明了,所以存储在while中的数据为0。但是,该程序会正确打印输入的反面。

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("================================================================");
    }

}

1 个答案:

答案 0 :(得分:2)

while(integer != 0)循环结束时,您知道integer必须为0,并且再也不会在if (integer < reverse)之前更改它,因此也可能是{{1} },它具有您所看到的行为。要解决该问题,请让您的循环对一个变量进行操作,然后再进行测试。