Java基础学习Day02

本文介绍了Java的基础知识,包括命名规范、变量和常量的使用,详细讲解了8个基本数据类型以及逻辑运算符。此外,还提出了若干课后作业,如类型转换、条件判断和循环计算等实践练习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

知识点
  1. Java命名规范
  2. 常量和变量
  3. 8个基本数据类型
  4. 逻辑运算符
Java命名规范
  • 标识符不能使用关键字和保留字
  • 标识符由字母、数字、下划线和dollar符号组成
  • 标识符不能以数字开头
  • 项目名最好是首字母大写
  • 包名全部小写字母
  • 类名和接口名每个单词首字母大写
  • 变量名和方法名采用驼峰命名法 第一个首字母小写
  • 常量名字母全部大写 ,每个字母之间最好用下划线连接
变量和常量

常量用final修饰 不可二次修改

8个基本数据类型
类型字节个数注意事项
byte1
short2
int4
long8变量后加L
float4变量后加F
double8
char2
boolean1只有true和false两个值
逻辑运算符

&&(短路与)
&(按位与)
||(短路或)
|(按位或)
!(非)
^(异或)

课后作业
  1. 自己尝试类型之间的转换
  2. 用if else 写一个关于成绩测评的代码
package com.day02;

import java.util.Scanner;

public class Score {
	public static void main(String[] args) {
		double score;
		Scanner sca = new Scanner(System.in);
		score = sca.nextDouble();
		
		if(score < 60) {
			System.out.println("不及格");
		}
		else if(score >= 60 && score < 70) {
			System.out.println("及格");
		}
		else if(score >= 70 && score < 80) {
			System.out.println("一般");
		}
		else if(score >= 80 && score < 90) {
			System.out.println("良好");
		}
		else {
			System.out.println("优秀");
		}
	}
}
  1. 用for循环和赋值运算求1-100的和
package com.day02;

public class Sum {
	public static void main(String[] args) {
		int sum = 0;
		for(int i = 1; i <= 100; i++) {
			sum += i;
		}
		System.out.println(sum);
	}
}
  1. 用三层for循环写1-1000以内的水仙花数
package com.day02;

public class DaffodilNumber {
	public static void main(String[] args) {
		for(int i = 1; i <= 9; i++) {
			for(int j = 0; j <= 9; j++) {
				for(int k = 0; k <= 9; k++) {
					if(i * i * i + j * j * j + k * k * k == i * 100 + j * 10 + k) {
						System.out.printf("%d\t", i * 100 + j * 10 + k);
					}
				}
			}
		}
	}
}
  1. 1-100以内前5个能被3整除的数
package com.day02;

public class Test02 {
	public static void main(String[] args) {
		int count = 0;
		for(int i = 1; i <= 100; i++) {
			if(i % 3 == 0) {
				count++;
				System.out.printf("%d\t", i);
			}
			if(count == 5) {
				break;
			}
		}
	}
}
  1. 输入三个班 每班输入10个学生成绩 算每个班总成绩和平均成绩
package com.day02;

import java.util.Scanner;

public class Test03 {
	public static void main(String[] args) {
		Scanner sca = new Scanner(System.in);
		int num;
		
		double sum = 0;
		double score;
		for(int i = 0; i < 3; i++) {
			num = sca.nextInt();
			
			for(int j = 0; j < 10; j++) {
				score = sca.nextDouble();
				sum += score;
			}
			System.out.println(num + "班" + sum + "\t" + sum / 10);
		}	
	}
}
  1. 计算2000年1月1到2008年1月1总共多少天?
package com.day02;

import java.time.Year;

public class Test04 {
	public static void main(String[] args) {
		int sum = 0;
		for(int year = 2000; year < 2008; year++) {
			if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
				sum += 366;
			}
      else {
        sum += 365;
      }
		}
		System.out.println(sum);
	}
}
  1. 循环输入大于0的数字进行累加,直到输入的数字为0,就结束循环,并最后输出累加的结果
package com.day02;

import java.util.Scanner;

public class Test05 {
	public static void main(String[] args) {
		Scanner sca = new Scanner(System.in);
		
		int n;
		int sum = 0;
		while(true) {
			n = sca.nextInt();
			sum += n;
			if(n == 0) {
				break;
			}
		}
		System.out.println(sum);
	}
}
  1. 计算用户输入的日期离1900年1月1日相距多少天
package com.day02;

import java.util.Scanner;

public class Test06 {
	public static void main(String[] args) {
		Scanner sca = new Scanner(System.in);
		int year;
		int month;
		int day;
		int sum = 0;
		
		year = sca.nextInt();
		month = sca.nextInt();
		day = sca.nextInt();
		
		for(int i = 1900; i < year; i++) {
			if(i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
				sum += 366;
			}
			else {
				sum += 365;
			}
		}
		
    //这里最好用数组写
		for(int i = 1; i < month; i++) {
			switch(i) {
			case 1: 
				sum += 31;
				break;
			case 2: 
				if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
						sum += 29;
				}
				else {
					sum += 28;
				}
				break;
			case 3: 
				sum += 31;
				break;
			case 4: 
				sum += 30;
				break;
			case 5: 
				sum += 31;
				break;
			case 6: 
				sum += 30;
				break;
			case 7: 
				sum += 31;
				break;
			case 8: 
				sum += 31;
				break;
			case 9: 
				sum += 30;
				break;
			case 10: 
				sum += 31;
				break;
			case 11: 
				sum += 30;
				break;
			case 12: 
				sum += 31;
				break;
			}
		}
		sum += day;
		System.out.println(sum - 1);
	}
}
  1. 生日问候
    请输入今天的日期(月/日<用两位数表示:): 05/29
    请输入客户生日:08/09
    然后判断是否今天生日
    是否继续(y/n)
    如果是y就重复以上步骤,否则退出程序
package com.day02;

import java.time.Year;
import java.util.Scanner;

public class Test07 {
	public static void main(String[] args) {
		boolean flage = true;
		String str1;
		String str2;
		Scanner sca = new Scanner(System.in);
		
		while(flage) {
			System.out.println("请输入今天的日期(月/日<用两位数表示:):");
			str1 = sca.next();
			System.out.println("请输入客户生日:");
			str2 = sca.next();
			System.out.println();
			if(str1.equals(str2)) {
				System.out.println("今天生日");
			}
			else {
				System.out.println("今天不生日");
			}
			System.out.println("是否继续(y/n) ");
			if("y".equals(sca.next())) {
				flage = true;
			}
			else {
				flage = false;
			}
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值