for循环
循环三要素:循环的初始条件 循环的判断条件 循环的改变条件
#关键字:break和continue
break:表示中断整个循环;
continue:表示中断此次循环,继续执行下次循环,直至循环结束;
例:1、求出一到十中偶数的和
public class Test1 {
//求1~10的和偶数和
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i <= 10; i++) {
if (i%2==0){
sum = sum + i;
}
}
System.out.println(sum);
2、求阶层
import java.util.Scanner;
public class Test2 {
// 求阶层
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你需要求的数的阶层:");
int key = sc.nextInt();
int sum = 1;
for (int i = key; i >= 1; i--) {
sum = sum * i;
}
System.out.println("i!为:"+sum);
}
}
3、求三位数的水仙花数
拓展
水仙花数只是自幂数的一种,严格来说3位数的3次幂数才称为水仙花数。
附:其他位数的自幂数名字
一位自幂数:独身数
三位自幂数:水仙花数
四位自幂数:四叶玫瑰数
五位自幂数:五角星数
六位自幂数:六合数
七位自幂数:北斗七星数
八位自幂数:八仙数
九位自幂数:九九重阳数
十位自幂数:十全十美数
代码
//求三位数的水仙花数:
public class Test3 {
public static void main(String[] args) {
for (int number = 100; number < 1000; number++) {
int ge = number%10; //取得个位数字
int shi = number/10%10; //取得十位数字
int bai = number/100%10; //取得百位数字
int total = ge*ge*ge + shi*shi*shi + bai*bai*bai;
if (total == number){
System.out.println(number);
}else{
continue;//结束此次循环,开始下一次循环
}
}
}
}
4、鸡兔同笼
public class Test4 {
//鸡兔同笼问题
// 现有一笼子,有若干鸡和兔,14个头,38条腿,问鸡兔各多少只;
public static void main(String[] args) {
for (int i = 0; i <= 14; i++) {
int total = i*2 + (14-i)*4;
if (total == 38){
System.out.println("鸡有"+i+"只");
System.out.println("兔有"+(14-i)+"只");
}
}
}
}
5、日历问题
方法一:
import java.util.Scanner;
public class Test5 {
//用户输入一个正确的年和月,打印输出对应的日历信息
// 分析:若打印日历,需两个条件;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入年份:");
int year = input.nextInt();
System.out.println("请输入月份:");
int month = input.nextInt();
//2.算年的总天数[1900,2023)年的天数
int sum1 = 0;//保存年的总天数
for (int i = 1900; i < year; i++) {
if(i%4==0&&i%100!=0||i%400==0){
sum1 += 366;
}else{
sum1 += 365;
}
}
System.out.println(sum1);//2023--44925
//3.算月的总天数[1,9)月的天数
int sum2 = 0;
for (int i = 1; i < month; i++) {
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12){
sum2 += 31;
}else if(i==2){
if(year%4==0&&year%100!=0||year%400==0){
sum2 += 29;
}else{
sum2 += 28;
}
}else{
sum2 += 30;
}
}
int index = (1+sum1+sum2)%7;
System.out.println(index);
//
int days ;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
days = 31;
}else if(month==2){
if(year%4==0&&year%100!=0||year%400==0){
days = 29;
}else{
days = 28;
}
}else{
days = 30;
}
//6.可以打印日历
System.out.println("日\t一\t二\t三\t四\t五\t六");
for (int i = 1;i <= index;i++){
System.out.print("\t");
}
for (int i = 1; i <= days; i++) {
System.out.print(i);
if((i+index)%7==0){
System.out.print("\n");
}else{
System.out.print("\t");
}
}
}
}
方法二:
import java.util.Scanner;
public class Test5_2 {
// 日历问题:用户输入年,月;
// 打印出对应年月的信息,包括日期对应的星期(即打印出当月月历)
public static void main(String[] args) {
// 解析:日期拓展知识:日历从1900年一月一日开始,为周一
// 闰年366天,平年355天;4年为一闰,百年不闰,400年一闰
// 1、用户输入年份与月份;
// 2、计算用户输入年份与月份1日到1900年1月1日共经历多少天;
// 3、算出该月份1日对应周几,后打印出对应月历表;
//第一步:用户输入年份与月份;
Scanner sc = new Scanner(System.in);
System.out.println("请驶入年份:");
int year = sc.nextInt();
System.out.println("请输入月份:");
int moon = sc.nextInt();
//第二步:计算用户输入年份与月份1日到1900年1月1日共经历多少天;
int days = 0; //记录输入年月至1900年1月1日总天数
for (int i = 1900; i < year; i++) {
if (JudgeingYear(i)){
days = days + 366;
}else{
days = days + 365;
}
}
// System.out.println(days);
for (int i = 1; i < moon; i++) {
days = days + JudgeingMoon(year, i);
}
// System.out.println(days);
// 第三步:算出该月份1日对应周几,后打印出对应月历表;
int week1;
week1 = (days+1)%7;
System.out.println("周一\t周二\t周三\t周四\t周五\t周六\t周天\t");
for (int i = 1; i < week1; i++) {
System.out.print("\t");
}
for (int i = 1; i <= JudgeingMoon(year,moon); i++) {
System.out.print(i);
if ((i+week1-1)%7==0){
System.out.print("\n");
}else{
System.out.print("\t");
}
}
}
public static int JudgeingMoon(int year, int i) {//判断月份天数
int day = 0;
if (JudgeingYear(year)){
switch (i){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 4:
case 6:
case 9:
case 11:
day = 30;
break;
case 2:
day = 29;
break;
}
}else{
switch (i){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 4:
case 6:
case 9:
case 11:
day = 30;
break;
case 2:
day = 28;
break;
}
}
return day;
}
//判断闰年与平年
public static boolean JudgeingYear(int year) {
boolean result;
if (year%4==0&&year%100!=0||year%400==0){
result = true;
}else{
result = false;
}
return result;
}
}