2017-04-04 77 views
-1

我使用不apear:nextLine(),當我運行我的代碼

public static final Scanner CONSOLE = new Scanner(System.in); 

,然後在我的主要方法:

System.out.print("\nPlease choose a color for the circle between the following options:\nBlack\nRandom"); 
String colorChoice = CONSOLE.nextLine(); 
colorChoice = colorChoice.trim(); 

程序將顯示「請選擇一個顏色」,但然後在允許任何用戶輸入之前終止。我不知道爲什麼它不會工作。

這是輸出:

請輸入一個半徑爲50和400之間的循環:[DrJava輸入框]

請選擇下列選項之間的圈顏色: 黑色 隨機>

編輯1:

我改了行System.out.print("\nPlease choose a color for the circle between the following options:\nBlack\nRandom");System.out.print("\nPlease choose a color for the circle between the following options: Black, Random");看是否\n是問題。它仍然不起作用。

我包括一個while循環(用於檢查的值),使代碼讀取後:

System.out.print("\nPlease choose a color for the circle between the following options: Black, Random"); 
String colorChoice = CONSOLE.nextLine(); 
colorChoice = colorChoice.trim(); 
boolean matchesChoice = matchesChoice(colorChoice, "black", "random"); 
while(matchesChoice != true){ 
System.out.print("\nInvalid color choice. Please try again."); 
colorChoice = CONSOLE.nextLine(); 
colorChoice = colorChoice.trim(); 
matchesChoice = matchesChoice(colorChoice, "black", "random"); 
} 

這種情況的輸出讀取:

請輸入一個半徑爲50和400之間的循環: [DrJava輸入框]

請爲以下選項之間的圓圈選擇一種顏色:黑色,隨機 顏色選擇無效。請再試一次。 [DrJava輸入框]

因此,CONSOLE.nextLine()在循環內工作,但不在外部。無論我作爲用戶輸入插入(正確與否),它只是重複「無效的顏色選擇」行和輸入。

+0

我認爲它是由緩衝區延遲引起的,nextLine一直持續閱讀,直到它找到一個換行符「\ n」,然後停止,你正在打印的語句在其中打印兩個換行符。我猜測''CONSOLE.nextLine()'是在輸出緩衝區打印完所有'print'語句之前執行的,所以當print語句繼續時,它給'nextLine()'一些東西讀取。如果是這樣的話,那很奇怪。嘗試像這樣打印:'「...選項:黑色,隨機:「' – Everyone

+0

」然後在允許任何用戶輸入之前終止。「它允許我在終止於我的IDE之前輸入內容 –

+0

您需要提供與問題相關的漏洞代碼。一個錯誤/異常???? –

回答

-1
import java.util.Scanner; 
    import javax.swing; 

    public class (NAME) { 
     public static void main(String args[])throws Exception{ 
      Scanner CONSOLE = new Scanner(System.in); 
      System.out.println(("Please enter a radius for the circle between 50 and 400") 
      int radius = Integer.parseInt(JOptionPane.showInputDialog("Please choose a number from 50 to 400:")); 
      System.out.println("Radius is " + radius.toString(); 
      System.out.println(("Please choose a color for the circle between the following options: Black Random"); 
      String colorChoice = CONSOLE.nextLine(); 
      colorChoice = colorChoice.trim(); 
->   System.out.println("You chose " + colorChoice); 
    } 

箭頭所在的行缺少代碼。它實際上接受colorChoice的值,但不會顯示它,因爲您忘記顯示它。 :)

import java.util.Scanner; 
    import javax.swing; 

    public class (NAME) { 
     public static final Scanner CONSOLE = new Scanner(System.in); 
     public static void main(String args[])throws Exception{ 

      System.out.println(("Please enter a radius for the circle between 50 and 400") 
      int radius = Integer.parseInt(JOptionPane.showInputDialog("Please choose a number from 50 to 400:")); 
      System.out.println("Radius is " + radius.toString(); 
      System.out.println(("Please choose a color for the circle between the following options: Black Random"); 
      String colorChoice = CONSOLE.nextLine(); 
      colorChoice = colorChoice.trim(); 
->   System.out.println("You chose " + colorChoice); 
    } 

這也可以。如果你想讓掃描儀公開。它可以是私人的,特別是當你只使用一個文件/類時。

PS:總是有沒有理由的選民。即使只是看問題本身,也可能需要多個答案,我們大多數人都不知道要提供什麼具體內容。

-2

嘗試將其分配給colorChoice前使用

while(CONSOLE.hasNextLine()) 

+0

我的程序只是卡在while循環中,但至少要求用戶輸入。 – JaredAndrz

相關問題