允许用户在JAVA中输入非标准字符,例如“或”

时间:2018-02-07 21:24:27

标签: java input

好的,所以我不知道我错过了什么,而且我已经浏览了我的材料并用Google搜索了几个小时。我要做的是允许用户使用整数和相应的“和”输入高度和英寸。

感谢您对此的帮助:

 int height, weight;
 double bmi;

 Scanner keyboard = new Scanner(System.in);

 System.out.print("What is your height in ft/in. You can say for" +
                 " example 5'9\" :" );
 height = keyboard.nextInt();

 System.out.print("What is your weight in lb :");

 weight = keyboard.nextInt();

 bmi = weight *703 / (height*height);

 System.out.println("Your bmi is : " + bmi);

1 个答案:

答案 0 :(得分:0)

您需要创建自己的高度类,如:

class Height {
   private int feet;
   private int inches;

   public Height(int feet, int inches) {
      this.feet = feet;
      this.inches = inches;
   }

   public void setfeet(int feet)
   {
      this.feet = feet;
   }
   public void setinches(int inches)
   {
      this.inches= inches;
   }
   public int getfeet()
   {
      return this.feet;
   }
   public int getinches()
   {
      return this.inches;
   }

}

然后在你的主要课程中,你可以这样做:

System.out.print("What is your height -> feet: );
feet = keyboard.nextInt();

System.out.print("What is your height -> inches: );
inches= keyboard.nextInt();

Height height = new Height(feet,inches);