三元 导包
//一个源文件只能有一个为public的类 与源文件名称一致
//public class Day03
class Day03{
//程序的主方法,是程序的入口
public static void main(String[] ages){
System.out.println("Hello World");
}
}
//**********************
//变量的类型转换 自动转 强制转换
short s = 12;
int i = s +2 ;
//强转
int s = 12;
short i = (short) s;
//浮点型强转整形
double d = 3.14;
int i = (int) d ; //3
//**********************
//赋值运算符
class Day03{
public static void main(String[] agrs){
int i = 10;
i1 += 3; //i1 =13
int i2 = 12;
i2 %= 5; //i2=i2%5 =2
}
}
int i = 1;
i *= 0.1;//i = (int)(i * 0.1)
System.out.println(i);//0 int是整数型
//****************************
//三元运算(三元必须返回一个结果)
String str = (5<3)?"5大于3":"5不大于三";
System.out.println(str);
// if-else代替
String str = null;
if(5>3){
str = "5大于3";
}else{
str = "5不大于3";
}
System.out.println(str);
//三元输出3个数最大值
int a = 2;
int b = 4;
int c = 6;
int max =(a>b)?a:b;
int max2=(max >c)? max:c;
System.out.println(max2);
//*************************
//if if-else else -if
int age =20;
if(age >= 18){
System.out.println("成年");
}else{
System.out.println("未成年");
}
//**********************
//键盘输入
//导包
import java.util.Scanner;
class Day03{
public static void main(String[] agrs){
//2.创建一个Scanner类的对象
Scanner s = new Scanner(System.in);
System.out.println("请输入一个字符串");
//next():表示从键盘获取一个字符串
String str = s.next();
System.out.println(str);
}
}
//键盘输入成绩
import java.util.Scanner;
class Day03{
public static void main(String[] agrs){
Scanner s = new Scanner(System.in);
System.out.println("请输入成绩");
int cj = s.nextInt();
if(cj>100||cj<0){
System.out.println("非法成绩");
}else {
if(cj==100){
System.out.println("奖励宝马");
}else if(cj>80){
System.out.println("奖励手机");
}else if(cj>=60){
System.out.println("奖励参考书");
}else{
System.out.println("没有奖励");
}
}
}
}
//键盘输入 三个整数 排序(使用 if-else if-else), 小 到大 输出
import java.util.Scanner;
class Day03{
public static void main(String[] agrs){
//键盘输入三个整数
Scanner s = new Scanner(System.in);
System.out.println("输入第一个数");
int num1 =s.nextInt();
System.out.println("输入第2个数");
int num2 =s.nextInt();
System.out.println("输入第3个数");
int num3 =s.nextInt();
System.out.println("大到小排序是: ");
if(num1>num2){
if(num3>num1)
System.out.println(num2 +" " +num1 + " " +num2);
else if(num2>num1)
System.out.println(num3 + "," + num2 + "," + num1);
else
System.out.println(num2 + "," + num3 + "," + num1);
}else{
if(num3 > num2)
System.out.println(num1 + "," + num2 + "," + num3);
else if(num3 < num1)
System.out.println(num3 + "," + num1 + "," + num2);
else
System.out.println(num1 + "," + num3 + "," + num2);
}
}
}