Chapter 4 Selection Structure (2)

1, switch output week
public class Week{

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int shu =1;
    while(shu!=0) {
     System.out.print("Day of the week: ");
      shu =input.nextInt();
     switch(shu) {
     case 1:
      System.out.println("Today is Monday");
      break;
     case 2:
      System.out.println("Today is Tuesday");
      break;
     case 3:
      System.out.println("Today is Wednesday");
      break;
     case 4:
      System.out.println("Today is Thursday");
      break;
     case 5:
      System.out.println("Today is Friday");
      break;
     case 6:
      System.out.println("Today is Saturday");
      break;
     case 7:
      System.out.println("Today is Sunday");
      break;
   
     }
   if(shu>=7&&shu<=10) {System.out.println("Please re-enter!");
   continue;
   
  } System.out.println("Program end!"); }
}
}
2. Air ticket price
public class Fjc {
	public static void main(String[] args) {
		double money = 5000; //Air ticket price
 		Scanner input = new Scanner(System.in);
		System.out.print("Please enter the month you travel:\n");
		if (input.hasNext() ) {
			int num = input.nextInt();
			System.out.println("Would you like to choose first class or economy class? Enter 1 for first class and 2 for economy class");
			int choose = input.nextInt();
			switch (num) {
			case 9:
			case 8:
			case 7:
			case 6:
			case 5:
			case 4:
				if (choose == 1) {
					money = money * 0.9;
					break;
				}
				if (choose == 2) {
					money = money * 0.8;
					break;
				}
			default:
				if (choose == 1) {
					money = money * 0.5;
					break;
				}
				if (choose == 2) {
					money = money * 0.4;
				}
				break;
			}
			System.out.println("Your ticket price is: " + money);
		} else {
			System.out.println("Please enter the correct number");
		}
	}
}
3. Study plan
public class Curricula {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Please enter the day of the week: ");
		int dayofWeek = input.nextInt();
		switch (dayofWeek) {
		case 1:
		case 3:
		case 5:
			System.out.println("Learn to program");
			break;
		case 2:
		case 4:
		case 6:
			System.out.println("Learn English");
			break;
		default:
			System.out.println("休息");
		}
	}
}
4. Mini calculator
public class Calculator {
       public static void main(String[] args) {
              Scanner input = new Scanner(System.in);
              System.out.println("Please enter your first operand:");
		    double num1 = input.nextDouble();
		    System.out.println("Please enter your second operand:");
		         if (input.hasNextDouble() ) {
			 double num2 = input.nextDouble();
			 System.out.println("Please choose your operation mode: 1 addition, 2 subtraction, 3 multiplication, 4 division");
			     int choose = input.nextInt();
			     switch (choose) { //Use switch to select calculation method
 			        case 1:
				    System.out.println("Calculation result: " + num1 + "+" + num2+ "=" + (num1+ num2));
   	                        break;
			        case 2:
				    System.out.println("计算结果:" + number + "-" + number1 + "=" + (num1 - num2));
				break;
			        case 3:
				    System.out.println("计算结果:" + number + "*" + number1 + "=" + (num1 * num2));
				break;
			        case 4:
                                    System.out.println("计算结果:" + number + "/" + number1 + "=" + (num1 / num2));
				break;
			      }
		           } else {
			          System.out.println("Please enter the correct number");
		     }
	    }
  }



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325990985&siteId=291194637