2013年2月8日 星期五

TQC+ JAVA 條件判斷式 201 ~ 210

TQC+ JAVA6 試題總覽:LINK
Github 備份:LINK

/* TQC+ JAVA6 - 201 */
import java.util.Scanner;
public class JPA201 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Please enter score");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
test(a);
int b = sc.nextInt();
test(b);
}
//建立一個static的方法,來判斷是否有大於60
public static void test(int c) {
if(c>=60)
System.out.println("You Pass");
//以下這行不屬於if的判斷式之內,故無論if是否成立,皆會運作下列該行
System.out.println("End");
}
}
/* TQC+ JAVA6 - 202 */
import java.util.*;
class JPA202{
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
test();
test();
}
public static void test() {
System.out.println("Input:");
Scanner sc = new Scanner(System.in);
int a,b;
a = sc.nextInt();
b = sc.nextInt();
//建立條件判斷式,結果可能有三種狀況,a>b,a=b,a<b,所以判斷式處理如下
if(a>b)
System.out.printf("%d is larger than %d\n",a,b);
else if(b>a)
System.out.printf("%d is larger than %d\n",b,a);
else
System.out.printf("%d is equal to %d\n",a,b);
}
}
/* TQC+ JAVA6 - 203 */
import java.util.*;
public class JPA203 {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
test();
test();
}
static void test() {
System.out.println("Input:");
Scanner sc = new Scanner(System.in);
int a;
a = sc.nextInt();
//要判斷奇偶數,便可用mod2的運算,如果整除2,則mod2=0,視為偶數;反之,mod2=1,沒有整除2,而為奇數
if((a%2)==0)
System.out.printf("The number is even.\n");
else if((a%2)==1)
System.out.printf("The number is odd.\n");
else
System.out.printf("Error!!");
}
}
/* TQC+ JAVA6 - 204 */
import java.util.*;
class JPA204 {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
test();
test();
}
public static void test() {
System.out.println("Input:");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
//使用and的運算,並且同時符合mod5=0和mod9=0的數字,使得if的判斷式成立
if((a%5)==0 && (a%9)==0)
System.out.printf("Yes\n");
else
System.out.printf("No\n");
}
}
/* TQC+ JAVA6 - 205 */
import java.util.*;
public class JPA205 {
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:");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
//先判斷是否是6的倍數,由最大的數開始判斷
if ((a%6)==0)
System.out.printf("%d是2、3、6的倍數\n",a);
//如果不是6的倍數,再判斷是否為2或3的倍數,其中判斷2或3的順序沒有差
else if ((a%2)==0)
System.out.printf("%d是2的倍數\n",a);
else if ((a%3)==0)
System.out.printf("%d是3的倍數\n",a);
else
System.out.printf("%d不是2、3、6的倍數\n",a);
}
}
/* TQC+ JAVA6 - 206 */
import java.util.*;
public class JPA206 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
test();
test();
test();
test();
}
static void test() {
int chi, eng, math;
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個別獨立去判斷,個別的分數是否不及格
if(chi<60)
System.out.printf("Chinese failed.\n");
if(eng<60)
System.out.printf("English failed.\n");
if(math<60)
System.out.printf("Math failed.\n");
if(chi>60 && eng>60 && math>60)
System.out.printf("All pass.\n");
}
}
/* TQC+ JAVA6 - 207 */
import java.util.*;
public class JPA207 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
test();
test();
test();
test();
}
static void test() {
System.out.print("請輸入三個整數:");
Scanner sc = new Scanner(System.in);
int[] n= new int[3];
n[0]=sc.nextInt();
n[1]=sc.nextInt();
n[2]=sc.nextInt();
//使用Arrays.sort(n),將陣列中的數字由小排到大
Arrays.sort(n);
//三角形條件判斷,兩個短邊相加大於最大邊,並且其中一邊的數不可為零
if(n[0]+n[1]>n[2]&&n[0]*n[1]*n[2]!=0)
{
//直角三角形:較小的兩邊平方和等於最大邊的平方
if(n[0]*n[0]+n[1]*n[1]==n[2]*n[2])
System.out.print("直角三角形\n");
//鈍角三角形:較小的兩邊平方和小於最大邊的平方
else if(n[0]*n[0]+n[1]*n[1]<n[2]*n[2])
System.out.print("鈍角三角形\n");
//銳角三角形:較小的兩邊平方和大於最大邊的平方
else if(n[0]*n[0]+n[1]*n[1]>n[2]*n[2])
System.out.print("銳角三角形\n");
}
//若無法構成三角形則進入此判斷式
else
System.out.print("不可以構成三角形\n");
}
}
/* TQC+ JAVA6 - 208 */
import java.util.*;
class JPA208 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
test();
test();
test();
test();
test();
}
//建立分數判斷的方法,在main方法中,宣告五次,便執行五次
public static void test() {
System.out.println("Input:");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
//使用條件判斷式,"if" "else if" "else",因為一定只能符合其依規則,不能用多個if來判斷
//這邊使用漏斗的寫法,如果沒有大於等於90那該數一定小於90往下繼續執行,那底下便只需寫判斷大於等於80即可,不用再判斷是否小於90
if(a>=90)
System.out.printf("Your grade is A\n");
else if(a>=80)
System.out.printf("Your grade is B\n");
else if(a>=70)
System.out.printf("Your grade is C\n");
else if(a>=60)
System.out.printf("Your grade is D\n");
else if(a<60)
System.out.printf("Your grade is F\n");
}
}
/* TQC+ JAVA6 - 209 */
import java.util.*;
public class JPA209 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
test();
test();
test();
test();
}
public static void test() {
Scanner sc = new Scanner(System.in);
double x,y;
System.out.print("請輸入 x 座標:");
x = sc.nextDouble();
System.out.print("請輸入 y 座標:");
y = sc.nextDouble();
if(x==0.0&&y==0.0)
System.out.printf("(%1.2f,%1.2f)在原點上\n",x,y);
else if (x==0.0)
System.out.printf("(%1.2f,%1.2f)在y軸上\n",x,y);
else if (y==0.0)
System.out.printf("(%1.2f,%1.2f)在x軸上\n",x,y);
else if (x>0.0&&y>0.0)
System.out.printf("(%1.2f,%1.2f)在第一象限上\n",x,y);
else if (x<0.0&&y>0.0)
System.out.printf("(%1.2f,%1.2f)在第二象限上\n",x,y);
else if (x<0.0&&y<0.0)
System.out.printf("(%1.2f,%1.2f)在第三象限上\n",x,y);
else if (x>0.0&&y<0.0)
System.out.printf("(%1.2f,%1.2f)在第四象限上\n",x,y);
}
}
import java.util.*;
class JPA210 {
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:");
Scanner sc = new Scanner(System.in);
//讀取字串
String tm = sc.next();
//僅擷取自串的第一個字元
char tm0 = tm.charAt(0);
switch(tm0) {
case 'a'://這邊不需要特別去處理,因為沒有寫break,所以會繼續跑到case 'B'的內容,直到碰到break
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;
};
}
}


TQC+ JAVA6 試題總覽:LINK
Github 備份:LINK

本篇教學的程式碼皆由筆者編輯,歡迎轉貼本教學,但請全文轉貼,謝啦~

沒有留言:

張貼留言