2014-06-07 155 views
-2

我只想知道如何刪除初始化的wgt錯誤。休息,我希望我的代碼是基本的,非常簡單。BMI計算器。怎麼了?

//Program By Aryansh Malviya 
//BMI Calculator 
import java.util.Scanner; 

public class BMICalc 
{ 
    public static void main(String args[]) 
    { 
     Scanner input = new Scanner(System.in); 

     int cw; 
     int ch; 
     int bmi; 
     int wgt; 
     int hgt; 
     int ct; 

     System.out.print("\nNote: This program is not yet fully functional.\nThere might be issues regarding decimal values. \nProgram has not been equipped to accept or display in decimal values. \nThe program will be updated soon. \nSorry for the inconvinience."); 
     System.out.print("Enter 1 if you want weight in Kilograms"); 
     System.out.print("Enter 2 if you want weight in Pounds"); 
     cw = input.nextInt(); 

     System.out.print("\nNote: If you choose to enter weight in Pounds, you'd have to enter height in Inches. Else, in meters"); 
     if(cw == 1) 
     { 
      System.out.print("\nEnter weight in Kilograms: "); 
      wgt = input.nextInt(); 
     } 

     else if(cw == 2) 
     { 
      System.out.print("\nEnter weight in Pounds: "); 
      wgt = input.nextInt(); 
     } 

     else 
     { 
      System.out.print("\nEnter a valid choice and try again!"); 
      } 

     System.out.print("\nEnter your height: "); 
     hgt = input.nextInt(); 

    bmi = wgt/(hgt * hgt); 

    System.out.printf("\nYour weight is: %d", wgt); 
    System.out.printf("\nYour height is: %d", hgt); 
    System.out.printf("\n\nYour BMI is: %d", bmi); 

    System.out.print("\nBMI VALUES"); 
    System.out.print("\nUnderweight: less than 18.5"); 
    System.out.print("\nNormal: between 18.5 and 24.9"); 
    System.out.print("\nOverweight: between 25 and 29.9"); 
    System.out.print("\nObese: 30 or greater"); 
} 
} 

當我編譯程序時,我得到的錯誤是變量wgt沒有被初始化。請告訴我如何解決這個問題。

+2

本地變量需要初始化,如錯誤消息所示。將它們初始化爲0. –

+0

可能的重複[爲什麼局部變量未在Java中初始化?](http://stackoverflow.com/questions/415687/why-are-local-variables-not-initialized-in-java) – Niemand

回答

0

你有兩個if語句 - 但如果用戶既不進入1也不2您在您嘗試使用wgt而沒有對其進行初始化代碼的一部分結束。

你應該做兩兩件事:

首先,初始化wgt當你第一次宣佈它(使編譯器錯誤消失)。

int wgt=0; 

會做到這一點。

接下來,請確保您的程序在cw既不是1也不是2時陷阱。您可以使用帶有標誌或其他機制的while循環。眼下,爲cw輸入錯誤只會繼續去(您打印「輸入一個有效的選擇」,但實際上不回去的開始......)

這裏是你如何解決一切:

import java.util.Scanner; 

public class BMICalc 
{ 
    public static void main(String args[]) 
    { 
     Scanner input = new Scanner(System.in); 

     int cw=0; 
     int ch; 
     double bmi; 
     double wgt=0; 
     double hgt; 
     int ct; 
     double BMIinchPoundFactor = 703; 

     System.out.print("\nNote: This program is not yet fully functional.\nThere might be issues regarding decimal values. \nProgram has not been equipped to accept or display in decimal values. \nThe program will be updated soon. \nSorry for the inconvinience."); 

     System.out.print("\nNote: If you choose to enter weight in Pounds, you'd have to enter height in Inches. Else, in meters"); 
     boolean validInput = false; 
     while (!validInput) { 
      System.out.print("\nEnter 1 if you want weight in Kilograms"); 
      System.out.print("\nEnter 2 if you want weight in Pounds\n"); 
      cw = input.nextInt(); 
      switch(cw) { 
      case 1: 
      System.out.print("\nEnter weight in Kilograms: "); 
      wgt = input.nextDouble(); 
      validInput=true; 
      break; 
      case 2: 
      System.out.print("\nEnter weight in Pounds: "); 
      wgt = input.nextDouble(); 
      validInput=true; 
      break; 
      default: 
      System.out.print("\nEnter a valid choice and try again!"); 
      } 
     } 
     System.out.print("\nEnter your height: "); 
     hgt = input.nextDouble(); 

     bmi = wgt/(hgt * hgt); 

     if(cw==2) bmi *= BMIinchPoundFactor; 

     System.out.printf("\nYour weight is: %.0f", wgt); 
     System.out.printf("\nYour height is: %.2f", hgt); 
     System.out.printf("\n\nYour BMI is: %.1f", bmi); 

     System.out.print("\nBMI VALUES"); 
     System.out.print("\nUnderweight: less than 18.5"); 
     System.out.print("\nNormal: between 18.5 and 24.9"); 
     System.out.print("\nOverweight: between 25 and 29.9"); 
     System.out.print("\nObese: 30 or greater\n\n"); 
    } 
} 

我拍了幾張其他自由:

  1. 使用double的東西可能不是整數
  2. 清理I/O(加入一些'\n'
  3. 創建一個循環,一直走,直到你給出一個有效的輸入
  4. 正確計算BMI文你給英鎊和英寸

我敢肯定,這可以進一步提高輸入...

0
bmi = wgt/(hgt * hgt); 

您在這裏使用wgt並有可能無法初始化如果cw既不是1也不是2

0

沒有保證WGT已初始化。例如,用戶可以在第一個問題中輸入'3'。你應該考慮和處理非法輸入。