//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);
}
它表示我的变量尚未初始化,但是当我创建键盘等时,chChoice已经以相同的方式初始化并且如果我删除了chM则没有任何问题,有人可以解决这个问题吗?
如果你很好奇这是正常运作的代码。
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 :(得分:0)
在您的第二个条件检查区块中,没有初始化的dMetres。如果为false,则在您计算BMI时代码的末尾,没有初始化的变量。因此,eclipse建议您初始化,以便在每个条件块中,变量至少具有默认值。
将您的变量初始化为null或默认值,如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);
}
}
}