不斷要求用戶輸入一個數字(假定用戶輸入的數字都是正整數),當用戶輸入end的時候顯示剛纔輸入數字中的最大值
“`
這裏寫代碼片
import java.util.Scanner;
public class PrintMax {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String number = "";
int max = 0;
do
{
number = sc.next();
try
{
max = Integer.parseInt(number) >= max ? Integer.parseInt(number) : max;
}catch(java.lang.NumberFormatException e)
{
System.out.println("非法輸入");
}
}while(!number.toString().toLowerCase().equals("end"));
System.out.println("Max = "+max);
}
}
“`