day04-方法_定义练习
This commit is contained in:
51
day04/src/com/inmind/method01/Test02.java
Normal file
51
day04/src/com/inmind/method01/Test02.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package com.inmind.method01;
|
||||
/*
|
||||
|
||||
方法练习1_比较两个整数是否相同
|
||||
方法练习2_运算1到n的累和
|
||||
方法练习3_打印n遍HelloWorld.
|
||||
|
||||
*/
|
||||
public class Test02 {
|
||||
public static void main(String[] args) {
|
||||
int a = 10;
|
||||
int b = 10;
|
||||
boolean result = isEqual(a,b);
|
||||
System.out.println(result);
|
||||
|
||||
int sum = getSum(10);
|
||||
System.out.println(sum);
|
||||
|
||||
print(10);
|
||||
}
|
||||
|
||||
//方法练习3_打印n遍HelloWorld.
|
||||
public static void print(int n){
|
||||
for (int i = 0; i < n; i++) {
|
||||
System.out.println("helloworld");
|
||||
}
|
||||
}
|
||||
|
||||
//方法练习2_运算1到n的累和
|
||||
public static int getSum(int n){
|
||||
int sum = 0;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
// sum = sum + i;
|
||||
sum += i;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//方法练习1_比较两个整数是否相同
|
||||
public static boolean isEqual(int x, int y) {// 形参:x = a; y = b;
|
||||
/*if (x == y) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}*/
|
||||
return x == y;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user