moka同学java学习笔记
package moka.hello;
public class HelloWorld {
public static void main(String[] args){
//初始java
int a = 1;
int b = 3;
int sum = a+b;
//if语句
if(a<b){
System.out.println(sum);
}else{
System.out.println("我要学java!"+sum);
}
//for循环语句
for(int i=1;i<sum;i++){
System.out.println(a+sum);
}
//运算表达式
System.out.println((11+3*8)/4%3);
//if多重语句
int age =70;
if(age>=60){
System.out.println("老年人");
}else if(40 <= age && age <60){
System.out.println("中年");
}else if(18 <= age && age < 40){
System.out.println("少年");
}else{
System.out.println("儿童");
}
//if语句嵌套[要求:如果是周末晴天逛西湖,阴天室内玩,周内去上班]
String today="周末";
String wether="晴朗";
if(today.equals("周末")){
if(wether.equals("晴朗")){
System.out.println("来杭州逛西湖!:):)");
}else{
System.out.println("室内玩桌游!");
}
}else{
System.out.println("上班,来不了。:(");
}
//if嵌套[要求:预赛成绩大于80分的进决赛,然后根据性别再划分男子组决赛和女子组决赛]
int score = 94;
String sex = "女";
if(score >= 80){
if(sex.equals("女")){
System.out.println("女子组决赛");
}else{
System.out.println("男子组决赛");
}
}
//switch语句[根据考试的名次,给予前4名不同奖品,笔记本,IPAD2,移动电源,U盘]
int rank = 3;
switch(rank){
case 1:
System.out.println("第一名,奖励笔记本");
break;
case 2:
System.out.println("第二名,奖励IPAD");
break;
case 3:
System.out.println("第三名,奖励移动电源");
break;
default:
System.out.println("其他,奖励U盘");
}
//switch练习[如果是周一、三、五、早餐包子,二四六油条,周日主席套餐]
char nowday=‘日‘;
String x="吃包子";
String y="吃油条";
String z="吃主席套餐";
switch(nowday){
case ‘一‘:
case ‘三‘:
case ‘五‘:
System.out.println(x);
break;
case ‘二‘:
case ‘四‘:
case ‘六‘:
System.out.println(y);
break;
default:
System.out.println(z);
}
//while 循环[先判断,后执行]
int i = 1;
while(i<=10){
System.out.println(i+"我在学java");
i++;
}
for(i=1;i<=5;i++){
System.out.println(i+"For循环");
}
//do while 循环[先执行,后判断---》输出5次]
int j =1;
do{
System.out.println("我要学java");
j++;
}while(j<=5);
// do while 计算50以内偶数的和
int sum1 = 0;
int num = 2;
do{
sum1 += num;
num = num + 2;
}while(num<=50);
System.out.println("50以内的偶数之和:"+sum1);
//[练习: 加法运算式]
for(int a1=1,b1=5;a1<=b1;a1++,b1--){
System.out.println(a1+"+"+b1+"="+(a1+b1));
}
//[练习:计算1到100之间不能被3整除的数之和]
int sum3 =0;
for(int i3=1;i3<=100;i3++){
if(i3 % 3 !=0){
sum3 = sum3+i3;
}
}
System.out.println("1-100不能被3整除的数之和:"+sum3);
}
}