2020-08-31(循环有关练习)

package demo;

public class Demo {
	public static void main(String[] args) {
		short sum = 0;
		short i = 0;
		for (i = 0; i < 10; i++) {
			//sum = sum + i;错误:类型不匹配:不能将int转换为short
			//错误原因是因为 i++; 把short的i  隐式转换为int类型了;
			sum += i;  // += 是一个赋值,而不是运算,一般这种不报错,(面试注意)
		}
		System.out.println(sum);
	}
}
package demo;
/**
 * 在控制台输入用户名和密码,
 * 判断输入的是不是用户名张三,密码123456,
 * 如果是,打印登陆成功,如果不是,打印登录失败
 */
import java.util.Scanner;

public class JudgeZhangSan {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入用户名:");
		String name = sc.next();
		System.out.println("请输入密码:");
		int password = sc.nextInt();
		
		if(name.equals("张三")&&password == 123456) {
			//String字符串要用equals函数,不能用==符号。
			System.out.println("登陆成功");
		}else {
			System.out.println("登陆失败");
		}
		sc.close();
	}
}
package demo;
/**
 * 乘法口诀表
 * @author WDD
 *
 */
public class MultiplicationTable {
	public static void main(String[] args) {
		for (int i = 1; i <= 9; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print(j+"x"+i+"="+(i*j)+" ");
			}
			System.out.println();
		}
	}

}
package demo;
/**
 * 猜数游戏:
 * 后台随机生成1-100的整数:int a = (int) (Math.random()*100+1);
 * 然后用户输入一个数,
 * 如果猜大了,就输出猜大了;
 * 如果猜小了,就输出猜小了;
 * 如果猜对了,就输出猜对了。
 * 并且记录用户输入的次数,
 * 如果输入次数小于3,输出优秀;
 * 如果输入次数大于等于3并且小于7,输出还不错;
 * 如果输入次数大于7,输出继续努力;
 */

import java.util.Scanner;

public class RandomNumberDemo {
	public static void main(String[] args) {
		int a = (int) (Math.random()*100+1);
		System.out.println(a);
		Scanner sc = new Scanner(System.in);
		int b,c=0;
		do {
			System.out.println("请输入你要猜测的数(1-100):");
			b = sc.nextInt();
			if(a > b) {
				System.out.println("你猜小了...");
			}else if(a < b) {
				System.out.println("你猜大了...");
			}else {
				System.out.println("你猜对了!");
			}
			c++;
		} while (a != b);
		System.out.println("你猜测了"+c+"次.");
		if(c < 3) {
			System.out.println("优秀!");
		}else if(c >= 3 && c < 7) {
			System.out.println("还不错哦~");
		}else {
			System.out.println("继续努力...");
		}
		sc.close();
	}

}
package demo;

import java.util.Scanner;

/**
 * *
 * **
 * ***
 * .....
 * 打印上面的图形    根据用户输入m行打印
 * 拓展到乘法口诀表
 * @author WDD
 *
 */

public class StarsDemo {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入要打印的星星的行数:");
		int m = sc.nextInt();
		for (int i = 1; i <= m; i++) {
			for (int j = 0; j < i; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
		sc.close();
	}

}
package demo;

import java.util.Scanner;

/**
 *      *
 *     ***
 *    *****
 *   *******
 *   输出如上的*阵
 * @author WDD
 *
 */
public class StarsDemo2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入要打印的星星的行数:");
		int n = sc.nextInt();
		for (int i = 1; i <= n; i++) {
			for (int j = n-i; j > 0; j--) {
				System.out.print(" ");
			}
			for (int k = 1; k <= 2*i-1; k++) {
				System.out.print("*");
				
			}
			System.out.println();
		}
		sc.close();
	}
}
package demo;

import java.util.Scanner;

/**
 * 输入年月,默认为1号,计算距离1900.1.1多少天,
 * 计算出这个月1号为星期几,
 * 星期几前面就输出几个空格(最多6个)
 * 每输出7次(这里面包括空格和数字日期)就换行输出
 * 在决定这个月是多少天
 * 
 * @author WDD
 *
 */
public class CalendarDemo {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入要查询的年份:");
		int year = sc.nextInt();
		System.out.println("请输入要查询的月份:");
		int month = sc.nextInt();
		int num = 0;  //总共有多少天
		
		//计算年
		for (int i = 1900; i < year; i++) {
			if((i%4==0&&i%100!=0)||i%400==0) {
				num += 366;
			}else {
				num += 365;
			}
		}
		
		//计算月
		for (int i = 1; i < month; i++) {
			switch (i) {
			case 2:
				if((year%4==0&&year%100!=0)||year%400==0) {
					num += 29;
				}else {
					num += 28;
				}
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				num += 30;
				break;
			default:
				num += 31;
				break;
			}
		}
		int week = (num+1)%7;
		System.out.println(year+"年"+month+"月1日是星期"+week);
		System.out.println("********************欢迎来到万年历*****************************");
		System.out.println("星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期日\t");
		
		//根据星期几打印空格占位
		if(week == 0) {
			for (int k = 1; k < 7; k++) {
				System.out.print("\t");
			}
		} else {
			for (int k = 1; k < week; k++) {
				System.out.print("\t");
			}
		}
		
		//计算输出的这个月有多少天?
		int n;
		switch (month) {
		case 2:
			if((year%4==0&&year%100!=0)||year%400==0) {
				n = 29;
			}else {
				n = 28;
			}
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			n = 30;
			break;
		default:
			n = 31;
			break;
		}
		//打印这个月的n天
		for (int j = 1; j <= n; j++) {
			//num++;
			if (++num%7==0) {   //这个num没有算上了month中1号那一天,上面没有+1了
				System.out.print(j+"\n");
			}else {
			System.out.print(j+"\t");
			}
		}
		
		sc.close();
	}
}

package demo;

import java.util.Scanner;

public class CalendarDemo2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入要查询的年份:");
		int year = sc.nextInt();
		System.out.println("请输入要查询的月份:");
		int month = sc.nextInt();
		//计算距离1900.1.1多久
		int num = 0;
		//计算年
		for (int i = 1900; i < year; i++) {
			if((i%4==0&&i%100!=0)||i%400==0) {
				num += 366;
			}else {
				num += 365;
			}
		}
		//计算月
		for (int i = 1; i < month; i++) {
			switch (i) {
			case 2:
				if((year%4==0&&year%100!=0)||year%400==0) {
					num += 29;
				}else {
					num += 28;
				}
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				num += 30;
				break;

			default:
				num += 31;
				break;
			}
		}
		//距离总天数为:
		//num++;   //注意这个
		
		//这个月的第一天是星期几?week
		int week = ++num%7;
		
		//计算出这个月有n天
		int monthDay = 0;

		switch (month) {
		case 2:
			if((year%4==0&&year%100!=0)||year%400==0) {
				monthDay = 29;
			}else {
				monthDay = 28;
			}
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			monthDay = 30;
			break;

		default:
			monthDay = 31;
			break;
		}
		System.out.println("*******************************************");
		System.out.println("星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期日\t");
		
		//如果这个月第一天是星期日,那么oneDayWeek == 0; 上面占位for循环无法执行,加if
		if(week == 0) {
			for (int i = 1; i < 7; i++) {
				System.out.print(" "+"\t");
			}
		}else {
			for (int i = 1; i < week; i++) {
				System.out.print(" "+"\t");
			}
		}
		for (int i = 1; i <= monthDay; i++) {
			//num++;
			if(++num%7==1) {   //这个num算上了month中1号那一天,上面+1了
				System.out.print(i+"\n");
			} else {
				System.out.print(i+"\t");
			}
		}
		
		
		sc.close();
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值