Basic questions
Question 1: Grammar exercises
-
Grammar points: method definition, method overloading
-
Write the code step by step, and the effect is as shown in the figure:
-
Writing steps:
- Define the class Test1, define the main method in the class, and define the int type a as 10 and b as 10.
- Define the printNum method, the parameters are (int iVar, int iVar2), the return value is none, and the values of iVar and iVar2 are output.
- Define the doubling method, the parameters are (int r, int p), the return value is none, r is doubled and p is doubled in the method, and the printNum method is called to output the values of r and p.
- Define the doubling method, the parameter is (int r), the return value is int, r is doubled in the method, and r is returned.
- In the main method, call the printNum method and pass in a and b
- In the main method, call the doubling method and pass in a and b
- In the main method, call the printNum method and pass in a and b
- In the main method, call the doubling method, pass in a, and use a to receive the return value
- In the main method, call the doubling method, pass in b, and use b to receive the return value.
- In the main method, call the printNum method and pass in a and b
-
Reference answer:
public class Test1 { public static void main(String[] args) { // 定义int类型a为10, b为10. int a = 10; int b = 10; printNum(a, b); doubling(a, b); printNum(a, b); System.out.println("***********************"); a = doubling(a); b = doubling(b); printNum(a, b); } // 定义printNum方法,参数为(int iVar, int iVar2),返回值无,输出iVar和iVar2的值 public static void printNum(int iVar, int iVar2) { System.out.println("iVar:" + iVar + ", iVar2:" + iVar2); } // 定义doubling方法,参数为(int r, int p),返回值无,方法内r翻倍,p翻倍,并调用printNum方法,输出r和p的值 public static void doubling(int r, int p) { r *= 2; p *= 2; System.out.println("翻倍:r=" + r + ",p=" + p); } // 定义doubling方法,参数为(int r),返回值int, 方法内r翻倍,返回r. public static int doubling(int r) { r *= 2; return r; } }
Question 2: Grammar exercises
-
Grammar points: method definition, if
-
Write the code step by step, and the effect is as shown in the figure:
-
Writing steps:
- Define the class Test2 and define the main method in the class
- Define the doCheck method, the parameter is (int iVar), and the return value is boolean type
- Within the doCheck method, define the variable boolean flag.
- In the doCheck method, determine whether num is an even number.
- If it is an even number, use a for loop, the initial value i is 0, i<=20 enters the loop, and the step expression i++
- Within the loop, num-=i;
- flag is assigned a value of true.
- Otherwise, if it is an odd number, use a for loop, initialize the value i to 0, enter the loop with i<=20, and step the expression i++
- Within the loop, num+=i;
- flag is assigned a value of false.
- Output the value of num
- return flag
- Call the doCheck method, pass in 2, save the return value, and output
- Call the doCheck method, pass in 3, save the return value, and output
-
Reference answer:
public class Test2 { public static void main(String[] args) { boolean b = doCheck(2); System.out.println(b); boolean b1 = doCheck(3); System.out.println(b1); } // 定义doCheck方法,参数为(int iVar),返回值boolean类型 public static boolean doCheck(int num){ // 3.doCheck方法内,定义变量boolean flag. boolean flag ; // 4.doCheck方法内,判断num是否为偶数. if (num % 2== 0 ) { // 如果是偶数,使用for循环,初始化值i为0,i<=20进入循环,步进表达式i++ for (int i = 0; i <= 20; i++) { num-=i; } flag = true; }else { // 否则是奇数,使用for循环,初始化值i为0,i<=20进入循环,步进表达式i++ for (int i = 0; i <= 20; i++) { num+=i; } flag = false; } // 输出num的值 System.out.println("num:"+ num); return flag; } }
Question 4: Requirements realization
-
Define the showColor method to output the corresponding color based on the English word.
-
Code implementation, the effect is as shown in the figure:
-
Reference answer:
public class Test4 { public static void main(String[] args) { showColor("red"); } // showColor方法中,使用switch语句,判断颜色. public static void showColor(String color) { switch (color) { case "red": System.out.println(color + "是红色!"); break; case "blue": System.out.println(color + "是蓝色!"); break; case "green": System.out.println(color + "是绿色!"); break; default: System.out.println(color+" 颜色未知!"); } } }
Question 5: Requirement realization
-
Define the getValue method to obtain the maximum value among three numbers. You can specify the maximum or minimum value through a string.
-
Code implementation, the effect is as shown in the figure:
-
Development tips:
- In getValue, there is a String type parameter, which can specify "big" or "small"
- Define the maximum value method and the minimum value method respectively for call by getValue.
-
Reference answer:
public class Test5 { public static void main(String[] args) { getExtValue("小" , 5, 6, 7); } // 定义getExtValue方法,参数为(String str, int n, int n2, int n3),返回值无 public static void getExtValue(String ext , int n1,int n2 , int n3) { switch (ext) { // // 当str为大时,调用getMax方法,获取n,n2,n3中的最大值输出 case "大": int max = getMax(n1,n2,n3); System.out.println("最大值为:" + max); break; // 当str为小时,调用getMin方法,获取n,n2,n3中的最小值输出 case "小": int min = getMin(n1,n2,n3); System.out.println("最小值为:" + min); break; default: System.out.println("指令有误!"); } } private static int getMin(int i, int j, int k) { int min = i < j ? (i < k ? i : k) : (j < k ? j : k); return min ; } private static int getMax(int i, int j, int k) { int max = i > j ? (i > k ? i : k) : (j > k ? j : k); return max ; } }
Question Six: Requirements Realization
-
Define the printX method to print any line of graphics.
-
Code implementation, the effect is as shown in the figure:
-
Development tips:
- Refer to the previous exercise to extract the code into a method.
-
Reference answer:
public class Test6 { public static void main(String[] args) { printX(10); } /* 2.定义printX方法,参数为(int m) , 返回值无 3.printX方法中,使用for循环,初始化变量x = 1,如果x<=m进入循环,步进表达式x++ 4.for循环内部,再嵌套定义一套for循环,初始化变量y = 1,如果y<=m进入循环,步进表达式y++ 5.在内循环内部,判断x==y 或者 x+y==1+m ,则打印"O",否则打印"*" */ public static void printX(int m) { for (int x = 1; x <= m; x++) { //循环7行 for (int y = 1; y <= m; y++) { //循环7列 if (x == y || x + y == m + 1) { //对角线打印O System.out.print("O"); } else { System.out.print("*");//其他位置打印. } } System.out.println();//换行 } } }
Question 7: Requirements realization
-
Define the round method, receive one decimal place, implement rounding operation, and return the result.
-
Code implementation, the effect is as shown in the figure:
-
Development tips:
- In the round method, after the parameter is +0.5, it is converted to int type and returned.
-
Reference answer:
public class Test7 { public static void main(String[] args) { System.out.println( 10.1 + "->"+ round(10.1)); System.out.println( 10.4 +"->"+ round(10.4)); System.out.println( 10.5 +"->"+ round(10.5)); System.out.println( 10.9 +"->"+ round(10.9)); } // 定义round方法,参数为(double d) , 返回值int public static int round(double d) { // round方法中,d+0.5后,转换为int类型,并返回. int n = (int) (d + 0.5); return n; } }