If-Else语句未正确执行

时间:2016-10-06 01:16:23

标签: java

这是我的代码,顺便说一句,我不是要求免费解决方案,我正在找人帮助我并解释我的错误。提前谢谢!

    * Psuedocode: 
 * 1. Ask the user if they want to enter a number or if they want the computer to select a random number. 
 * 2. Based on the user selection, Ask the user for a number or generate a random number. 
 * 3. Read the number from the user (Skip the step if a random number is generated)
 * 4. Check if the number is 1 digit or 2 digit or 3 digit or 4 digit
 * 5. if it is 1 digit then check if the cube of number equals the number
 * 6. else if it is 2 digit, get the cube of first and second digits and then sum them up and check if the number is equal to the sum.
 * 7. else if it is 3 digit, get the cube of first, second and third digits and then sum them up andcheck if the number is equal to the sum.
 * 8. if it is 4 digit, get the cube of first, second, third and fourth digits and then sum them up andcheck if the number is equal to the sum.
 * 9. else tell the user that the number they have entered is not within 9999
 * 10. Print a closing message saying if the number is an Armstrong number or not. 
 * 11. Print a goodbye statement.

* /

   import java.util.Scanner;
   import java.util.Random;

    public class ArmstrongNumber {

       public static boolean Armstrong(int input) {
           String inputString = input + "";
       int numberOfDigits = inputString.length();
       int copyOfInput = input;
       int sum = 0;

       while (copyOfInput != 0) {
           int lastDigit = copyOfInput % 10;
           sum = sum + (int) Math.pow(lastDigit, numberOfDigits);
           copyOfInput = copyOfInput / 10;
       }
       if (sum == input) {
           return true;
       } else {
           return false;
       }
   }

       public static void main(String[] args) {
           Scanner scanner = new Scanner(System.in);

           System.out.print("Enter a number to check if it is an Armstrong number or generate a random number: ");
       int inputNumber = scanner.nextInt();
       int choice = 0;
       Random rand = new Random();
       if(choice == 1)
       {
                     inputNumber = scanner.nextInt();
       }
       else if(choice == 2)
       {
                     inputNumber =  rand.nextInt(9999) + 1; ;//generate a random number between 1 and 9999
       }





       boolean result = Armstrong(inputNumber);
       if (result) {
           System.out.println("");
           System.out.println(inputNumber + " is an Armstrong number");
       } else {
           System.out.println("");
           System.out.println(inputNumber + " is not an Armstrong number");

       }
       System.out.println("");
       System.out.println("Thanks for using my code. Goodbye");
   }
}

我选择的随机生成器方法不起作用。它只显示1和2作为阿姆斯特朗号或非阿姆斯特朗号,但没有为选择2生成随机数

1 个答案:

答案 0 :(得分:0)

在此代码中

   int choice = 0;
   Random rand = new Random();
   // you need to assign something to choice before the next line
   // maybe choice = rand.nextInt (); ?
   if(choice == 1)