Im getting "Variable rr may not have been initialized" with it pointing at one of my if statements at the very bottom.
This was the problem:Code:import java.util.Scanner; class TirePressure2 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int rf; //right front int lf; //left front int rr; //right rear int lr; //left rear int gi; //interger for good inflation. Either 0 or 1 int gf; //good front inflation int gr; //good rear inflation System.out.println("Enter right front tire pressure:"); rf = scan.nextInt(); if (rf > 45 || rf < 35) { gi = 0; System.out.println("Bad right front tire pressure"); System.out.println("Enter left front tire pressure:"); lf = scan.nextInt(); if (lf > 45 || lf < 35) { gi = 0; System.out.println("Bad left front tire pressure"); System.out.println("Enter right rear tire pressure:"); rr = scan.nextInt(); if (rr > 45 || rr < 35) { gi = 0; System.out.println("Bad right rear tire pressure"); System.out.println("Enter left rear tire pressure:"); lr = scan.nextInt(); if (lr > 45 || lr < 35) { gi = 0; System.out.println("Bad left rear tire pressure"); } } } } else { gi = 1; System.out.println("Good right front tire pressure"); System.out.println("Enter left front tire pressure:"); lf = scan.nextInt(); if (lf > 45 || lf < 35) { gi = 0; System.out.println("Bad left front tire pressure"); System.out.println("Enter right rear tire pressure:"); rr = scan.nextInt(); if (rr > 45 || rr < 35) { gi = 0; System.out.println("Bad right rear tire pressure"); System.out.println("Enter left rear tire pressure:"); lr = scan.nextInt(); if (lr > 45 || lr < 35) { gi = 0; System.out.println("Bad left rear tire pressure"); } } } else { gi = 1; System.out.println("Good left front tire pressure"); System.out.println("Enter right rear tire pressure:"); rr = scan.nextInt(); if (rr > 45 || rr < 35) { gi = 0; System.out.println("Bad right rear tire pressure"); System.out.println("Enter left rear tire pressure:"); lr = scan.nextInt(); if (lr > 45 || lr < 35) { gi = 0; System.out.println("Bad left rear tire pressure"); } } else { gi = 1; System.out.println("Good right rear tire pressure"); System.out.println("Enter left rear tire pressure:"); lr = scan.nextInt(); if (lr > 45 || lr < 35) { gi = 0; System.out.println("Bad left rear tire pressure"); } else { gi = 1; System.out.println("Good left rear tire pressure"); } } } } if (rf == lf) { gf = 1; } else { gf = 0; } if (rr == lr) { gr = 1; } else { gr = 0; } if (gi == 0) { System.out.println("Bad inflation!"); } else { if (gf == 1) { if (gr == 1) { System.out.println("Good inflation!"); } else { System.out.println("Check rear tire pressures, not equal!"); } } else { System.out.println("Check front tire pressures, not equal!"); } } } }
And this was exercise one, if you need it:Its not enough that the pressures are the same in the tires, but the pressures must also be within range. Modify the program in exercise 1 so that it also checks that each tire has a pressure between 35 and 45. If a tire is out of range, write out an error message immediately, but continue inputting values and processing them:
Input right front pressure
32
Warning: pressure is out of range
Input left front pressure
32
Warning: pressure is out of range
Input right rear pressure
42
Input left rear pressure
42
Inflation is BAD
If there have been any warnings, write out a final error message. (To do this, declare a boolean variable goodPressure that is initialized to true but is changed to false when an out of range tire is first found.)
I feel like I'm making this problem way longer and way more complicated than it needs to be, any tips?The front tires of a car should both have the same pressure. Also, the rear tires of a car should both have the same pressure (but not neccessarily the same pressure as the front tires.) Write a program that reads in the pressure of the four tires and writes a message that says if the inflation is OK or not.
Input right front pressure
38
Input left front pressure
38
Input right rear pressure
42
Input left rear pressure
42
Inflation is OK
Results 1 to 16 of 16
Thread: One last java question
Threaded View
- 25 Nov. 2012 07:17pm #1
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 0.00
One last java question