1. 程式人生 > >java代碼示例(3)

java代碼示例(3)

三目運算 星期幾 cor operator 釣魚 double logs 成績 ted

 1 /**
 2  * 需求分析:根據輸入的天數是否是周六或是周日,
 3  * 並且天氣的溫度大於28攝氏度,則外出遊泳,否則釣魚
 4  * @author chenyanlong
 5  * 日期:2017/10/14
 6  */
 7 package com.hp.test03;
 8 
 9 import java.util.Scanner;
10 
11 public class HS_JudgeOutgoing {
12 
13     public static void main(String[] args) {
14         // TODO Auto-generated method stub
15 int day; 16 double temperature; 17 //double temperature = 0.0; 18 19 System.out.println("請輸入今天星期幾,如果周n ,請輸入”n“,eg:7"); 20 Scanner input=new Scanner(System.in); 21 day=input.nextInt(); 22 23 if(day==6||day==7){ 24 //
溫度判斷 25 System.out.println("請輸入今天的溫度,eg:29.8"); 26 Scanner input2=new Scanner(System.in); 27 temperature=input2.nextDouble(); 28 System.out.println("溫度:"+temperature); 29 if(temperature>25){ 30 System.out.println("今天適合——遊泳");
31 }else{ 32 System.out.println("今天適合——釣魚"); 33 } 34 }else{ 35 System.out.println("你還是老實寫代碼!!"); 36 } 37 38 39 } 40 }
 1 /**
 2  * 需求分析:判斷成績是否合格
 3  */
 4 package com.hp.test03;
 5 
 6 import java.util.Scanner;
 7 
 8 public class HS_JudgeScore {
 9 
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12          int score;
13          System.out.println("請輸入你的成績");
14          
15          Scanner input=new Scanner(System.in);
16          score=input.nextInt();
17          
18          //判斷成績是否合格
19          if(score<60){
20              System.out.println("成績不及格");
21          }else if(score<70){
22              System.out.println("成績差");
23          }else if(score<80){
24              System.out.println("成績中等");
25          }
26          else if(score<90){
27              System.out.println("成績良好");
28          }else{
29              System.out.println("成績優秀");
30          }
31          
32     }
33 
34 }
/**
 * 需求說明: 根據分解後的數字之和,判斷用戶是否中獎
 * @author chenyanlong
 * 時間:2017/10/14
 */
package com.hp.test03;

import java.util.Scanner;

public class HS_SumGetMp3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //定義用到的所有變量
        int  num,a,b,c,d,sum;
        System.out.println("請輸入4位的會員卡號:");
        
        //鍵盤數據的輸入與讀取
        Scanner input=new Scanner(System.in);
        num=input.nextInt();
        
        //分離num,抽出千位,百位,十位,個位
        System.out.println("會員卡號是:"+num);
        System.out.println("----------------------------------");
        a=num/1000;//千位
        b=num/100%10;//百位
        c=num/10%10;//十位
        d=num%10;//個位
        System.out.println("千位數:"+a+",百位數:"+b+",十位數:"+c+",個位數:"+d);
        System.out.println("----------------------------------");
       
        //a,b,c,d之和
        sum=a+b+c+d;
        System.out.println("會員卡號"+num+"各個位之和:"+sum);
        
        //判斷是否中獎
        if(sum>30){
            System.out.println("會員卡號"+num+"的會員,你中獎了!獎品是劍豪最新版皮膚!!!");
        }else{
            System.out.println("很遺憾,你沒有中獎!");
        }
    }

}
 1 /**
 2  * 需求說明: switch的基本使用
 3  * @author chenyanlong
 4  * 時間:2017/10/14
 5  */
 6 package com.hp.test03;
 7 
 8 import java.util.Scanner;
 9 
10 public class HS_Switch {
11 
12     public static void main(String[] args) {
13         
14       int score,a;
15       System.out.println("請輸入成績:");
16       Scanner input=new Scanner(System.in);
17       score=input.nextInt();
18        
19       a=score/10;
20        switch(a){
21           case 10:
22           case 9:
23               System.out.println("優秀");
24               break;
25           case 8:
26               System.out.println("良好");
27               break;
28           case 7:
29               System.out.println("中等");
30               break;
31           case 6:
32               System.out.println("差");
33               break;
34          default:
35              System.out.println("不及格");
36        }
37      
38     }
39 
40 }
 1 /**
 2  * 需求分析:三目運算符的基本使用
 3  * @author chenyanlong
 4  * 日期:2017/10/14
 5  */
 6 package com.hp.test03;
 7 
 8 public class HS_TernaryOperator {
 9 
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12        int min=3<1?2:3;//條件為false,輸入3
13        System.out.println("輸出了:"+min);
14        
15        int min2=3>1?2:3;//條件為ture,輸入2
16        System.out.println("輸出了:"+min2);
17        
18     }
19 
20 }

java代碼示例(3)