简体   繁体   English

Java扫描仪:输入后如何保持相同的行

[英]Java Scanners: How to keep the same line after an input

I have a small code segment that I do not know how to fix. 我有一小段代码,我不知道如何解决。 This is it: 就是这个:

System.out.print("y=");
while(!scan.hasNextInt()) scan.next();
m = scan.nextInt();
System.out.print("x+");
while(!scan.hasNextInt()) scan.next();
b = scan.nextInt();

The output is: y=3 on one line, and x+4 on the next. 输出为:y = 3一行,x + 4下一行。 I would like them to be on the same line. 我希望他们在同一条线上。 How do I do this? 我该怎么做呢?

I'm not sure what you want to do there but maybe you just need to use one System.out.print? 我不确定您要在那里做什么,但也许您只需要使用一个System.out.print? at the bottom of your code with example: 在代码底部的示例:

System.out.print("y= %d  x+%d", m , b);

It is bit confusing to understand your question why you want it ? 理解您的问题为什么会有点令人困惑。

But here is a snippet you can try and see if it can be useful to you! 但是,您可以尝试以下代码片段,看看它是否对您有用!

 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;


 public class so1 {
     public static void main(String[] args) throws IOException {
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         String arrayStr = br.readLine();
         String[] auxStr = arrayStr.split(" ");
         int[] arr = new int[2];

        for(int i=0;i<auxStr.length;i++){
           arr[i]= Integer.parseInt(auxStr[i]);
        }

        System.out.println("y="+arr[0] + " x+"+arr[1]);
  }

}

Here is what you can get - 这是您可以获得的-

3 4
y=3 x+4

Update your question if you not find it useful . 如果您觉得问题没有用,请更新您的问题。 I will update accordingly. 我将进行相应的更新。

Looks like this is how your program looks: 程序看起来像这样:

y=3<newline>
x+5<newline>

But you want it to look like: 但您希望它看起来像:

y=3x+5

The problematic newlines come from using the scanner and the inherently buffered nature of standard input. 有问题的换行符来自于使用扫描仪和标准输入的固有缓冲性质。 In order for the scanner to know that it's reached the end of the number, there has to be whitespace (space, newline, etc.) And in order for the program to receive the input from STDIN, the character length of the int has to be larger than the buffer size, or ENTER has to be pushed. 为了使扫描程序知道到达数字的末尾,必须有空格(空格,换行符等)。为了使程序从STDIN接收输入,int的字符长度必须为大于缓冲区大小,或者必须按ENTER键。

TL;DR - What you want is very difficult to do, and maybe impossible, just using basic standard in. If you really want the output you desire you'll probably need to find a library for interacting with the console and use that. TL; DR-仅使用基本标准就很难做到,甚至可能做不到。如果您真的想要输出,则可能需要找到一个与控制台交互的库并使用它。

However, if you reframe your input, you might be able to get something almost as good: 但是,如果您重新构造输入内容,则可能会得到几乎一样的效果:

enter intercept: 5<enter>
enter slope:     3<enter>
y=3x+5

Your code, modified to produce that output: 您的代码经过修改以产生该输出:

System.out.print("enter intercept:");
while(!scan.hasNextInt()) scan.next();
m = scan.nextInt();
System.out.print("enter slope:    ");
while(!scan.hasNextInt()) scan.next();
b = scan.nextInt();
System.out.println("y="+m+"x+"+b);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM