Jump to content

Help with Java If Else conditions

SickSix66
Go to solution Solved by DioOmicida,

Why don't you change the code to this format:
IIF(CITY1 >= CITY2 && CITY1 <= CITY3)  || (CITY1 >=CITY3 && CITY1 <= CITY2)
PRINT CITY1
*CITY = TEMP

Hi in this Java program I have to enter 3 cities.

Display the temperature of the 3 cities & display the cities in the order from lowest temperature to the highest temperature.

So the final output has to be something like the image I've attached.

However I can't get the correct "Middle city" output. 

 

if((tempCelcius <= tempCelcius2) && (tempCelcius <= tempCelcius3))
		{
			System.out.println("Coldest city is " + city + " at " + tempCelcius + " degrees");
		}
		
		else if((tempCelcius2 <= tempCelcius3) && (tempCelcius2 <= tempCelcius))
		{
			System.out.println("Coldest city is " + city2 + " at " + tempCelcius2 + " degrees");
		}
		
		else if((tempCelcius3 <= tempCelcius2) && (tempCelcius3 <= tempCelcius))
		{
			System.out.println("Coldest city is " + city3 + " at " + tempCelcius3 + " degrees");
		}
		
		if((tempCelcius >= tempCelcius2) || (tempCelcius <= tempCelcius2) && (tempCelcius2 <= tempCelcius3) || (tempCelcius2 >= tempCelcius3))
		{
			System.out.println("Middle city is " + city + " at " + tempCelcius + " degrees");
		}
		
		//else if((tempCelcius2 >= tempCelcius) || (tempCelcius2 <= tempCelcius) && (tempCelcius2 <= tempCelcius3) || (tempCelcius2 >= tempCelcius3))
		//{
			//System.out.println("Middle city is " + city2 + " at " + tempCelcius2 + " degrees");
		//}
		
		//else 
		//{
			//System.out.println("Middle city is " + city2 + " at " + tempCelcius2 + " degrees");
		//}
		
		
		if((tempCelcius >= tempCelcius2) && (tempCelcius >= tempCelcius3))
		{
			System.out.println("Hottest city is " + city + " at " + tempCelcius + " degrees");
		}
		
		else if((tempCelcius2 >= tempCelcius3) && (tempCelcius2 >= tempCelcius))
		{
			System.out.println("Hottest city is " + city2 + " at " + tempCelcius2 + " degrees");
		}
		
		else if((tempCelcius3 >= tempCelcius2) && (tempCelcius3 >= tempCelcius))
		{
			System.out.println("Hottest city is " + city3 + " at " + tempCelcius3 + " degrees");
		}

 

Capture.JPG

Link to comment
Share on other sites

Link to post
Share on other sites

Why don't you change the code to this format:
IIF(CITY1 >= CITY2 && CITY1 <= CITY3)  || (CITY1 >=CITY3 && CITY1 <= CITY2)
PRINT CITY1
*CITY = TEMP

[CPU: 4.7ghz I5 6600k] [MBAsus Z170 Pro G] [RAM: G.Skill 2400 16GB(2x8)]

[GPU: MSI Twin Frozr GTX 970] [PSU: XFX Pro 850W] [Cooler: Hyper 212 Evo]
[Storage: 500GB WD HDD / 128GB SanDisk SSD ] [Case: DeepCool Tessaract]

[Keyboard: AZIO MGK1] [Mouse: Logitech G303] [Monitor: 2 x Acer 23" 1080p IPS]

 

Link to comment
Share on other sites

Link to post
Share on other sites

No offence but your code is a complete mess...

2 hours ago, SickSix66 said:

Hi in this Java program I have to enter 3 cities.

Display the temperature of the 3 cities & display the cities in the order from lowest temperature to the highest temperature.

  • You need to input temperature data for cities into your application.
  • You need store the data that has been entered.
  • You need to display the data that you have entered in a number of formats.

Think about this in terms of concerns and not spaghetti. You have an entity, City for which you need two things I am guessing; a name and a temperature. Simply make a class to describe this entity, otherwise known as a Plain Old Data (POD) class. This will allow you to store your entities in a generic list which you may then sort based on whatever property of the type it contains.

 

Pseudo code:

class City
{
	property string Name { get; }
	property double Temperature { get; }
}

List<City> someListOfCities

No need for all that mess of if else if else garbage... and on the plus side your application is much more extensible, robust, eloquent and readable.

 

Does this make sense to you?

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×