2017-01-16 48 views
0

这是我在运行java代码进行清理和格式化的NetBeans上的第一个任务。我没有任何其他编程语言的经验。在我运行该文件后,它永远不会结束运行。我不知道要寻找什么。我尝试了一些建议,包括灯泡旁边的灯泡,但没有奏效。 (*代码已给出且未更改)带输入/输出的Java程序没有完成运行(挂起)

import java.util.Scanner; 

public class InputOutput { 
    public static void main(String[] args) { 
     /* Using print statements */ 
     System.out.println("Print first "); 
     System.out.println("Print second "); 

     /* Using println statements */ 
     System.out.println("Print third"); 
     System.out.println("Print fourth"); 
     System.out.println("Print fifth"); 

     /* Create a scanner for inputs */ 
     Scanner input = new Scanner(System.in); 

     /* Get the color and display it */ 
     System.out.println("What is your favorite color?"); 
     String color = input.nextLine(); 
     System.out.println("You wrote: " + color); 

     /* Get the num of apples and display */ 
     System.out.println("How many apples do you have?"); 
     int numApples = input.nextInt(); 
     System.out.println("You wrote: " + numApples); 

     /* Close the scanner */ 
     input.close(); 
    } 
} 
+3

你怎么知道它“永不结束”?我跑你的代码,并终止罚款。 –

+0

请显示输入/输出**编辑**对我来说工作正常 –

+0

也许你应该在你的代码的最后添加一条消息,打印一条消息说明程序已经完成。 –

回答

0

在控制台中键入您最喜欢的颜色,然后按Enter键。该计划挂起,因为它期望扫描一条线。在键入之前,没有任何可扫描的内容。

0

您的java程序仍在等待输入。

从终端窗口(或来自提示符)运行时,您只需从此处开始键入。

然而,在你的情况下,你从netbeans内运行。 Netbeans会在'输出'窗口收集输出。同样的窗口也将用于收集终端输入。只需确保您在输出内容的窗口中单击,当您开始输入内容时,就会看到您的输入内容出现。点击输入键以接受条目,并沿着代码进一步移动您的代码,并可能输入下一行信息。