타이어 압력 중 하나가 35 또는 45 이상이면 시스템이 "나쁜 인플레이션"을 출력하도록 부울 테스트를하려고합니다.Java에서 내 부울 테스트가 항상 실패하는 이유는 무엇입니까?
내 수업에서는 내가 시도한 부울을 사용해야합니다. 그러나 반환 된 부울은 항상 참입니다. 나는 왜 그런지 이해하지 못한다.
public class tirePressure
{
private static double getDoubleSystem1() //Private routine to simply read a double in from the command line
{
String myInput1 = null; //Store the string that is read form the command line
double numInput1 = 0; //Used to store the converted string into an double
BufferedReader mySystem; //Buffer to store input
mySystem = new BufferedReader (new InputStreamReader (System.in)); // creates a connection to system files or cmd
try
{
myInput1 = mySystem.readLine(); //reads in data from console
myInput1 = myInput1.trim(); //trim command cuts off unneccesary inputs
}
catch (IOException e) //checks for errors
{
System.out.println ("IOException: " + e);
return -1;
}
numInput1 = Double.parseDouble (myInput1); //converts the string to an double
return numInput1; //return double value to main program
}
static public void main (String[] args)
{
double TireFR; //double to store input from console
double TireFL;
double TireBR;
double TireBL;
boolean goodPressure;
goodPressure = false;
System.out.println ("Tire Pressure Checker");
System.out.println (" ");
System.out.print ("Enter pressure of front left tire:");
TireFL = getDoubleSystem1(); //read in an double from the user
if (TireFL < 35 || TireFL > 45)
{
System.out.println ("Pressure out of range");
goodPressure = false;
}
System.out.print ("Enter pressure of front right tire:");
TireFR = getDoubleSystem1(); //read in an double from the user
if (TireFR < 35 || TireFR > 45)
{
System.out.println ("Pressure out of range");
goodPressure = false;
}
if (TireFL == TireFR)
System.out.print (" ");
else
System.out.println ("Front tire pressures do not match");
System.out.println (" ");
System.out.print ("Enter pressure of back left tire:");
TireBL = getDoubleSystem1(); //read in an double from the user
if (TireBL < 35 || TireBL > 45)
{
System.out.println ("Pressure out of range");
goodPressure = false;
}
System.out.print ("Enter pressure of back right tire:");
TireBR = getDoubleSystem1(); //read in an double from the user
if (TireBR < 35 || TireBR > 45)
{
System.out.println ("Pressure out of range");
goodPressure = false;
}
if (TireBL == TireBR)
System.out.print (" ");
else
System.out.println ("Back tire pressures do not match");
if (goodPressure = true)
System.out.println ("Inflation is OK.");
else
System.out.println ("Inflation is BAD.");
System.out.println (goodPressure);
} //mainmethod
} // tirePressure Class
코드가 명확하므로 주석이 필요하지 않은만큼 똑같은 정보를 반복하므로 (좋은 것입니다!).나중에 참조 할 때 기억해야 할 가장 중요한 점은 무엇을하고 있는지, 왜 무엇을하고 있는지를 설명하는 것입니다. : D –
'java.util.Scanner'를 살펴볼 수도 있습니다. – polygenelubricants