首先想起使用循环结构来实现,本题我使用do-while循环并且注意定义变量必须定义在do-while的外层循环。
package
Homework1208
;
import
java
.
util
.
Scanner
;
public
class
CompareMaxMin
{
public
static
void
main
(
String
[
]
args
)
{
int
max
=
-
99999
;
int
min
=
99999
;
int
aInteger
;
Scanner input
=
new
Scanner
(
System
.
in
)
;
System
.
out
.
println
(
"输入一个整数(输入0时结束)"
)
;
aInteger
=
input
.
nextInt
(
)
;
max
=
aInteger
;
min
=
aInteger
;
System
.
out
.
println
(
"输入一个整数(输入0时结束)"
)
;
aInteger
=
input
.
nextInt
(
)
;
if
(
aInteger
>
max
)
{
max
=
aInteger
;
}
else
if
(
aInteger
<
min
&&
aInteger
!=
0
)
{
min
=
aInteger
;
}
while
(
aInteger
!=
0
)
;
System
.
out
.
println
(
"最大值:"
+
max
+
"\t最小值:"
+
min
)
;