Java如何在同一行上获取scanner.nextLine()?

时间:2016-04-03 12:10:32

标签: java

我有一个程序从命令行接收用户的输入。令人讨厌的是,当我第一次使用扫描仪时,输入的文本会在下一行(System.out.println之后)进行。但是,所有其他时间它与System.out.println在同一行。

代码:

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

//Gets type
System.out.println("--Television Show or Film (t or f): ");
String type = input.nextLine();
input.close();

输出:

--Television Show or Film (t or f): 
t

Desierd输出:

--Television Show or Film (t or f): t

感谢任何帮助。另外,我正在使用Eclipse Console。

3 个答案:

答案 0 :(得分:5)

使用System.out.println而不是{{1}}?

答案 1 :(得分:2)

尝试:

   System.out.print("--Television Show or Film (t or f): ");
   String type = input.next();

答案 2 :(得分:2)

原因是您使用println System.out版本的print。仅使用System.out.print("--Television Show or Film (t or f): ");Sub FindMatches() Dim oldrow As Integer Dim newrow As Integer For oldrow = 4 To 14 For newrow = 3 To 20 If Cells(oldrow, 12) = Cells(1, newrow) And Cells(oldrow, 13) = Cells(newrow, 1) Then 'date and brand If Cells(1, 14) = Cells(newrow, 2) Then Cells(newrow, 3).Value = Cells(oldrow, 14).Value ' m1 End If If Cells(1, 15) = Cells(newrow + 1, 2) Then Cells(newrow + 1, 3).Value = Cells(oldrow, 15).Value ' m2 End If If Cells(1, 16) = Cells(newrow + 2, 2) Then Cells(newrow + 2, 3).Value = Cells(oldrow, 16).Value ' m3 End If If Cells(1, 17) = Cells(newrow + 3, 2) Then Cells(newrow + 3, 3).Value = Cells(oldrow, 17).Value ' issue End If If Cells(1, 18) = Cells(newrow + 4, 2) Then Cells(newrow + 4, 3).Value = Cells(oldrow, 18).Value ' repack End If If Cells(1, 19) = Cells(newrow + 5, 2) Then Cells(newrow + 5, 3).Value = Cells(oldrow, 19).Value ' extra End If If Cells(1, 20) = Cells(newrow + 6, 2) Then Cells(newrow + 6, 3).Value = Cells(oldrow, 20).Value ' wastage End If End If Next newrow Next oldrow End Sub