2016-11-16 155 views
-5

我必须编写一个完整的Java程序,提示用户输入一系列数字来确定输入的最小值。程序终止前,显示最小值。我要使用此代码,并完成信息来运行程序:我需要帮助解决这个Java代码

这是代码:

Scanner keyboard = new Scanner(System.in); 

    int smallest = 9999999; 
    String user_Input; 
    boolean quit = false; 

    System.out.println("This program finds the smallest number" 
     + " in a series of numbers"); 
    System.out.println("When you want to exit, type Q"); 

    while(…………..) 
    { 
     System.out.print("Enter a number: "); 
     user_Input = keyboard.next(); 
     if(user_Input.equals("Q")……….. user_Input.equals("q")) 
     { 
      quit = true; 
     } 
     ……….. 
     { 
      int user_Number = Integer.parseInt(user_Input); 

      if(……………………) 
       smallest = user_Number; 
     } 
    } 
    System.out.println("The smallest number is " + smallest); 
    System.exit(0); 
} 

}

+0

这是一个可怕的称号。你的头衔应该总结这个问题。请编辑它以反映您询问的实际问题。 –

+1

一个更好的标题可能是:“谁愿意免费做我的作业?”。 – Tom

+0

你到底需要什么帮助?这段代码的哪部分给你提供了问题? – Gulllie

回答

-1

这只是读取并试图解析数,如果失败,那么它就转到while循环的后卫。

此外,您应该尝试使用Integer.MAX_VALUE而不是随机数。以防有人决定实际使用最大值;不要以为9,999,999或者你想怎么过许多9的是最大的

  1. 你实际上将超过最大值,并导致错误
  2. 其实并没有达到最大值
  3. 幻数是坏

有关max值的详细信息:https://en.wikipedia.org/wiki/2147483647_(number)

Scanner keyboard = new Scanner(System.in); 

int smallest = Integer.MAX_VALUE; 
String input = ""; 

System.out.println("This program find the smallest number" 
    + " in a series of numbers"); 
System.out.println("When you want to exit, type Q"); 

while (!input.toLowerCase().equals("q")) { 
    System.out.print("Enter a number: "); 
    input = keyboard.next(); 
    try { 
     int numb = Integer.parseInt(input); 
     if (numb < smallest) 
      smallest = numb; 
    } catch (NumberFormatException e) { 
     // Maybe check for other random strings here? 
     // If you expect only "Q" or a number, then no need 
    } 
} 

System.out.println("Smallest number: " + smallest); 
System.exit(0);