我需要從這段代碼中獲得最大值,但它確實很棘手。任何人有任何建議?獲取「用戶輸入的最大值」?
import java.util.Scanner;
public class Test{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
final int sentinel = 0;
int number;
int maxval = 0;
int count = 0;
System.out.println ("Enter a number: ");
number = scan.nextInt();
while (number != sentinel)
{
count++;
System.out.println("Enter another number");
number = scan.nextInt();
maxval = number;
}
if (number > maxval)
{
maxval = number;
System.out.println ("The max value is " + maxval);
}
}
}
刪除'while'裏面的'maxval = number'並在循環中獲得條件語句 – mshsayem 2012-04-06 01:31:41