Java编程中常用到的编程思想
public class SumTest{ public static void main(String[] args) { //求1-100(包含1和100)的数字总和 int sum = 0; for (int i = 1; i <= 100; i++) { sum += i ; } System.out.println("sum = " + sum); } }
public class CountTest{ public static void main(String[] args) { //统计1-100之间(包含1和100)的偶数出现的次数 int count = 0; for (int i = 1; i <= 100; i++) { if (i%2==0) { count ++; } } System.out.println("count = " + count); } }
public class TempTest{ public static void main(String[] args) { //交换变量a和b的值 int a = 10; int b = 20; int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b); } }
public class HelloWorld { public static void main(String[] args) { //数组反转 //1.定义两个变量 //2.start表示小索引 //3.end表示大索引 //4.小索引和大索引交换 //5.小索引++,大索引-- //6.小索引<大索引交换位置 //7.小索引>=大索引停止交换 int[] arr = {19, 28, 37, 46, 50}; int start = 0; int end = arr.length-1; int temp = 0; while(start<end){ temp = arr[start]; arr[start]=arr[end]; arr[end]=temp; start++; end--; } System.out.println(Arrays.toString(arr)); } }