else
else 已经包括代码行除外的的区间了,不用再次判断month >= 6 ,合理的话也不用判断month >= 3
题目:判断月份在第几季度
if (month >= 1 && month <= 3){
System.out.println(month + "月在第1季度");
}else if (month >= 4 && month <= 6){
System.out.println(month + "月在第2季度");
}else if (month >= 7 && month <= 9){
System.out.println(month + "月在第3季度");
}else if (month >= 10 && month <= 12)){
System.out.println(month + "月在第4季度");
}else {
System.out.println("没有这个月份,请重新输入!");
}
更改之后:
if (month >= 1 && month <= 12){
if (month <= 3){
System.out.println(month + "月在第1季度");
}else if (month <= 6){
System.out.println(month + "月在第2季度");
}else if (month <= 9){
System.out.println(month + "月在第3季度");
}else {
System.out.println(month + "月在第4季度");
}
}else {
System.out.println("没有这个月份,请重新输入!");
}
指数
^ 这个在java里面是异或不是指数
显示文件所在的地方
右键点击类,点击Show in Explorer(Idea为例)
跳出文件夹
质数
质数: 只能1和它自己本身整除,其他找不到其他除数。
比如:16,找他是否是质数,除数从2开始找,1是绝对可以整除,所以不用,从2开始,到15
比如:25 2-25
规律: 我们需要遍历我们的每一个数,看它能否被2~(x-1)之间的数整除。
public class Demo01 {
public static void main(String[] args) {
int count =0;//代码找到整数的数量也就是合数的数量
for (int i = 1; i <= 100; i++) {
count = 0;//重新计数器
for (int j = 2; j < i; j++) {
//判断i能否整除j
if (i % j == 0){//找到整除的情况,那么说明能i不是质数
count++;//那我们可以给一个标记
}
}
if (count == 0){
System.out.print(i);
}
}
}
}
外面的for是遍历100以内的数,里面的for找里面2~(x-1)的数
continue
跳出当次循环,countinue后面的语句不执行了
public class Demo02 {
public static void main(String[] args) {
//打印出1-10的数,但4不输出
for (int i = 1; i < 11; i++) {
if (i == 4){
continue;//回头。不会往下执行
}
System.out.println(i);
}
}
}
break
跳出当层循环,跟continue不一样
public class Demo03 {
public static void main(String[] args) {
//打印1-10的数,到数字4跳出循环
for (int i = 1; i < 11; i++) {
System.out.println(i);
if (i == 4){
break;//退出循环
}
}
System.out.println("over")
}
}
Debug
- Debug 调试、消除bug ——臭虫
- 断点:就是在编辑器中设置一个代码执行停留的位置,单机或者双击代码左边
- F5:进入方法(方法里面没有断点,在方法调用的时候断点,按F5能够进入)
- F6:逐步执行代码,按一下执行一步(不一定是一行)
- F7:退出当前代码的方法
- F8:跳到下一个断点处(在循环里面的话,下一个断点处还在循环里面),如果没有下一个断点,则执行到程序结束