2014-02-12 96 views
0

我在初始化程序中的if語句時遇到了問題。程序每次運行if語句之前都會終止。誰能告訴我我究竟做錯了什麼?程序在if語句之前終止

import java.util.Scanner; 

public class attempt1 { 

    public static void main (String [] args) { 
     Scanner console = new Scanner(System.in); 
     String userInput; 
     boolean done=false; 
     System.out.println("say something"); 
     userInput=console.nextLine(); 

     if (userInput.equals("stop")) { 
      done=true; 
     } 
     while(done=false) { 
      System.out.println("it worked!"); 
     } 
    } 
} 
+3

你確定它的終止,這是不等待輸入? – PakkuDon

+1

它應該是'while(done == false)',而不是'while(done = false)' –

+0

@sashkello完全相反;它永遠不會輸入循環。 –

回答

1

取而代之的是構建

if (userInput.equals("stop")) { 
     done=true; 
    } 
    while(done=false) { 
     System.out.println("it worked!"); 
    } 

我想你想說的比較值在這種情況下不想使用循環。

+0

這對我有效的方法!感謝您的幫助! – JamesDavisSmith

0

您正在使用掃描儀。如果用戶沒有輸入任何數據,程序將不會運行。嘗試使用預設的字符串或int或任何您需要的來運行它。代碼看起來很好

+3

*「代碼看起來不錯」* ...所以我假設你錯過了*「while(done = false)」*然後.. – MadProgrammer

+0

@MadProgrammer我沒有錯過。他有一個if語句可以將布爾值更改爲true。我會假設,當測試一個程序時,他會測試所有的可能性。他聲稱該計劃甚至沒有獲得if語句。因此,我假設用戶沒有給出任何輸入 – ChriskOlson

+0

'done = false'是'done'值的重新分配,這意味着當while循環被評估時它永遠不會是'true' ......這個在任何一本書中都不是「很好」... – MadProgrammer

-1

你有一個無限循環。

while(done=false) { 
System.out.println("it worked!"); 
} 

這使程序崩潰。只需簡單地將(完成= false)更改爲「else if(!done)」。

+2

它不會「崩潰」任何東西,它肯定不是無限的......它會評估爲「假」,永遠不會進入循環。 –

7

我想你想是這樣的:「它的工作」

public static void main (String [] args) { 
    Scanner console = new Scanner(System.in); 
    String userInput; 
    boolean done=false; 

    while(done==false) { 
     System.out.println("say something"); 
     userInput=console.nextLine(); 
     if (userInput.equals("stop")) { 
     done=true; 
    } 

    } 
    System.out.println("it worked!"); 
} 

此代碼讓用戶繼續說着什麼,直到他們說停止,此時將其打印出來

程序會在每次打電話時等待您的輸入userInput=console.nextLine();這不是終止,而是等待輸入。

另外,你想==在你的比較中。

done = userInput.equals("stop") 
    if (!done) { 
     System.out.println("it worked!"); 
    } 

我不知道是什麼的while目的,但你肯定:==同時=分配

+2

' while(done == false)'可以改寫爲'while(!done)':) –

+0

這是真的,儘管我認爲做比較對於初學者來說更有意義 – connor

+1

@connor不,它引導*你指出的問題*;忘記一個而不是兩個'='並引入一個錯誤。 –

0

我在這裏看到一些問題。

你確定有下一行嗎?可以肯定的是:

System.out.println(console.hasNextLine()) 

另外,你的布爾值是不正確的。請記住,一個'='是一個賦值,'=='的意思是「等於」。

0

也許這是更好地這樣做 -

public class Example { 
    public static void main(String []args) { 

     Scanner console = new Scanner(System.in); 
     String userInput; 
     boolean done = false; 

     while(!done) { 
      System.out.println("it worked!"); 
      System.out.println("say something"); 
      userInput = console.nextLine(); 

      if (userInput.equals("stop")) { 
       done = true; 
      } 
     } 
    } 
} 

輸出:

it worked! 
say something 
something 
it worked! 
say something 
stop 
1

使用do-while在這裏,它會更容易:

Scanner console = new Scanner(System.in); 
String userInput; 
do { 
    System.out.println("say something"); 
    userInput=console.nextLine(); 
    if(userInput.equals("stop")) 
     break; 
} while(console.hasNextLine());