break
| ||||
case
| const | |||
continue | default | do | double | else |
enum | extends | final | finally | float |
for | goto | if | implements | import |
instanceof | int | interface | long | native |
new | package | private | protected | public |
return | strictfp | short | static | super |
switch | synchronized | this | throw | throws |
transient | try | void | volatile | while |
不常用的(不能用的)(go to,const)
JAVA基本数据类型 1.整形byte(1)(-128~127),short(2),int(4)注意public class Test{
public static void main(String [] args){
short b = 32000;
long i = 1243253454456L;
System.out.println(b);
System.out.println(i);
}
},long(8)
2.浮点型float(4),double(8)注意默认的浮点数类型是double类型
short b = 32000;
long i = 1243253454456L;
float f = 1.24F;
double d = 33.44;
System.out.println(b);
System.out.println(i);
System.out.println(f);
//浮点数的运算会有问题...现在尽量避免
System.out.println(0.6 - 0.2);
JAVA流程控制。1.if...else if..else例子://学校考试,某个学生考了70分,>=90 优秀 >=80良好 >=60及格 否则就是差
//if判断中得到的始终都是boolean类型的值
//比较判断
int a = 91;
/*if(a >= 90){
System.out.println("优秀");
}
if(a >= 80 && a < 90){
System.out.println("良好");
}
if( a >= 60 && a < 80){
System.out.println("及格");
}
if( a < 60){
System.out.println("差");
}*/
/*if( a >= 90){
System.out.println("优秀");
}
else if( a >= 80){
System.out.println("良好");
}
else if( a >= 60){
System.out.println("及格");
}
else{
System.out.println("差");
}*/
//我现在有存款52w,
//如果我有500w,我就买法拉利
//如果我有100w,我就买卡迪拉克
//如果我有50w,我就买宝马3
//如果我有10w,我就买奥拓
//否则,就骑共享单车
//Scanner需要导入包java.util.Scanner
//java为了方便,将常用的类和基本类型放入到了java.lang包中
//而我们写代码的时候,默认就在java.lang中
//当然如果不在java.lang中的类,就必须导入包,通知程序在那里去找到这个类
Scanner input = new Scanner(System.in);
// System.out.println("请输入你的名字:");
// String n = input.next();
/*System.out.println("请输入你的存款:");
int n = input.nextInt();
if( n >= 500){
System.out.println("随便买什么车");
}else if(n >= 200){
System.out.println("买法拉利");
}else if(n >= 50){
System.out.println("买宝马3");
}else if(n >= 10){
System.out.println("买奥拓");
}else{
System.out.println("骑摩拜...");
}*/
//输入会员积分,积分<=2000,购买商品打9折
//积分>2000并且<=4000 打8折
//积分>4000并且<=10000 打7折
//积分>10000 打6折
//输入购买金额,得到应付金额
/*System.out.println("请输入你的积分:");
int jifen = input.nextInt();
System.out.println("请输入你的购买金额");
double money = input.nextDouble();
double discount = 0.0;
if(jifen > 10000){
discount = 0.6;
}else if(jifen > 4000){
discount = 0.7;
}else if(jifen > 2000){
discount = 0.8;
}else{
discount = 0.9;
}
double disMoney = money * discount;
System.out.println("你应付款" + money + ",你的积分是:"+jifen+",应该享用的折扣是:"+discount+",折扣之后应该支付:" + disMoney);*/
//System.out.println(Math.random());
//Math.random()得到的是[0,1)之间的随机浮点数
//int n = (int)(Math.random() * 11);(0-10的随机数)
//System.out.println(n);
//随机数公式
//(Math.random() * (max - min + 1) + min);
//1-3的随机数
// int n = (int)(Math.random()*3 + 1);
// System.out.println(n);
//商场幸运抽奖 顾客卡号的百位数如果等于幸运号码,那么顾客就中奖,顾客的卡号都是4位数
//请输入顾客的卡号,并生成一个随机幸运号码,判断该顾客是否中奖
/*System.out.println("请输入顾客卡号");
int cardNum = input.nextInt();
//生成幸运号码
int luckyNum = (int)(Math.random() * 10);
System.out.println("幸运号码是:" + luckyNum);
//分解获得百位
int baiwei = cardNum / 100 % 10;
if(baiwei == luckyNum){
System.out.println("中奖了");
}else{
System.out.println("再接再厉");
}*/
//if嵌套
//学校进行短跑比赛,成绩小10秒进入决赛,根据性别分别进入男子组和女子组
/*System.out.println("请输入你的成绩");
double n = input.nextDouble();
System.out.println("请输入你的性别");
String sex = input.next();
if(n <= 10){
//注意字符串比较不能用==去比较是否相等
//只能用字符串.equals(另外一个字符串);
if(sex.equals("男")){
System.out.println("进入男子组");
}else{
System.out.println("进入女子组");
}
}else{
System.out.println("你已经被淘汰了...");
}*/
// System.out.println("请输入你的名字:");
// String name = input.next();
//
// if(name.equalsIgnoreCase("lucy")){
// System.out.println("你的名字真洋气");
// }else{
// System.out.println("你的名字好土");
// }
// equalsIgnoreCase 忽略大小写的比较
//商场购买物品会有折扣
//会员 >= 200以上 75折,其他金额 8折
//非会员 消费在100以上 9折
/*System.out.println("请输入你消费的金额");
double money = input.nextDouble();
System.out.println("请问,你是会员吗?(y/n)");
String judge = input.next();
if(judge.equalsIgnoreCase("y")){
if( money >= 200){
money = money * 0.75;
}else{
money *= 0.8;
}
}else{
if(money >= 100){
money = money * 0.9;
}
}
System.out.println("实际支付:" + money);*/
//机场机票打折 机票原价4000
//其中(5-10)旺季 头等舱9折,经济舱75折
//淡季 头等舱6折,经济舱3折
//应该付多少机票钱
/*int price = 4000;
int month;
int type; //1是头等舱 2经济舱
System.out.println("请输入你出行的月份(1-12)");
month = input.nextInt();
System.out.println("选择头等舱还是经济舱 1.头等舱 2.经济舱");
type = input.nextInt();
if(month >= 5 && month <= 10){
if(type == 1){
price *= 0.9;
}else{
price *= 0.75;
}
}else{
if(type == 1){
price *= 0.6;
}else{
price *= 0.3;
}
}
System.out.println("你的机票的价格是:" + price );*/
//随便输入一个年份,输出该年份2月的天数
System.out.println("请输入年份");
int year = input.nextInt();
boolean flag = false;
//闰年能被4整除不能被100整除,或者能被400整除
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
flag = true;
}
if(flag){
System.out.println("2月有29天");
}else{
System.out.println("2月有28天");
}
2.switch例子:Scanner input = new Scanner(System.in);
System.out.println("请输入你的名次:");
int n = input.nextInt();
/*if(n == 1){
System.out.println("------");
}
else if(n == 2){
System.out.println("=======");
}
else if(n == 3){
System.out.println("********");
}else{
System.out.println("########");
}*/
//switch语句其实都可以通过if...else语句进行替代
//只是在做等值比较的时候用switch语句比较清晰
//1.注意break的问题
//如果不写break,会发生穿透
//2.在java 6 之前的版本switch()中的表达式只能是byte,short,int,char
//在java7以后的版本中,可以使用String类型了
//建议大家还是不要在switch表达式中使用String类型
/*switch(n){
case 1:
System.out.println("奖励美国夏令营一个月");
//break;
case 2:
System.out.println("奖励苹果手机");
//break;
case 3:
System.out.println("奖励小米手机");
//break;
default:
System.out.println("奖励认真学习一个月");
//break;
}*/
//通过switch 的穿透特性来得到每个月的天数
/*System.out.println("请输入年份:");
int year = input.nextInt();
System.out.println("请输入月份:");
int month = input.nextInt();
boolean flag = false;
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
flag = true;
}
int monthDay = 0; //记录天数
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
monthDay = 31;
break;
case 2:
if(flag){
monthDay = 29;
}else{
monthDay = 28;
}
break;
default:
monthDay = 30;
break;
}
System.out.println(year + "年," + month + "月,有" + monthDay + "天");
*/
//商场进行换购活动
//消费金额 >= 50 就可以参加活动
//满50 +2 换购一瓶可乐
//满100 +3 换购一瓶500ML可乐
//满100 +10 换购5KG面粉
//满200 +10 换购 名牌炒锅
//满200 +20 换购 欧莱雅爽肤水一瓶
//当然也可以不换购
//打印总花费与换购的商品
System.out.println("请输入消费金额:");
double money = input.nextDouble();
double extra = 0;
if(money >= 50){
System.out.println("参加换购活动:");
System.out.println("1.满50 +2 换购一瓶可乐");
System.out.println("2.满100 +3 换购一瓶500ML可乐");
System.out.println("3.满100 +10 换购5KG面粉");
System.out.println("4.满200 +10 换购名牌炒锅");
System.out.println("5.满200 +20 换购欧莱雅爽肤水");
System.out.println("0.不换购");
System.out.println("请选择:(0-5)");
int choice = input.nextInt();
String str = "";
switch(choice){
case 1:
if(money >= 50){
extra = 2;
str = "换购百事可乐一瓶";
}
break;
case 2:
if(money >= 100){
extra = 3;
str = "换购500ML百事可乐一瓶";
}
break;
case 3:
if(money >= 100){
extra = 10;
str = "换购5KG面粉";
}
break;
case 4:
if(money >= 200){
extra = 10;
str = "换购名牌炒锅";
}
break;
case 5:
if(money >= 200){
extra = 20;
str = "换购爽肤水";
}
break;
default:
extra = 0;
break;
}
//结账
double total = money + extra;
System.out.println("你本次消费的总金额是:" + total +","+ str);
}else{
System.out.println("你购买金额是:" + money);
}
3循环for ,while, do...while