2013-11-21 31 views
-1

我越來越沒有,如果最後一個人的錯誤,並且似乎無法做任何事情。誰能告訴我什麼似乎是問題?謝謝!否則沒有,如果在一條線上的錯誤

public class FindMin 
{ 
public static void main(String[] args) 
{ 
    Scanner keyboard = new Scanner(System.in); 

    int smallest = 9999999; 
    String userInput; 
    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(quit != True) 
    { 
     System.out.print("Enter a number: "); 
     userInput = keyboard.next(); 
     if(userInput.equals("Q")) userInput.equals("q"); 
     { 
      if (quit = true); 

      } 
      else    

      { 

      int userNumber = Integer.parseInt(userInput); 

      if(UserNumber < smallest) 
       smallest = userNumber; 
     } 
    } 
    System.out.println("The smallest number is " + smallest); 
    System.exit(0); 
} 
} 
+0

你的兩個'if'語句都有完整的語句。第一個是'userInput.equals(「q」);',第二個是';'。刪除它們來解決你的問題 –

+1

你有很多語法錯誤......我建議你回想一下這個鏈接> http://www.tutorialspoint.com/java/java_basic_syntax.htm。 –

+0

'if(x); x ++'與'if(x){} x ++'和'x'將一直遞增相同。 「else」子句幫助您避免了很難發現錯誤的情況。 –

回答

2
if (quit = true); 

刪除分號,加==

結果:

if(quit == true) { 

} 

= =>分配
== =>比較

或者,簡寫:

if(quit) { 

} 

== true自動暗示。

由於此處沒有代碼,因此可以簡單地執行此操作以避免出現空白情況。

if(!quit) { 
    // usernumber & related stuff 
} 

此外:

while(quit != True) 

應該是

while(quit != true) 

或簡寫:

while(!quit) { 

} 

if(UserNumber < smallest) 

應該

if(userNumber < smallest) 

最後:

int smallest = 9999999; 

它的安全使用

int smallest = INTEGER.MAX_VALUE; 

一個小小的提示:你一個false值永遠不會設置爲quit所以它會成爲一個永久的循環。最後

一個說明,我承諾:

這(不編譯)

if(userInput.equals("Q")) userInput.equals("q"); 

可以改寫爲

if(userInput.equalsIgnoreCase("Q")); 
+0

好吧,所以我照顧了這一點,但現在我得到另一個錯誤。其:在分析時達到文件結尾。在支架的最後一行,這可能是什麼? – user3015774

+0

@ user3015774:你能編輯你的代碼來顯示你現在擁有什麼嗎? –

1

試試這個:

import java.util.Scanner; 

public class Main { 
    public static void main(String[] args) { 
     Scanner keyboard = new Scanner(System.in); 

     int smallest = 9999999; 
     String userInput; 

     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 (true) { 
      System.out.print("Enter a number: "); 
      userInput = keyboard.next(); 
      if (userInput.equals("Q") || userInput.equals("q")) { 
       System.exit(0); 
      } 
      smallest = Math.min(smallest, Integer.parseInt(userInput)); 
      System.out.println("The smallest number is " + smallest); 
     } 
    } 
} 

您的解決方案有許多語法呃其他用戶所描述的漫遊。此外,您錯過了檢查最小號碼的例程。