Start New Discussion within our Software Development Community

i want the output detect the error because the condition is -20-20, 1st key in is ok but the second time no matter what number i have key in it will accept even i key in out of condition. please help me...

import java.util.Scanner;
import java.text.DecimalFormat;

public class CPI
{

public static void main (String[] args)
{
DecimalFormat dformat = new DecimalFormat("#0.0");

double CPI, expenses, inflationInPercentage;

Scanner input = new Scanner (System.in);


System.out.println("Please enter the value of CPI:");
CPI = input.nextDouble();
CPI = getCPI(CPI);

System.out.println("Please enter the value of expenses:");
expenses = input.nextDouble();

inflationInPercentage = inflation(CPI, expenses);

System.out.println("The value of inflation is:" + dformat.format(inflationInPercentage));
}

public static double getCPI(double CPI)
{
Scanner input = new Scanner(System.in);
if(CPI < -20.0)
{
System.out.println("the value is too low");

System.out.println("Enter the value in the range between -20 to 20");


}
else if(CPI>20.0)
{
System.out.println("the value is too high");

System.out.println("Enter the value of CPI in the range between -20 to 20");
CPI= input.nextDouble();
}
else if (CPI>= -20.0 && CPI<=20.0)
{

System.out.println("the value has wrong precision");

System.out.println("enter the value in correct precision in the range -20 to 20");

CPI=input.nextDouble();
}
else
CPI=CPI/10;

return CPI;
}

public static double inflation(double CPI,double expenses)
{
return(CPI*expenses);
}
}

This article has been dead for over six months. Start a new discussion instead.