學習Java也快10天了,我的以爲學習過程有點枯燥,本身的效率也有點低。把本身作過的做業記錄下來,算是鞭策本身吧。java
做業1 從1加到100:
public class HomeWork { public static void main(String[] args ) { int n = 100; int sum = ((1 + n) * n) / 2; System.out.println(sum); System.out.println(sum == 5050 ? "pass" : "faild"); //A ? B : C if A = T,run B;else run C } }
做業2 計算成績增加率:
import java.util.Scanner; public class HomeWork { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Input your new grade: "); int grade_new = scanner.nextInt(); System.out.print("Input your last grade: "); int grade_last = scanner.nextInt(); int grade_diff = grade_new - grade_last; System.out.println(grade_diff); float grade_pcent = (float)grade_diff / (float)grade_last; System.out.println(grade_pcent); double incre_pcent = grade_pcent * 100.00; System.out.println(incre_pcent); System.out.printf("Your grade increase percentage is %.2f%%.", incre_pcent); scanner.close(); } }
做業3 計算體重BIM同時判斷身體狀態:
import java.util.Scanner; public class HomeWork { public static void main(String[] args) { System.out.print("Please enter your weight in Kg:"); Scanner scanner = new Scanner(System.in); float weight = scanner.nextFloat(); System.out.print("Please enter your height in m:"); float height = scanner.nextFloat(); float BMI = weight / (height * height); System.out.printf("Your BMI is:%.2f.\n", BMI); if (BMI < 18.5) { System.out.println("You are underweight."); } else if (BMI >= 18.5 && BMI < 25) { System.out.println("Your weight is normal."); } else if (BMI >= 25 && BMI < 28) { System.out.println("You are overweight."); } else if (BMI >= 28 && BMI < 32) { System.out.println("You are at risk for obesity."); } else { System.out.println("Watch out!" + "You are seriously overweight!"); } scanner.close(); } }
做業4 「剪刀石頭布」遊戲:
import java.util.Scanner; /* * Switch Multiple choices - Rock-paper-scissors game */ public class Game { public static void main(String[] args) { System.out.println("Please select:"); System.out.println("1:Rock"); System.out.println("2:Scissors"); System.out.println("3:Paper"); Scanner scanner = new Scanner(System.in); int choice = 0; int random = 1 + (int) (Math.random() * 3); System.out.println("Tell me your choice:"); choice = scanner.nextInt(); switch (choice) { case 1: if (random == 1) { System.out.println("Uh-oh, it's a tie game."); } else if (random == 2) { System.out.println("You win!"); } else { System.out.println("Sorry, You loose."); } break; case 2: if (random == 1) { System.out.println("Sorry, You loose."); } else if (random == 2) { System.out.println("YUh-oh, it's a tie game."); } else { System.out.println("You win!"); } break; case 3: if (random == 1) { System.out.println("You win!"); } else if (random == 2) { System.out.println("Sorry, You loose."); } else { System.out.println("YUh-oh, it's a tie game."); } break; default: System.out.println("Pleas choice again."); break; } if (random == 1) { System.out.println("Your opponent chooses the Rock."); } else if (random == 2) { System.out.println("Your opponent chooses the Scissors."); } else { System.out.println("Your opponent chooses the Paper."); } scanner.close(); } }
做業5 while循環求和:
/* * While loop exercise */ public class HomeWork { public static void main(String[] args) { int sum = 0; int m = 20; int n = 100; while(m<=n) { sum = sum + m; m++; } System.out.println(sum); } }
做業6 do while循環求和:
/* * do while loop exercise */ public class HomeWork { public static void main(String[] args) { int sum = 0; int m = 20; int n = 100; do { sum = sum + m; m++; } while (m <= n); System.out.println(sum); } }
做業7 倒敘輸出數組:
/* * if loop exercise1 */ public class HomeWork { public static void main(String[] args) { int[] ns = {1, 4, 9 ,16,25}; for (int i = 4; i >= 0; i--) { System.out.println(ns[i]); } } }
做業8 for循環練習2:
/* * for loop exercise2 */ public class HomeWork { public static void main(String[] args) { int[] ns = {1, 4, 9, 16, 25}; int sum = 0; for (int n : ns) { sum = sum + n; } System.out.println(sum); } }
做業9 計算圓周率(for循環練習3):
/* * for loop exercise3 */ public class HomWork { public static void main(String[] args) { double pi = 0; double n = 1; for (int i = 1; i <= 9999999; i = i + 2) { pi = pi + Math.pow(-1.0, n-1) * (4 / (double)i); n++; } System.out.printf("%.10f", pi); } }
做業10 類練習:
public class ClassCity { public static void main(String[] args) { City bj = new City(); bj.name = "BeiJing"; bj.latitude = 39.903; bj.longitude = 116.401; System.out.println(bj.name); System.out.println("location: " + bj.latitude + ", " + bj.longitude); } } class City { public String name; public double latitude; public double longitude; }
做業11 類中方法練習:
public class HomeWork { public static void main(String[] args) { Person ming = new Person(); ming.setName("Xiao Ming"); ming.setAge(15); System.out.println(ming.getName()); System.out.println(ming.getAge()); } } class Person { private String name; private int age; public String getName() { return name; } public int getAge() { return age; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } }
做業12 類中方法練習2:
public class HomeWork { public static void main(String[] args) { Person ming = new Person("Xiao Ming", 12); System.out.println(ming.getName()); System.out.println(ming.getAge()); } } class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } }
做業13 方法重載練習:
/* * exercise of overload */ public class HomeWork { public static void main(String[] args) { Person ming = new Person(); Person hong = new Person(); ming.setName("Xiao Ming"); hong.setName("Xaio", "Hong"); System.out.println(ming.getName()); System.out.println(hong.getName()); } } class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void setName(String firstname, String lastname) { this.name = firstname + " " + lastname; } }