1.判断输入数据的奇偶性
package com.atxiaoyao.method;
import java.util.Scanner;
public class Test02 {
public static void main(String[] args) {
// 方法练习:判断输入数据的奇偶性
method01();
}
public static void method01(){
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
if (a%2 == 0){
System.out.println("输入的数值" + a + "是偶数");
}else {
System.out.println("输入的数值" + a + "是奇数");
}
}
}
2.练习n个连续的数相加求和 0-100
package com.atxiaoyao.method;
public class Test03 {
public static void main(String[] args) {
// 方法练习:练习n个连续的数相加求和 0-100
method03();
}
public static void method03(){
int sum = 0;
for (int i = 0; i <= 100; i++) {
sum+=i;
}
System.out.println("0-100的和为: " + sum);
}
}
3.求一个数组中的最大值
package com.atxiaoyao.method;
public class Test04 {
public static void main(String[] args) {
// 求一个数组中的最大值
int[] arr = {2,5,7,3,5,23,4,56,78,54,63,45,67,98,12};
int max = arr[0];
for (int i = 0; i < arr.length; i++) {
if (max > arr[i] ){
max = max;
}else {
max = arr[i];
}
}
System.out.println("数组中的最大值为:" + max);
}
}