0

InputMismatchException がスローされると、無限ループに入り、その理由を一生理解できません。基本的に、このプログラムの主な目的は、ユーザーが入力した負の数に対して例外をスローし、ユーザーが実際に整数 (「r45」のようなものではない) を入力することを確認することです。どんな助けでも大歓迎です。ありがとうございました。

  import java.util.*;

  public class conversion{
  static Scanner in = new Scanner (System.in);
  static final double centimeters_per_inch = 2.54;
  static final int inches_per_foot = 12;

  public static void main (String [] args){
  int feet;
  int inches;
  int totalInches;
  double centimeters;
  boolean done = false;
  do{
    try
       {
       System.out.print("Enter feet: ");
       System.out.flush();
       feet = in.nextInt();
       System.out.println();
       System.out.print("Enter inches: ");
       System.out.flush();
       inches = in.nextInt();

       if (feet < 0 || inches < 0)
         throw new NonNegative();

       System.out.println();

       done = true;
       System.out.println("The numbers you entered are " + feet +" feet and " + inches+ " inches");
       totalInches = inches_per_foot * feet + inches;
       System.out.println();
       System.out.println("The total number of inches = " + totalInches);
       centimeters = totalInches * centimeters_per_inch;
       System.out.println("The number of centimeteres = " + centimeters); 
     }

     catch (NonNegative a){
       System.out.println(a.toString());   
     }
     catch(InputMismatchException e) {
       System.out.println("This is not a number");
     }
   }while(!done);
 }

}

4

2 に答える 2