2017-05-13 10 views
0
 //create scanner class 
     Scanner iKeyboard = new Scanner (System.in); 
     Scanner dKeyboard = new Scanner (System.in); 
     Scanner chKeyboard = new Scanner (System.in); 

     //assign variables 
     int iInches; 
     int iPounds; 
     double dMetres; 
     double dKilo; 
     double dMetric; 
     double dImp; 
     char chChoice; 
     char chI = 'I'; 
     char chM = 'M'; 

     //Ask the user to input M (metric) or I (imperial) 
     System.out.println("Would you like to enter your details using the metric (M) or imperial system (I)"); 
     System.out.println("Please enter M or I: "); 
     chChoice = chKeyboard.next().charAt(0);        //Initialised variable 

     //Ask the user to enter his weight and height but depending on it's choice of system 
     if (chChoice == chM) 
     { 
      System.out.println("Please enter your weight in kilograms: "); 
      dKilo = dKeyboard.nextDouble();         //Initialised variable 
      System.out.println("Please enter your height in metres: "); 
      dMetres = dKeyboard.nextDouble();        //Initialised variable 
     } 
     else if (chChoice == chI) 
     { 
      System.out.println("Please enter your weight in pounds: "); 
      iPounds = iKeyboard.nextInt();         //Initialised variable 
      System.out.println("Please enter your height in inches: "); 
      iInches = iKeyboard.nextInt();         //Initialised variable 
     } 
     else 
     { 
      System.out.println("You have entered an incorrect value"); 
      System.out.println("Please enter M or I: "); 
      chChoice = chKeyboard.next().charAt(0); 
      if (chChoice == chM) 
      { 
       System.out.println("Please enter your weight in kilograms: "); 
       dKilo = dKeyboard.nextDouble(); 
       System.out.println("Please enter your height in metres: "); 
       dMetres = dKeyboard.nextDouble(); 
      } 
      else if (chChoice == chI) 
      { 
       System.out.println("Please enter your weight in pounds: "); 
       iPounds = iKeyboard.nextInt(); 
       System.out.println("Please enter your height in inches: "); 
       iInches = iKeyboard.nextInt(); 
      } 
      else 
      { 
       System.out.println("You have entered an incorrect value"); 
       System.out.println("Goodbye!"); 
       chChoice = chKeyboard.next().charAt(0); 
      } 
     } 



     //Calculate the users BMI depending on users preferred system of measurement 
     if (chChoice == chM) 
     { 
      dMetric = dKilo/(dMetres * dMetres);   //error on dKilo and dMetres 
      System.out.println("Your BMI is: " + dMetric); 
     } 
     else 
     { 
      dImp = iPounds/(iInches * iInches) * 703;  //error on iPounds and iInches 
      System.out.println("Your BMI is: " + dImp); 
     } 

내 변수가 초기화되지 않았지만 키보드 등을 만들 때 가지고 있다고 말합니다. choice는 동일한 방법으로 initialised되었고 아무런 문제가 없습니다. chM을 제거하십시오. 누군가 제발 해결할 수 있습니까?로컬 변수 dMetres가 초기화되지 않았을 수 있습니다.

당신이 궁금하다면 여기에 작동하는 코드가 있습니다.

import java.util.Scanner; 
/** 
* 
* @author 
* @version 1 13/05/17 
* @aim to get the user to input their weight and height using their choice of measurement and then telling them their BMI and their category of weight for example obese 
*/ 
public class L04BMICalculator 
{ 



    public static void main(String[] args) 
    { 

     //create scanner classes 
     Scanner dKeyboard = new Scanner (System.in); 
     Scanner chKeyboard = new Scanner (System.in); 

     //assign variables 
     double dInches = 0; 
     double dPounds = 0; 
     double dMetres = 0; 
     double dKilo = 0; 
     double dMetric = 0; 
     double dImp = 0; 
     char chChoice; 
     char chI = 'I'; 
     char chM = 'M'; 

     //Ask the user to input M (metric) or I (imperial) 
     System.out.println("Would you like to enter your details using the metric (M) or imperial system (I)"); 
     System.out.println("Please enter M or I: "); 
     chChoice = chKeyboard.next().toUpperCase().charAt(0);        

     //Ask the user to enter his weight and height but depending on it's choice of system 
     if (chChoice == chM) 
     { 
      System.out.println("Please enter your weight in kilograms: "); 
      dKilo = dKeyboard.nextDouble();         
      System.out.println("Please enter your height in metres: "); 
      dMetres = dKeyboard.nextDouble();        
     } 
     else if (chChoice == chI) 
     { 
      System.out.println("Please enter your weight in pounds: "); 
      dPounds = dKeyboard.nextDouble();         
      System.out.println("Please enter your height in inches: "); 
      dInches = dKeyboard.nextDouble();         
     } 
     else 
     { 
      System.out.println("You have entered an incorrect value");  //If user entered something else this would come up 
      System.out.println("Please enter M or I: ");     //Asks the user to try again 
      chChoice = chKeyboard.next().charAt(0); 
      if (chChoice == chM) 
      { 
       System.out.println("Please enter your weight in kilograms: "); 
       dKilo = dKeyboard.nextDouble(); 
       System.out.println("Please enter your height in metres: "); 
       dMetres = dKeyboard.nextDouble(); 
      } 
      else if (chChoice == chI) 
      { 
       System.out.println("Please enter your weight in pounds: "); 
       dPounds = dKeyboard.nextDouble(); 
       System.out.println("Please enter your height in inches: "); 
       dInches = dKeyboard.nextDouble(); 
      } 
      else 
      { 
       System.out.println("You have entered an incorrect value"); //Says the same thing as before if they ignore previous instruction but.... 
       System.out.println("Goodbye!");        //Failure to oblige leads to termination 
       chChoice = chKeyboard.next().charAt(0); 
      } 
     }//end if 

     //Calculate the users BMI depending on users preferred system of measurement 
     if (chChoice == chM) 
     { 
      dMetric = dKilo/(dMetres * dMetres);   
      System.out.println("Your BMI is: " + dMetric); 
     } 
     else 
     { 
      dImp = dPounds/(dInches * dInches) * 703;  
      System.out.println("Your BMI is: " + dImp); 
     } 

     //Using the knowledge gained assign the user's BMI to a specific set for example, under weight 
     if (dMetric <= 18.5) 
     { 
      System.out.println("If ones BMI is 18.5 or less they are usually cosidered to be underweight"); 
     } 
     else if (dMetric > 18.5 && dMetric <= 24.99) 
     { 
      System.out.println("If ones BMI is between 18.6 and 24.99 they are usually cosidered to be of average weight (normal)"); 
     } 
     else if (dMetric > 24.99 && dMetric <= 29.99) 
     { 
      System.out.println("If ones BMI is between 25 and 29.99 they are usually cosidered to be overweight"); 
     } 
     else if (dMetric > 29.99 && dMetric <= 34.99) 
     { 
      System.out.println("If ones BMI is between 30 and 34.99 they are usually cosidered to be in obese class 1"); 
     } 
     else if (dMetric > 34.99 && dMetric <= 39.99) 
     { 
      System.out.println("If ones BMI is between 30 and 34.99 they are usually cosidered to be in obese class 2"); 
     } 
     else if (dMetric > 39.99) 
     { 
      System.out.println("If ones BMI is greater than 40 they are usually cosidered to be morbidly obese "); 
     }//end if 

     if (dImp <= 18.5) 
     { 
      System.out.println("If ones BMI is 18.5 or less they are usually cosidered to be underweight"); 
     } 
     else if (dImp > 18.5 && dImp <= 24.99) 
     { 
      System.out.println("If ones BMI is between 18.6 and 24.99 they are usually cosidered to be of average weight (normal)"); 
     } 
     else if (dImp > 24.99 && dImp <= 29.99) 
     { 
      System.out.println("If ones BMI is between 25 and 29.99 they are usually cosidered to be overweight"); 
     } 
     else if (dImp > 29.99 && dImp <= 34.99) 
     { 
      System.out.println("If ones BMI is between 30 and 34.99 they are usually cosidered to be in obese class 1"); 
     } 
     else if (dImp > 34.99 && dImp <= 39.99) 
     { 
      System.out.println("If ones BMI is between 30 and 34.99 they are usually cosidered to be in obese class 2"); 
     } 
     else if (dImp > 39.99) 
     { 
      System.out.println("If ones BMI is greater than 40 they are usually cosidered to be morbidly obese "); 
     }//end if 

     //Close keyboard 
     dKeyboard.close(); 
     chKeyboard.close(); 

    }//end main 

}//end class 

답변

0

두 번째 조건 검사 블록에는 dMetres 초기화가 없습니다. false이면 코드 끝에서 BMI를 계산할 때 초기화 된 변수가 없습니다. 따라서 eclipse는 모든 조건 블록에서 변수가 최소한의 기본값을 갖도록 초기화 할 것을 제안합니다.

변수를 널 또는 0과 같은 기본값으로 초기화하십시오.

import java.util.Scanner; 

public class BMICalc { 

    public static void main(String args[]){ 
     //create scanner class 
     Scanner iKeyboard = new Scanner (System.in); 
     Scanner dKeyboard = new Scanner (System.in); 
     Scanner chKeyboard = new Scanner (System.in); 

     //assign variables 
     int iInches = 0; 
     int iPounds = 0; 
     double dMetres = 0; 
     double dKilo = 0; 
     double dMetric; 
     double dImp; 
     char chChoice; 
     char chI = 'I'; 
     char chM = 'M'; 

     //Ask the user to input M (metric) or I (imperial) 
     System.out.println("Would you like to enter your details using the metric (M) or imperial system (I)"); 
     System.out.println("Please enter M or I: "); 
     chChoice = chKeyboard.next().charAt(0);        //Initialised variable 

     //Ask the user to enter his weight and height but depending on it's choice of system 
     if (chChoice == chM) 
     { 
      System.out.println("Please enter your weight in kilograms: "); 
      dKilo = dKeyboard.nextDouble();         //Initialised variable 
      System.out.println("Please enter your height in metres: "); 
      dMetres = dKeyboard.nextDouble();        //Initialised variable 
     } 
     else if (chChoice == chI) 
     { 
      System.out.println("Please enter your weight in pounds: "); 
      iPounds = iKeyboard.nextInt();         //Initialised variable 
      System.out.println("Please enter your height in inches: "); 
      iInches = iKeyboard.nextInt();         //Initialised variable 
     } 
     else 
     { 
      System.out.println("You have entered an incorrect value"); 
      System.out.println("Please enter M or I: "); 
      chChoice = chKeyboard.next().charAt(0); 
      if (chChoice == chM) 
      { 
       System.out.println("Please enter your weight in kilograms: "); 
       dKilo = dKeyboard.nextDouble(); 
       System.out.println("Please enter your height in metres: "); 
       dMetres = dKeyboard.nextDouble(); 
      } 
      else if (chChoice == chI) 
      { 
       System.out.println("Please enter your weight in pounds: "); 
       iPounds = iKeyboard.nextInt(); 
       System.out.println("Please enter your height in inches: "); 
       iInches = iKeyboard.nextInt(); 
      } 
      else 
      { 
       System.out.println("You have entered an incorrect value"); 
       System.out.println("Goodbye!"); 
       chChoice = chKeyboard.next().charAt(0); 
      } 
     } 



     //Calculate the users BMI depending on users preferred system of measurement 
     if (chChoice == chM) 
     { 
      dMetric = dKilo/(dMetres * dMetres);   //error on dKilo and dMetres 
      System.out.println("Your BMI is: " + dMetric); 
     } 
     else 
     { 
      dImp = iPounds/(iInches * iInches) * 703;  //error on iPounds and iInches 
      System.out.println("Your BMI is: " + dImp); 
     } 



    } 





} 
+0

어떻게해야이 문제를 해결하기 위해 다음과 같습니다

전체 프로그램입니다? –

+0

내 대답이 업데이트되었습니다. 전체 프로그램. –

+0

안녕하세요, 코드는 Metric에서만 작동합니다. 내가 파운드의 체중을 입력하십시오 : I의 출력은 0.0 –

관련 문제