Using if and if/else

advertisement
Comp313
Java Programming
Using if and if/else structures
Example code
A single if structure
import javax.swing.JOptionPane;
public class DividingNumbers
{
public static void main(String args[])
{
String userInput;
double numberOne, numberTwo;
userInput = JOptionPane.showInputDialog("Enter a number");
numberOne = Double.parseDouble(userInput);
userInput = JOptionPane.showInputDialog("Enter number to divide by:");
numberTwo = Double.parseDouble(userInput);
if(numberTwo == 0)
{
JOptionPane.showMessageDialog(null,
"You cannot divide by zero", "Program Termination",
JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
JOptionPane.showMessageDialog(null, numberOne + " divided by "
+ numberTwo + " is " + numberOne/numberTwo,
"Results of division", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
Using if and if/else structures
Page 1 of 8
Comp313
Java Programming
Using if and if/else structures
Example code
Another program using single if structure
import javax.swing.JOptionPane;
public class IDCheck
{
public static void main(String args[])
{
String userInput;
int age;
userInput = JOptionPane.showInputDialog("How old are you?");
age = Integer.parseInt(userInput);
if(age < 18)
{
JOptionPane.showMessageDialog(null,
"You are under age", "No Admittance",
JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
JOptionPane.showMessageDialog(null, "You are allowed in",
"Age Check", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
Using if and if/else structures
Page 2 of 8
Comp313
Java Programming
Using if and if/else structures
Example code
Using a couple simple if structures
import javax.swing.JOptionPane;
public class EvenOddExample
{
public static void main(String args[])
{
String userInput, outputMessage;
int numberOne;
userInput = JOptionPane.showInputDialog("Enter a whole number");
numberOne = Integer.parseInt(userInput);
outputMessage = "";
//initializing String variable
if(numberOne % 2 = = 0)
{
outputMessage = numberOne + " is an even number";
}
if(numberOne % 2 = = 1)
{
outputMessage = numberOne + " is an odd number";
}
JOptionPane.showMessageDialog(null, outputMessage,
"Even or Odd?", JOptionPane.QUESTION_MESSAGE);
System.exit(0);
}
}
Using if and if/else structures
Page 3 of 8
Comp313
Java Programming
Using if and if/else structures
Example code
Previous program but using if/else structure
import javax.swing.JOptionPane;
public class EvenOddExample2
{
public static void main(String args[])
{
String userInput, outputMessage;
int numberOne;
userInput = JOptionPane.showInputDialog("Enter a whole number");
numberOne = Integer.parseInt(userInput);
outputMessage="";
if(numberOne % 2 = = 0)
{
outputMessage = numberOne + " is an even number";
}
else
{
outputMessage = numberOne + " is an odd number";
}
JOptionPane.showMessageDialog(null, outputMessage,
"Even or Odd?", JOptionPane.QUESTION_MESSAGE);
System.exit(0);
}
}
Using if and if/else structures
Page 4 of 8
Comp313
Java Programming
Using if and if/else structures
Example code
Another example using if/else structure
import javax.swing.JOptionPane;
public class MoneyWithdrawal
{
public static void main(String args[])
{
String whatUserEntered, approvalMessage = "";
double bankBalance, amountOfWithdrawal;
whatUserEntered = JOptionPane.showInputDialog("Enter your bank balance");
bankBalance = Double.parseDouble(whatUserEntered);
whatUserEntered = JOptionPane.showInputDialog("Enter withdrawal amount");
amountOfWithdrawal = Double.parseDouble(whatUserEntered);
if(amountOfWithdrawal < bankBalance)
{
JOptionPane.showMessageDialog(null, "Withdrawal approved\n"
+ "Your new balance is now $"
+ (bankBalance-amountOfWithdrawal),
"Approved", JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "Insufficient Funds",
"Withdrawal Denied", JOptionPane.WARNING_MESSAGE);
}
System.exit(0);
}
}
Using if and if/else structures
Page 5 of 8
Comp313
Java Programming
Using if and if/else structures
Example code
Example before using nested if/else structure
import javax.swing.JOptionPane;
public class SpeedLimit
{
public static void main(String args[])
{
String textEntered, response, userName;
int carSpeed, postedSpeed;
userName = JOptionPane.showInputDialog("What is your name?");
textEntered = JOptionPane.showInputDialog("Enter Speed Limit");
postedSpeed = Integer.parseInt(textEntered);
textEntered = JOptionPane.showInputDialog("Enter your speed");
carSpeed = Integer.parseInt(textEntered);
response = userName + ", ";
if(carSpeed > postedSpeed)
{
response = response + "you are " + (carSpeed-postedSpeed) +
" MPH over the limit";
}
if(carSpeed = = postedSpeed)
{
response = response + "you are at the correct speed";
}
if(carSpeed < postedSpeed)
{
response = response + "you are " + (postedSpeed-carSpeed) +
" MPH under the speed limit";
}
JOptionPane.showMessageDialog(null, response,
"Driving Observation", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
Using if and if/else structures
Page 6 of 8
Comp313
Java Programming
Using if and if/else structures
Example code
Previous program written using nested if/else structures
import javax.swing.JOptionPane;
public class SpeedLimit2
{
public static void main(String args[])
{
String textEntered, response, userName;
int carSpeed, postedSpeed;
userName = JOptionPane.showInputDialog("What is your name?");
textEntered = JOptionPane.showInputDialog("Enter Speed Limit");
postedSpeed = Integer.parseInt(textEntered);
textEntered = JOptionPane.showInputDialog("Enter your speed");
carSpeed = Integer.parseInt(textEntered);
response = userName + ", ";
if(carSpeed > postedSpeed)
{
response = response + "you are " + (carSpeed-postedSpeed) +
" MPH over the limit";
}
else
{
if(carSpeed == postedSpeed)
{
response = response + "you are at the correct speed";
}
else
{
response = response + "you are " + (postedSpeed-carSpeed) +
" MPH under the speed limit";
}
}
JOptionPane.showMessageDialog(null, response,
"Driving Observation", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
Using if and if/else structures
Page 7 of 8
Comp313
Java Programming
Using if and if/else structures
Example code
A puzzler
import javax.swing.JOptionPane;
public class WeatherAdvisor
{
public static void main(String args[])
{
String inputFromUser, message="";
int temperature;
inputFromUser = JOptionPane.showInputDialog("What is the temperature?");
temperature = Integer.parseInt(inputFromUser);
if(temperature > 32)
if(temperature > 60)
message = "Sounds like a nice day";
else
message = "You may need a jacket today";
else
if(temperature < 0)
message = "Stay inside! It's bitter cold outside";
else
message = "If you go outside, bundle up!";
JOptionPane.showMessageDialog(null, message);
System.exit(0);
}
}
What is the message if the user enters 45?
What is the message if the user enters 18?
What is the message if the user enters 32? (careful!)
Using if and if/else structures
Page 8 of 8
Download