2013年2月8日 星期五

TQC+ JAVA 基本認識 101 ~ 110

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

/* TQC+ JAVA6 - 101 */
//程式中四處錯誤之處,建議將其全部刪除,整個重寫即可
public class JPA101 {
public static void main (String[] args) {
System.out.println("I love Java!");
System.out.println("Java is so good!");
}
}
/* TQC+ JAVA6 - 102 */
import java.util.Scanner;
public class JPA102 {
public static void main (String[] args) {
System.out.println("Please input:");
//使用Scanner這個方法來讀取鍵盤輸入
double k = new Scanner(System.in).nextDouble();
System.out.println(k+" kg = "+ (k*2.20462) + " ponds");
}
}
/* TQC+ JAVA6 - 103 */
import java.util.Scanner;
public class JPA103 {
public static void main (String[] args) {
System.out.println("Please input:");
Scanner sc = new Scanner(System.in);
//Scanner在讀取的時候會以空白為區隔
int a,b,c;
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
//這邊記得要將三個變數之和強制轉型成double,這樣除出來的數字才會是浮點數
System.out.printf("Average: %4.2f",((double)(a+b+c)/3));
}
}
/* TQC+ JAVA6 - 104 */
import java.util.Scanner;
public class JPA104 {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
double x1,x2,y1,y2;
System.out.print("輸入第1組的x和y座標:");
x1 = sc.nextDouble();
y1 = sc.nextDouble();
System.out.print("輸入第2組的x和y座標:");
x2 = sc.nextDouble();
y2 = sc.nextDouble();
//printf的用法是是顯示出一串字串,而字串中會有一些變數可帶入,例如%d 就是帶入整數,%f就是帶入浮點數,而%4.2f是指全部的位數有四位(包含小數點),而小數點後面佔兩位
//不過如果輸出的為數大於使用者輸出的,則會忽略使用者所輸入的位數限制
//Math.sqrt(X) = X開根號
//Math.pow(2.0,3.0) = 二的三次方,其中兩個變數皆是double形式
System.out.printf("介於(%4.2f,%4.2f)和(%4.2f,%4.2f)之間的距離是%4.2f", x1,y1,x2,y2,(Math.sqrt(Math.pow((x2-x1),2) + Math.pow((y2-y1),2))));
}
}
/* TQC+ JAVA6 - 105 */
import java.util.Scanner;
public class JPA105 {
public static void main (String[] args) {
System.out.print("請輸入您的姓名:");
Scanner sc = new Scanner(System.in);
String name = sc.next();
System.out.printf("Hi, %s,請輸入您的銅板的個數:\n",name);
System.out.print("請輸入1元的數量:");
int n1 = sc.nextInt();
System.out.print("請輸入5元的數量:");
int n5 = sc.nextInt();
System.out.print("請輸入10元的數量:");
int n10 = sc.nextInt();
System.out.print("請輸入50元的數量:");
int n50 = sc.nextInt();
int sum,G1,G2,G3,G4;
sum = n1*1 + n5*5 + n50*50 +n10*10;
//將總金額除以1000,小數點前面僅剩千位數這個數,並由浮點數存至整數,後面小數點皆會消失
G1 = sum/1000;
//將總金額除以100,小數點前面僅剩千位數和百位數這兩數,再進行mod運算,除以10所餘的數,這樣便僅會存在百位數
G2 = (sum/100)%10;
//將總金額除以10,小數點前面僅剩千位數和百位數和十位數這三數,再進行mod運算,除以10所餘的數,這樣便僅會存在十位數
G3 = (sum/10)%10;
//將總金額進行mod運算,除以10所餘的數,這樣便僅會存在個位數
G4 = sum%10;
System.out.printf("您的錢總共有: %d 千 %d 百 %d 十 %d 元",G1,G2,G3,G4);
}
}
/* TQC+ JAVA6 - 106 */
public class JPA106 {
public static void main (String[] args) {
double x;
x = -3.2;
System.out.printf("f(%.1f) = %4.4f\n",x,f(x));
x = -2.1;
System.out.printf("f(%.1f) = %4.4f\n",x,f(x));
x = 0;
System.out.printf("f(%.1f) = %4.4f\n",x,f(x));
x = 2.1;
System.out.printf("f(%.1f) = %4.4f\n",x,f(x));
}
static double f(double d) {
return (3*(Math.pow(d, 3))+2*d-1);
}
}
/* TQC+ JAVA6 - 107 */
public class JPA107 {
public static void main(String argv[]) {
int action = 1, skill = 2, teamgame = 3;
System.out.println("The basketball grade is " + Basketball.calGrade(action,skill,teamgame));
System.out.println("The baseball grade is " + Baseball.calGrade(skill,teamgame));
}
}
//建立一個class
class Basketball {
//在裡面再建立一個方法,並宣告成static
public static int calGrade(int a,int s,int t) {
return a + s + t;
}
}
class Baseball {
public static int calGrade(int s,int t) {
return 10 + s + t;
}
}
/* TQC+ JAVA6 - 108 */
public class JPA108 {
public static void main (String[] args) {
int i = add(2, 3);
double d = add(5.2, 4.3);
String s = add("I love ", "Java!!");
System.out.printf("%d %f %s %n", i, d, s);
}
public static int add(int a, int b) {
System.out.printf("Adding two integer: %d , %d \n",a,b);
return (a+b);
}
public static double add(double a, double b) {
System.out.printf("Adding two doubles: %2.1f , %2.1f \n",a,b);
return (a+b);
}
public static String add(String a, String b) {
System.out.printf("Adding two strings: %s, %s \n",a,b);
return (a+b);
}
}
/* TQC+ JAVA6 - 109 */
public class JPA109 {
public static int adder (int s1, int a1, int e1) {
//傳回加總後的數值回去,先傳到gameRating這個方法,再由gameRating傳回至main中
return (s1+a1+e1);
}
public static int gameRating (int s, int a, int e) {
//再將剛剛傳進來的傳入到adder這個方法中
return adder(s,a,e);
}
public static void main (String argv[]) {
int skill = 6, action = 9, excitment = 8, result;
//將數字傳入gameRating這個方法中
result = gameRating(skill, action, excitment);
System.out.print("The rating of the game is ");
System.out.println(result);
}
}
/* TQC+ JAVA6 - 110 */
public class JPA110 {
public static void main(String args[]) {
double totalarea;
System.out.printf("圓形面積為:%f \n",calCircle(5));
System.out.printf("三角形面積為:%f \n",calTriangle(10,5));
System.out.printf("方形面積為:%f \n",calRectangle(10,5));
totalarea = calCircle(5) + calTriangle(10,5) + calRectangle(10,5) ;
System.out.printf("此圖形面積為:%f \n",totalarea);
}
//宣告一個計算圓面積的方法
public static double calCircle(int a) {
return (a*a*3.1415926);
}
//宣告一個計算三角形面積的方法
public static double calTriangle(int a,int b) {
//記得要將a*b的乘積強制轉型成double,這樣除出來數字才會保留小數部分
return ((double)(a*b)/2);
}
//宣告一個計算長方形面積的方法
public static double calRectangle(int a,int b) {
return (a*b);
}
}


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

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

沒有留言:

張貼留言