Skip to content

Instantly share code, notes, and snippets.

@bytecodeman
Last active September 27, 2021 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytecodeman/c8d1474d3e2aace015a047bbc0d76376 to your computer and use it in GitHub Desktop.
Save bytecodeman/c8d1474d3e2aace015a047bbc0d76376 to your computer and use it in GitHub Desktop.
CSC-111 If statement examples and sample code
import java.util.*;
public class DayOfDate {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter year: (e.g., 2008): ");
int year = input.nextInt();
System.out.print("Enter month: 1-12: ");
int month = input.nextInt();
if (month == 1)
{
month = 13;
year = year - 1; // January is counted as month 13 of the previous
// year.
}
else if (month == 2)
{
month = 14;
year = year - 1; // February is counted as month 14 of the previous
// year.
}
System.out.print("Enter the day of the month: 1-31: ");
int dayOfMonth = input.nextInt();
int j = year / 100;
int k = year % 100;
int dayOfWeek = (dayOfMonth + 26 * (month + 1) / 10 + k + k / 4 + j / 4 + 5 * j) % 7;
if (dayOfWeek == 0)
System.out.println("Day of the week is Saturday");
else if (dayOfWeek == 1)
System.out.println("Day of the week is Sunday");
else if (dayOfWeek == 2)
System.out.println("Day of the week is Monday");
else if (dayOfWeek == 3)
System.out.println("Day of the week is Tuesday");
else if (dayOfWeek == 4)
System.out.println("Day of the week is Wednesday");
else if (dayOfWeek == 5)
System.out.println("Day of the week is Thursday");
else if (dayOfWeek == 6)
System.out.println("Day of the week is Friday");
}
}
import java.util.Scanner;
public class NumberToMonth {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter number of month (1=January): ");
int month = input.nextInt();
input.close();
if (month == 1)
System.out.println("January");
if (month == 2)
System.out.println("February");
if (month == 3)
System.out.println("March");
if (month == 4)
System.out.println("April");
if (month == 5)
System.out.println("May");
if (month == 6)
System.out.println("June");
if (month == 7)
System.out.println("July");
if (month == 8)
System.out.println("August");
if (month == 9)
System.out.println("September");
if (month == 10)
System.out.println("October");
if (month == 11)
System.out.println("November");
if (month == 12)
System.out.println("December");
}
}
import java.util.Scanner;
public class NumberToMonth2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number of month (1=January): ");
int month = input.nextInt();
input.close();
String strMonth = "Illegal Month";
if (month == 1)
strMonth = "January";
if (month == 2)
strMonth = "February";
if (month == 3)
strMonth = "March";
if (month == 4)
strMonth = "April";
if (month == 5)
strMonth = "May";
if (month == 6)
strMonth = "June";
if (month == 7)
strMonth = "July";
if (month == 8)
strMonth = "August";
if (month == 9)
strMonth = "September";
if (month == 10)
strMonth = "October";
if (month == 11)
strMonth = "November";
if (month == 12)
strMonth = "December";
System.out.println("Month = " + strMonth);
}
}
import java.util.Scanner;
public class NumberToMonth3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number of month (1=January): ");
int month = input.nextInt();
input.close();
String strMonth;
int days;
if (month == 1) {
strMonth = "January";
days = 31;
}
else if (month == 2) {
strMonth = "February";
days = 28;
}
else if (month == 3) {
strMonth = "March";
days = 31;
}
else if (month == 4) {
strMonth = "April";
days = 30;
}
else if (month == 5) {
strMonth = "May";
days = 31;
}
else if (month == 6) {
strMonth = "June";
days = 30;
}
else if (month == 7) {
strMonth = "July";
days = 31;
}
else if (month == 8) {
strMonth = "August";
days = 31;
}
else if (month == 9) {
strMonth = "September";
days = 31;
}
else if (month == 10) {
strMonth = "October";
days = 31;
}
else if (month == 11) {
strMonth = "November";
days = 30;
}
else if (month == 12) {
strMonth = "December";
days = 31;
}
else {
strMonth = "Illegal Month";
days = 0;
}
if (days == 0) {
System.out.println(strMonth);
}
else {
System.out.printf("%s has %d days", strMonth, days);
}
}
}
import java.util.Scanner;
public class NumberToMonth4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number of month (1=January): ");
int month = input.nextInt();
input.close();
String strMonth;
if (month == 1)
strMonth = "January";
else if (month == 2)
strMonth = "February";
else if (month == 3)
strMonth = "March";
else if (month == 4)
strMonth = "April";
else if (month == 5)
strMonth = "May";
else if (month == 6)
strMonth = "June";
else if (month == 7)
strMonth = "July";
else if (month == 8)
strMonth = "August";
else if (month == 9)
strMonth = "September";
else if (month == 10)
strMonth = "October";
else if (month == 11)
strMonth = "November";
else if (month == 12)
strMonth = "December";
else
strMonth = "Illegal Month";
System.out.println("Month = " + strMonth);
}
}
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a three-digit integer: ");
int number = input.nextInt();
if (number >= 100 && number <= 999) { // && AND Both arguments must be TRUE
int lsd = number % 10;
int msd = number / 100;
if (lsd == msd)
System.out.println("Palindrome!!!");
else
System.out.println("NO Palindrome");
}
else {
System.out.println("Not a 3 Digit Integer.");
}
if (number < 100 || number > 999) // OR means one or the other must be TRUE
System.out.println("Not a 3 Digit Integer.");
else {
int lsd = number % 10;
int msd = number / 100;
if (lsd == msd)
System.out.println("Palindrome!!!");
else
System.out.println("NO Palindrome");
}
input.close();
}
}
public class PickACardWithIF {
public static void main(String[] args) {
int card = (int)(52 * Math.random());
int suit = card / 13;
int rank = card % 13;
String strRank;
if (rank == 0)
strRank = "Ace";
else if (rank >= 1 && rank <= 9)
strRank = "" + (rank + 1);
else if (rank == 10)
strRank = "Jack";
else if (rank == 11)
strRank = "Queen";
else
strRank = "King";
String strSuit;
if (suit == 0)
strSuit = "Spades";
else if (suit == 1)
strSuit = "Hearts";
else if (suit == 2)
strSuit = "Clubs";
else
strSuit = "Diamonds";
System.out.println(strRank + " of " + strSuit);
}
}
public class PickACardWithSwith {
public static void main(String[] args) {
int card = (int)(52 * Math.random());
int suit = card / 13;
int rank = card % 13;
String strRank = "";
switch (rank) {
case 0:
strRank = "Ace";
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
strRank = "" + (rank + 1);
break;
case 10:
strRank = "Jack";
break;
case 11:
strRank = "Queen";
break;
case 12:
strRank = "King";
break;
}
String strSuit = "";
switch (suit) {
case 0:
strSuit = "Spades";
break;
case 1:
strSuit = "Hearts";
break;
case 2:
strSuit = "Clubs";
break;
case 3:
strSuit = "Diamonds";
break;
}
System.out.println(strRank + " of " + strSuit);
}
}
import java.util.Scanner;
public class Quadratic {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a, b, c: ");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
if (a == 0) {
System.out.println("Sorry. a == 0 You don't have a quadratic.");
}
else {
double discriminant = b * b - 4 * a * c;
if (discriminant < 0) {
System.out.println("The equation has no real roots");
}
else if (discriminant == 0) {
double r = -b / (2 * a);
System.out.println("The equation has one root " + r);
}
else {
double r1 = (-b + Math.sqrt(discriminant) / (2 * a));
double r2 = (-b - Math.sqrt(discriminant) / (2 * a));
System.out.println("The equation has two roots " + r1 + " and " + r2);
}
}
input.close();
}
public static void mainxxx(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a, b, c: ");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double discriminant = b * b - 4 * a * c;
if (discriminant < 0)
{
System.out.println("The equation has no real roots");
}
else if (discriminant == 0)
{
double r1 = -b / (2 * a);
System.out.println("The equation has one root " + r1);
}
else
{ // (discriminant > 0)
double r1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);
double r2 = (-b - Math.pow(discriminant, 0.5)) / (2 * a);
System.out.println("The equation has two roots " + r1 + " and " + r2);
}
}
}
import java.util.Scanner;
public class RandomMonth {
public static void main(String[] args) {
int number = (int)(12 * Math.random()) + 1; // 1 .. 12
Scanner input = new Scanner(System.in);
System.out.println("Enter number: ");
number = input.nextInt();
switch (number) {
case 1:
case -1:
System.out.println("January");
break;
case 2:
case -2:
System.out.println("Feburary");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("Bad Month");
break;
}
/*
if (number == 1) {
System.out.println("January");
System.out.println("It's cold");
}
else if (number == 2) {
System.out.println("Feburary");
}
else if (number == 3)
System.out.println("March");
else if (number == 4)
System.out.println("April");
else if (number == 5)
System.out.println("May");
else if (number == 6)
System.out.println("June");
else if (number == 7)
System.out.println("July");
else if (number == 8)
System.out.println("August");
else if (number == 9)
System.out.println("September");
else if (number == 10)
System.out.println("October");
else if (number == 11)
System.out.println("November");
else if (number == 12)
System.out.println("December");
else
System.out.println("Bad Month");
*/
}
public static void mainxxx(String[] args) {
int number = (int) (Math.random() * 12) + 1;
// or int number = (int)(System.currentTimeMillis() % 12 + 1);
// or int number = (int)(Math.random() * 12) + 1;
}
}
import java.util.Scanner;
public class Sort3Numbers1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter 3 numbers: ");
int numb1 = input.nextInt();
int numb2 = input.nextInt();
int numb3 = input.nextInt();
if (numb1 > numb2) {
int temp = numb1;
numb1 = numb2;
numb2 = temp;
}
if (numb2 > numb3) {
int temp = numb2;
numb2 = numb3;
numb3 = temp;
}
if (numb1 > numb2) {
int temp = numb1;
numb1 = numb2;
numb2 = temp;
}
System.out.printf("Sorted numbers: %d %d %d\n", numb1, numb2, numb3);
}
}
import java.util.Scanner;
public class Sort3Numbers2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter 3 numbers: ");
int numb1 = input.nextInt();
int numb2 = input.nextInt();
int numb3 = input.nextInt();
if (numb1 > numb2) {
int temp = numb1;
numb1 = numb2;
numb2 = temp;
}
if (numb1 > numb3) {
int temp = numb1;
numb1 = numb3;
numb3 = temp;
}
if (numb2 > numb3) {
int temp = numb2;
numb2 = numb3;
numb3 = temp;
}
System.out.printf("Sorted numbers: %d %d %d\n", numb1, numb2, numb3);
}
}
import java.util.Scanner;
public class WindChill {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Enter the temperature in Fahrenheit
System.out.print("Enter the temperature in Fahrenheit: ");
double fahrenheit = input.nextDouble();
if (fahrenheit < -58 || fahrenheit > 41) {
System.out.println("Temperature must be between -58�F and 41�F");
}
else {
// Enter the wind speed miles per hour
System.out.print("Enter the wind speed miles per hour: ");
double speed = input.nextDouble();
if (speed < 2)
{
System.out.println("Speed must be greater than or equal to 2");
}
else {
// Compute wind chill index
double windChillIndex = 35.74 + 0.6215 * fahrenheit - 35.75 * Math.pow(speed, 0.16)
+ 0.4275 * fahrenheit * Math.pow(speed, 0.16);
// Display the result
System.out.println("The wind chill index is " + windChillIndex);
}
}
}
public static void mainxxx(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
// Enter the temperature in Fahrenheit
System.out.print("Enter the temperature in Fahrenheit: ");
double fahrenheit = input.nextDouble();
if (fahrenheit < -58 || fahrenheit > 41)
{
System.out.println("Temperature must be between -58�F and 41�F");
System.exit(1);
}
// Enter the wind speed miles per hour
System.out.print("Enter the wind speed miles per hour: ");
double speed = input.nextDouble();
if (speed < 2)
{
System.out.println("Speed must be greater than or equal to 2");
System.exit(2);
}
// Compute wind chill index
double windChillIndex = 35.74 + 0.6215 * fahrenheit - 35.75 * Math.pow(speed, 0.16)
+ 0.4275 * fahrenheit * Math.pow(speed, 0.16);
// Display the result
System.out.println("The wind chill index is " + windChillIndex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment