2012-10-29 54 views
0

我目前正在嘗試創建一個bmi計算器,我堅持試圖讓腳和英寸的人身高進入一個英寸值,因爲我不知道如何捕捉兩個不同的值(英尺和英寸)然後驗證它們。使用方法,我不能得到它來讀取兩個不同的輸入

公共類BMI {

public static void main (String[] args) 
{ 
    System.out.println("Value between 2 and 7: " + heightInInches(2,7)); 
    System.out.println("Value between 0 and 11: " + heightInInches(0,11)); 

    System.out.println("Value between 3 and 30: " + weightInPounds(3,30)); 
    System.out.println("Value between 0 and 13: " + weightInPounds(0,13)); 
} 

public static int heightInInches(int lower, int upper) 
{ 
    Scanner kybd = new Scanner(System.in); 
    System.out.printf("Enter your height between %d and %d: ", lower, upper); 
    int height = kybd.nextInt(); 

    while (height < lower || height > upper) 
    { 
     System.out.printf("Retry between %d and %d:" , lower, upper); 
     height = kybd.nextInt(); 
    }  

    return height; 

} 

回答

0

下面是一個簡單的BMI計算器,你可以通過修改自己的條件玩耍。

static int heightInInches(int f,int i){ 
      int in=f*12; 
      int inches=i+in; 
      return inches; 
     } 
     static int weightInPounds(int stone, int p){ 
      int po=stone*14; 
      int pounds=p+po; 
      return pounds; 
     } 
     static int calculateBMI(int height,int weight){ 
      int w=weight*703; 
      int h=height*height; 
      int bmi = w/h; 
      return bmi; 
     } 
     public static void main(String[] args){ 
      Scanner input=new Scanner(System.in); 
      System.out.print("Enter height in feet (2-7): "); 
      int feet=input.nextInt(); 
      while(!(feet>= 2 && feet<=7)) 
      { 
       System.out.println("Retry between 2 and 7 :"); 
       feet=input.nextInt(); 
      } 
      System.out.print("Enter height in inch(0-11): "); 
      int inch=input.nextInt(); 
      while(!(inch>= 0 && inch<=11)) 
      { 
       System.out.println("Retry between 0 and 11 :"); 
       inch=input.nextInt(); 
      } 
      System.out.print("Enter weight in stone: "); 
      int stone=input.nextInt(); 
      System.out.print("Enter weight in pound: "); 
      int pound=input.nextInt(); 
      int height=heightInInches(feet,inch); 
      int weight=weightInPounds(stone,pound); 
      int bmi=calculateBMI(height,weight); 
      System.out.println("BMI is: "+bmi); 
     } 
1

這並不期待權while (height < inches || height > inches)

逸岸,整個heightInInches()必須修改。您通過顯示英尺和英寸來接受用戶輸入的方式不正確。

+0

我知道這一點是錯誤的,因爲我需要驗證身高,但這樣做,我不能分開腳或英寸? –

+0

你的意思是不能分開腳或英寸?首先,你沒有一個明確的下限或上限爲英尺或英寸......要求用戶輸入一個值之間的值? – agirish

+0

我剛剛糾正,因爲我一直在玩,並把它回到它是如何它是 –

0

如果你想保持你的代碼,我wouldnt推薦。保持while循環掃描儀這樣

while(yourcond){ 
    kybd = new Scanner(System.in);//take the input again 
    height = kybd.nextInt(); 
} 
0

願你要查詢的腳用戶,其存儲在一個成員變量,然後查詢爲英寸,做你的計算。例如:

修改主要類別:

package feetinch; 

    public class FeetInch { 

    public static void main(String[] args) { 
     HeightQuery heightQuery = new HeightQuery(); 

     heightQuery.queryForFeet(); 
     heightQuery.queryForInches(); 

     System.out.println("Result=" + heightQuery.getHeight()); 


    } 
} 

新類:

package feetinch; 

import java.util.Scanner; 

public class HeightQuery { 
    private int feet, inches; 

    public void queryForFeet() { 
     Scanner kybd = new Scanner(System.in); 
     System.out.printf("Enter your height in feet: "); 
     feet = kybd.nextInt(); 
    } 

    public void queryForInches() { 
     Scanner kybd = new Scanner(System.in); 
     System.out.printf("Enter your height in inches: "); 
     inches = kybd.nextInt(); 
    } 

    public int getHeight() { 
     return (feet * 12) + inches; 
    } 

} 
相關問題