整数ではなく文字が入力されたときにこれらの行を出力する必要があります。これを行う方法がわかりません。この問題の解決を手伝っていただける場合は、コードと必要なものを以下に投稿してください。ありがとうございます。
This is what I am getting:
Input a valid whole number: abc
Input is not a valid number
Press any key to continue . . .
This is what I need:
Input a valid whole number: **ABC**
**ABC** is not a valid number
Press any key to continue . . .
below is what i have so far:
import java.io.PrintStream;
import java.util.Scanner;
public class FinalPractice
{
public static void main(String [] args)
{
Scanner scanner = new Scanner( System.in );
PrintStream out = System.out;
out.print( "Input a valid whole number: " );
String input = scanner.next();
int number;
try {
number = Integer.parseInt(input);
} catch (Exception e) {
out.println("Input is not a valid number");
return;
}
if (number < 0) {
out.println(number + " is not a valid number");
return;
}
printDivisors(number);
}
private static void printDivisors(int x){
PrintStream out = System.out;
for (int i=1; i<x; i++) {
if (isDivisibleBy(x, i)){
out.println(x + " is divisible by " + i);
} else {
out.println(x + " is not divisible by " + i);
}
}
}
private static Boolean isDivisibleBy(int x, int divisor){
while (x > 0) {
x -= divisor;
if (x == 0){
return true;
}
}
return false;
}
}