2.1铁路买票
package
hellow;
import
java.awt.SystemTray;
import
java.util.Scanner;
public
class
Main {
public
static
void
main(String[]
args
) {
//
TODO
Auto-generated method stub
//初始化
Scanner
in
=
new
Scanner(System.
in
);
//读入投币金额
System.
out
.println(
"请投币:"
);
int
amount
=
in
.nextInt();
System.
out
.println(
amount
>=10);
//打印车票
System.
out
.println(
"****************"
);
System.
out
.println(
"**java城际铁路专线**"
);
System.
out
.println(
"****无指定座位票****"
);
System.
out
.println(
"****票价:10元*****"
);
System.
out
.println(
"****************"
);
//计算并打印找零
System.
out
.println(
"找零:"
+(
amount
-10));
}
}
System.
out
.println(Math.abs(
b
-
c
)<1e6);
1.代码
int
a
=1;
double
b
=1.0;
double
c
=0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1;
System.
out
.println(
a
==
b
);
//int10=double 10.0
System.
out
.println(
b
==
c
);
//double 10.0!=double 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1
System.
out
.println(
"a="
+
a
+
"b="
+
b
+
"c="
+
c
);
System.
out
.println(Math.abs(
b
-
c
)<1e-6);
//比较double 10.0 和double 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1的方法
2.3判断语句
1.if语句
if(total>=amount)
total+=amount+10;
2.if-else语句
if (total>=amount)
total+=amount+10;
else total+=amount-10;
3.if-else嵌套使用
在多级if-else嵌套中,else和最近的if对应
代码:
Scanner
in
=
new
Scanner(System.
in
);
int
a
=0;
int
b
=0;
int
c
=0;
System.
out
.println(
"请输入三个数字"
);
a
=
in
.nextInt();
b
=
in
.nextInt();
c
=
in
.nextInt();
int
max
;
if
(
a
>
b
)
{
max
=
a
;
}
else
{
max
=
b
;
}
if
(
c
>
max
)
{
max
=
c
;
}
else
{
max
=
max
;
}
System.
out
.println(
"三个数字中,最大的数字是;"
+
max
);
}
4.if-else if级联使用
if(exp1)
st1;
else if(exp2)
st2;
else if (exp3)
se3;
5.注释
5.1//
5.2ctrl+/
2.3.1switch-case级联的
switch(控制表达式)
{
case 常量:语句
case 常量:语句
default 语句
break;
}
1.代码
Scanner
in
=
new
Scanner(System.
in
);
int
type
=0;
type
=
in
.nextInt();
switch
(
type
)
{
case
1:
case
2:System.
out
.println(
"你好?"
);
break
;
case
3:System.
out
.println(
"您不要好"
);
break
;
case
4:System.
out
.println(
"真美"
);
break
;
case
5:System.
out
.println(
"真漂亮"
);
break
;
default
:System.
out
.println(
"乖"
);
break
;
}