1. 程式人生 > >12-26作業

12-26作業

switch實現135學習,246複習,週日休息

程式碼如下:

 1 package com.kk;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Xuexi03 {
 6     public static void main(String[] args) {
 7         int week=0;
 8         Scanner sc=new Scanner(System.in);
 9         
10         do {
11             System.out.println("請輸入星期幾(1-7):");
12 week=sc.nextInt(); 13 }while(week<1||week>7); 14 switch (week) { 15 case 1: 16 case 3: 17 case 5: 18 System.out.println("今天要學習"); 19 break; 20 case 2: 21 case 4: 22 case
6: 23 System.out.println("今天要複習"); 24 break; 25 case 7: 26 System.out.println("週日休息"); 27 break; 28 } 29 } 30 }

結果如下:

 

 

 

 

 

小遊戲:

 1 package com.kk;
 2 
 3 import
java.util.Scanner; 4 5 public class Suiji0000 { 6 public static void main(String[] args) { 7 Scanner sc=new Scanner(System.in); 8 int money=1000; 9 System.out.println("--------歡迎來到BD22大賭場-------------"); 10 11 while(true) { 12 System.out.println("是否開始遊戲(y/n)?"); 13 String choose=sc.next(); 14 if(!"n".equals(choose)) { 15 int a=(int)(Math.random()*6)+1; 16 int b=(int)(Math.random()*6)+1; 17 int c=(int)(Math.random()*6)+1; 18 //3-9為小,10-18為大 19 String result=a+b+c>=10?"大":"小"; 20 21 System.out.println("請輸入下注金額:"); 22 int pay=sc.nextInt(); 23 System.out.println("請壓大壓小:"); 24 String guess=sc.next(); 25 26 System.out.println("買定離手:"+a+","+b+","+c+"-----"+result); 27 if(guess.equals(result)) { 28 System.out.println("恭喜你壓中"); 29 money+=pay; 30 }else { 31 System.out.println("未中獎"); 32 money-=pay; 33 } 34 System.out.println("當前餘額:"+money); 35 }else { 36 System.out.println("謝謝"); 37 break; 38 } 39 } 40 System.out.println("遊戲結束"); 41 } 42 }

效果如下: