-2
これは私のコードですが、私は無料修正を求めていません。私は助けてくれる人を探しています。前もって感謝します!If-Else Statementが正しく実行されていません
* 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");
}
}
私の選択肢のための乱数発生方法が機能していません。このコードで選択2
'int型の選択= '0'に続くようにのみ1及び2表示されている場合(選択= = 1) '...まあ、それは決して真実ではないでしょう...' else if(choice == 2) 'の後ろに...いいえ、決して真実でもありません。 – Andreas
選択を初期化しないと、if else文に初期化されていないというコンパイラエラーがあります。 – user6819875
その値をテストする前に、基本的に 'choice'が入力から割り当てられていません。 –