close

201

import java.util.Scanner;
public class JPA02 { //修改檔名
    static Scanner keyboard = new Scanner(System.in);
    
    public static void main(String[] args) {
        test();
        test();
    }
    
    public static void test() {
        System.out.println("Please enter score:");  //要求輸入分數(println換行)
        int x;  //設整數型態的變數
        x = keyboard.nextInt();  //將使用者輸入的值指派給變數x
        if (x >= 60){  //if條件判斷 大於等於60
            System.out.println("You pass");
        } //結束條件判斷
        System.out.println("End");  //最後按題目要求輸出End
    }
}

202

import java.util.*;
class JPA02 { //更改檔名
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String[] args) {
        test();
        test();
    }
    
    public static void test() {
        System.out.println("Input:"); //要求使用者輸入
        int x,y;
        x = keyboard.nextInt();  //輸入第一個整數
        y = keyboard.nextInt();  //輸入第二個整數
        if (x > y){   //若 x > y
            System.out.printf("%d is larger than %d\n",x, y); //格式化輸出並換行
        }
        else if(y > x){ //若 y > x
            System.out.printf("%d is larger than %d\n",y, x); //格式化輸出並換行
        }
    }
}

203

import java.util.*;
public class JPA02 { //更改檔名
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
        test();
        test();
    }
    
    static void test() {
        System.out.println("Input an integer:"); //要求輸入整數
        int x = input.nextInt();  //讀取整數
        if (x % 2 == 0){  //若除以2餘數為0
            System.out.println("The number is even.");
        }
        else{  //若除以2餘數不為0
            System.out.println("The number is odd.");
        }
    }
}

204

import java.util.*;
class JPA02 { //更改檔名
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
        test();
        test();
    }
  
    public static void test() {
        System.out.println("Input:"); //要求使用者輸入
        int x = input.nextInt();  //讀取輸入
        if ((x % 5 == 0) && (x % 9  == 0)){ // && 表示需同時滿足
            System.out.println("Yes");
        }
        else{
            System.out.println("No");
        }
    }
}

205

import java.util.*;
public class JPA02 { //更改檔名
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
        test();
        test();
        test();
        test();
    }
    
    static void test() {
        System.out.println("Enter an integer:"); //要求使用者輸入
        int x = input.nextInt();  //讀取輸入值
        //若為6的倍數,代表一定為2、3、6的倍數
        if ((x % 2 == 0) && (x % 3 == 0)){ //若為2或3的倍數
            System.out.printf("%d是2、3、6的倍數\n",x);
        }else if(x % 2 == 0){
            System.out.printf("%d是2的倍數\n",x);  //若只為2的倍數     
        }else if(x % 3 == 0){
            System.out.printf("%d是3的倍數\n",x);  //若只為3的倍數        
        }else{
            System.out.printf("%d不是2、3、6的倍數\n",x);        
        }
    }
}

206

import java.util.*;
public class JPA02 {
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String[] args) {
        test();        
        test();
        test();
        test();
    }
    
    static void test() {
        int chi, eng, math, avg;
        System.out.print("Input Chinese score:");
        chi = keyboard.nextInt();
        System.out.print("Input English score:");
        eng = keyboard.nextInt();
        System.out.print("Input Math score:");
        math = keyboard.nextInt();
        //因為不同科目不及格要同時顯示出不及格訊息,所以每個都要用獨立的if條件判斷
        //若使用else if只要滿足第一個條件,第二個不及格科目將不被顯示
        if (chi < 60){
            System.out.println("Chinese failed");
        }if (eng < 60){
            System.out.println("English failed");
        }if (math < 60){
            System.out.println("Math failed");
        }if ((chi >=60) && (eng >= 60) && (math >= 60)) { //若三個條件同時滿足(皆及格)
            System.out.println("All pass");
        }
    }
}

207

import java.util.*;
public class JPA02 {
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String[] args) {
        test();        
        test();
        test();
        test();
    }
    
    static void test() {
        System.out.println("請輸入三個整數:");
        int a,b,c;
        a = keyboard.nextInt();
        b = keyboard.nextInt();
        c = keyboard.nextInt();
        //滿足 a,b,c 不等於0、任兩邊相加都需大於大三邊(同時滿足)
        if((a!=0 && b!=0 && c!=0 && (a+b>c) && (a+c>b) && (b+c>a))){
            if((a*a+b*b==c*c) || (a*a+c*c==b*b) || (b*b+c*c==a*a)){ // "||"為部分滿足(只要符合其中一項條件即可)
                System.out.println("直角三角形");
            }else if((a*a+b*b<c*c) || (a*a+c*c<b*b) || (b*b+c*c<a*a)){
                System.out.println("鈍角三角形");
            }else if((a*a+b*b>c*c) || (a*a+c*c>b*b) || (b*b+c*c>a*a)){
                System.out.println("銳角三角形");
            }
        }
        else{ //若不滿足三角形構成條件
            System.out.println("不可以構成三角形");
        }
        
    }
}

208

import java.util.*;
class JPA02 {  //更改檔名
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String[] args) {
        test();
        test();
        test();
        test();
        test();
    }

    public static void test() {
        System.out.println("Input:");  //要求使用者輸入
        int x = keyboard.nextInt();  //讀取輸入值
        //若 if 條件只包含一行內容,可以不使用大括號
        if(x>=90)System.out.println("Your grade is A"); //若大於90,不滿足則往下判斷
        else if(x>=80)System.out.println("Your grade is B"); //若大於80,不滿足則往下判斷
        else if(x>=70)System.out.println("Your grade is C"); //若大於70,不滿足則往下判斷
        else if(x>=60)System.out.println("Your grade is D"); //若大於60,不滿足則往下判斷
        else System.out.println("Your grade is F"); //若以上條件都不滿足,則顯示此訊息
    }
}

209

import java.util.*;
public class JPA02 {
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String[] args) {
        test();        
        test();
        test();
        test();
    }
    
    public static void test() {
        double x,y;  //要用浮點數輸出,所以設變數為 double 型態
        System.out.print("請輸入x座標:");   //要求使用者輸入
        x = keyboard.nextDouble();        //讀取輸入值
        System.out.print("請輸入y座標:");
        y = keyboard.nextDouble();
        if (y == 0){       // y座標為0時,代表該座標位於x軸上
            System.out.printf("(%.2f,%.2f)在x軸上\n",x,y);
        }else if(x == 0){ // x座標為0時,代表該座標位於y軸上
            System.out.printf("(%.2f,%.2f)在y軸上\n",x,y);
        }else if (x > 0 && y > 0){
            System.out.printf("(%.2f,%.2f)在第一象限\n",x,y);
        }else if (x < 0 && y > 0){
            System.out.printf("(%.2f,%.2f)在第二象限\n",x,y);
        }else if (x < 0 && y < 0){
            System.out.printf("(%.2f,%.2f)在第三象限\n",x,y);
        }else if (x > 0 && y < 0){
            System.out.printf("(%.2f,%.2f)在第四象限\n",x,y);
        }
        

    }
}

210

import java.util.*;
class JPA02 {
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String[] args) {
        test();
        test();
        test();
        test();
        test();
    }
  
    public static void test() {
        System.out.println("Input a character:");
        char q = keyboard.next().charAt(0);  //char為字串型態 ,讀取字串的第0項內容
        switch(q){  //使用 switch 條件式
            case 'a':
            case 'b':
                System.out.println("You entered a or b");
                break;
            case 'x':
                System.out.println("You entered x");
                break;
            case 'y':
                System.out.println("You entered y");
                break;
            default:
                System.out.println("You entered something else.");
                break;
        }
    }
}


 

arrow
arrow
    全站熱搜

    ivankao 發表在 痞客邦 留言(0) 人氣()