运算符及程序控制结构(if-else,switch,双重for循环,while,do-while)的学习。
练习部分代码:
import java.util.Scanner;
public class jisuan {
public static void main(String[] args){
//位移计算
Scanner scanner = new Scanner(System.in);
System.out.println("请输入时间t:");
int t = scanner.nextInt();
double x = 0.5 * 9.8 * t * t;
System.out.println("位移为:" + x);
//运算符
int aa=3;
int bb=5;
System.out.println("aa+bb="+ (aa+bb));
System.out.println("aa-bb="+ (aa-bb));
System.out.println("aa*bb="+ (aa*bb));
System.out.println("aa/bb="+ (aa/bb));
System.out.println("aa%bb="+ (aa%bb));
int c=aa++;
int d=++aa;
System.out.println("d="+d);
System.out.println("请输入一个数");
Scanner scanner1=new Scanner(System.in);
int number=scanner.nextInt();
String s=number > 0 ? "你输入的是一个正数":number==0 ?"你输入的是0":"你输入的使用个负数";
System.out.println(s);
//正数逆序输出
System.out.println("请输入一个三位数的正数");
Scanner scanner2=new Scanner(System.in);
int number1=scanner.nextInt();
int ones = number1 % 10;
int tens = (number1 / 10) % 10;
int hundred = (number1 / 100) % 10;
int reverse = ones * 100 + tens * 10 + hundred;
System.out.println("逆序处理后的结果为:" + reverse);
//两个数求最大值
Scanner scanner3=new Scanner(System.in);
Scanner scanner4=new Scanner(System.in);
System.out.println("请输入第一个数:");
int e=scanner.nextInt();
System.out.println("请输入第二个数:");
int f=scanner.nextInt();
int max=e;
if(e<f){
max=f;
}
System.out.println("最大值为:"+max);
//if-else语句(判断正负数)
System.out.println("请输入一个整数");
Scanner scanner5=new Scanner(System.in);
int ss=scanner.nextInt();
if(ss>0){
System.out.println("该整数为正数");
}else if(ss<0){
System.out.println("该整数为负数");
}
//if-else语句(根据成绩判断学校阶级)
System.out.println("请输入高考成绩:");
Scanner scanner6=new Scanner(System.in);
int score=scanner6.nextInt();
if(score>=650){
System.out.println("清北");
}else if(score>=550&&score<650){
System.out.println("985");
}else if(score>=450&&score<550){
System.out.println("211");
}else if(score>=350&&score<450){
System.out.println("不知名");
}else if(score<350){
System.out.println("无");
}
//身份信息验证
Scanner scanner7 = new Scanner(System.in);
System.out.println("请输入您的身份信息(军人、学生、普通人):");
String identity = scanner7.nextLine();
if (identity.equals("军人")) {
System.out.println("您是军人,享受免票待遇。");
} else if (identity.equals("学生")) {
System.out.println("您是学生,享受半价待遇。");
} else if (identity.equals("普通人")) {
System.out.println("您是普通人,需要原价购买。");
} else {
System.out.println("身份信息输入有误,请重新输入。");
}
//判断业绩
Scanner scanner8 = new Scanner(System.in);
System.out.println("请输入您的业绩排名:");
int rank = scanner8.nextInt();
switch (rank) {
case 1:
System.out.println("排名第一,奖励欧洲游。");
break;
case 2:
System.out.println("排名第二,奖励亚洲游。");
break;
case 3:
System.out.println("排名第三,奖励国内游。");
break;
case 4:
System.out.println("排名第四,奖励省内游。");
break;
default:
System.out.println("加班。");
break;
}
//根据月份判断天数
Scanner scanner9 = new Scanner(System.in);
System.out.println("请输入月份(1-12):");
int month = scanner9.nextInt();
int days;
switch (month) {
case 2:
days = 28;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
default:
days = 31;
break;
}
System.out.println("该月份有" + days + "天。");
//输出1-200之间的偶数
for(int i=2;i<=100;i+=2){
System.out.print(i+" ");
}
System.out.println();
//
for(int i=2;i<=10;i++){
if(i==5){
continue;
}
System.out.print(i+" ");
}
}
}
import java.util.Scanner;
public class whilee {
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
int i=1;
while(true){
System.out.println("你已经跑了第"+i+"圈,是否继续。");
String str=scanner.next();
if(str.equals("否")){
System.out.println("跑不动了。");
break;
}else if("是".equals(str)){
System.out.println("接着跑。");
i++;
}
}
int i1=1;
do{
System.out.println(i1+" ");
i1++;
}while(i1<=5);
//双层for循环;九九乘法表
for(int i2=1;i2<=9;i2++){
for(int j=1;j<=i2;j++){
System.out.print(i2*j+" ");
}
System.out.println();
}
}
}