Java基础 3.23

1.程序控制结构章节练习

//实现判断一个整数,属于哪个范围:大于0,小于0,等于0
import java.util.Scanner;
public class unit5_homework02 {
	public static void main(String[] args) {
		Scanner myScanner = new Scanner(System.in);
		System.out.println("请输入你要判断的数: ");
		int num = myScanner.nextInt();
		if (num > 0) {
			System.out.println("该数大于0");
		} else if (num < 0) {
			System.out.println("该数小于0");
		} else {
			System.out.println("该数等于0");
		}
	}
}
//判断一个年份是不是润年
import java.util.Scanner;
public class unit5_homework02 {
	public static void main(String[] args) {
		Scanner myScanner = new Scanner(System.in);
		System.out.println("请输入你要判断的年份: ");
		int year = myScanner.nextInt();
		if (year % 4 == 0 || year % 100 == 0) {
			System.out.println("该年份为闰年");
		} else {
			System.out.println("该年份不为闰年");
		}
	}
}
//判断一个整数是否是水仙花数,所谓水仙花数是指一个3位数,其各个位上数字立方和等于其本身
import java.util.Scanner;
public class unit5_homework02 {
	public static void main(String[] args) {
		Scanner myScanner = new Scanner(System.in);
		System.out.println("请输入你要判断的数: ");
		int num = myScanner.nextInt();
		int num1 = (num / 100) * (num /100) * (num / 100);//取百位上的数的三次方
		int num2 = (num % 100 / 10) * (num % 100 / 10) * (num % 100 / 10);//取十位数上的数的三次方
		int num3 = (num % 10) * (num % 10) * (num % 10);//取个位上的数的三次方
		if (num == num1 + num2 + num3) {
			System.out.println("该数是水仙花数");
		} else {
			System.out.println("该数不是水仙花数");
		}
	}
}
//输出1-100之间的不能被5整除的数,每5个一行
//化繁为简
//输出一个数判断是否能被5整除
//输出1-100之间的数判断
//输出1-100之间的数判断并5个一行
public class unit5_homework03 {
	public static void main(String[] args) {
		int count = 0;
		for (int num = 1; num <= 100; num++) {//1-100之间取值
			if (num % 5 != 0) {
				count++;
				System.out.print(num + "\t");
				if (count % 5 == 0) {
				System.out.println();
				}
			}
		}	
	}
}
//输出小写的a-z以及大写的Z-A
//注意使用ASCII表对应
public class unit5_homework03 {
	public static void main(String[] args) {
		for (char j = 'Z'; j >= 'A'; j--) {
			System.out.println(j);
		}
		System.out.println("------");
		for (char i = 'a'; i <= 'z'; i++) {
			System.out.println(i);
		}
	}
}
// 求出1-1/2+1/3-1/4...1/100的和
// 化繁为简
// 求出1-1/2的值
// 用循环得出式子
// 定义求和
public class unit5_homework03 {
	public static void main(String[] args) {
		double result = 0;
		double result1 = 0;
		double result2 = 0;
		for (double i = 1; i <= 100; i++) {
			if (i % 2 != 0) {
				result1 += 1.0 / i; 
			}
			if (i % 2 == 0) {
				result2 -= 1.0 / i;
			}
		}
		result = result1 + result2;
		System.out.println(result);
	}
} 
//求1+(1+2)+(1+2+3)+...(1+2+..+100)的结果
//化繁为简
//求1 + (1+2)
//循环迭代 100项相加 双层循环 
//求和
public class unit5_homework03 {
	public static void main(String[] args) {
		int count = 0;
		for (int i = 1; i <= 100; i++) {
			for (int j = 1; j <= i; j++) {
				count += j;
			}
		}
		System.out.println(count);	
	}
}

tips:编程 = 思想 ---> 代码

做上面的练习花了好多时间/(ㄒoㄒ)/~~,不过对于程序控制结构的使用变得熟练多了,也锻炼到了编程思想,再结合视频学习更为便捷的思路,这些用的时间非常有价值!o(*^▽^*)┛

2.数组

  • 数组可以存放多个同一类型的数据
  • 数组也是一种数据类型,是引用类型
  • 数组就是一组数据

数组快速入门

public class Array01 {
	public static void main(String[] args) {
		double[] hens = {1, 2, 3, 4, 5, 6};
		//遍历数组得到数组的所有元素的和,使用for循环
		//1.我们可以通过hens[下标]来访问数组的元素
		//	下标是从0开始编号的 比如第一个元素就是hens[0]
		//	第2个元素就是hens[1], 以此类推
		//2.通过for就可以循环的访问数组的元素/值
		//3.使用一个变量totalWeight将各个元素累积
		double totalWeight = 0;
		for (int i = 0; i < hens.length; i++) {
			totalWeight += hens[i];
		}
		System.out.println(totalWeight + " " +(totalWeight / hens.length));
	}

}

数组的使用

使用方式1-动态初始化

数组的定义
  • 数据类型 数组名[] = new 数据类型[大小]
  • int a[] = new int[5];创建了一个数组,名字为a,存放5个int
  • 数组的引用(使用/访问/获取数组元素)
  • 数组名[下标/索引/index] a[2] a的第3个数 从0开始编号
import java.util.Scanner;
public class Array01 {
	public static void main(String[] args) {
		double score[] = new double[5];
		//等价于double[] score = new double[5];
		Scanner myScanner = new Scanner(System.in);
		for (int i = 0; i < score.length; i++) {
			System.out.println("请输入第" + (i + 1) + "个数: ");
			score[i] = myScanner.nextDouble();
		}
		for (int i = 0; i < score.length; i++) {
			System.out.println("第" + (i + 1) + "个数为: " + score[i]);
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值