java 在同一行打印输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31487156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:41:16  来源:igfitidea点击:

Print output on the same line

javaprinting

提问by Rajafa

I'm making a translator in Java to translate a fake language that I came up with for fun. I input an English word and it returns it's equivalent word in the other language. It's successfully translating everything, but each new word is on a separate line and I just want the output on one line. I'm still new to Java but here is my code:

我正在用 Java 制作一个翻译器来翻译一种我为了好玩而想出的假语言。我输入了一个英语单词,它返回它在其他语言中的等效单词。它成功地翻译了所有内容,但每个新单词都在单独的一行上,我只希望输出在一行上。我还是 Java 新手,但这是我的代码:

import java.io.*;
import java.util.*;

public class Translator {

    private static Scanner scan;

    public static void main(String[] args) {

            HashMap <String, String> XanthiumLang = new HashMap <String, String>();
            XanthiumLang.put("hello", "fohran");
            XanthiumLang.put("the", "krif");
            XanthiumLang.put("of", "ney");
            XanthiumLang.put("to", "dov");
            XanthiumLang.put("and", "ahrk");

            Scanner scan = new Scanner(System.in);
            String sentence = scan.nextLine();
            String[] result = sentence.split(" ");
            for(int i = 0; i < result.length; i++){
            if(XanthiumLang.containsKey(result[i])){
                result[i] = XanthiumLang.get(result[i]);
                   }
                 System.out.println(result[i]);
               }
        }
 }

I only have a few words in the code as of right now and they are stored in a hashmap. Anyways like I said the output of each word is on a separate line, not on just one line. Any ideas or changes to my code would be helpful!

到目前为止,我的代码中只有几个词,它们存储在哈希图中。无论如何,就像我说的,每个单词的输出都在单独的一行上,而不仅仅是一行。对我的代码的任何想法或更改都会有所帮助!

采纳答案by UnknownOctopus

Use System.out.print();. Doing so will print the entire array on one line. System.out.println();will print the result on a new line each time (hence the lnat the end).

使用System.out.print();. 这样做会将整个数组打印在一行上。System.out.println();每次都会在新行上打印结果(因此ln在最后)。

import java.io.*;
import java.util.*;

public class Translator {

  private static Scanner scan;

  public static void main(String[] args) {

    HashMap <String, String> XanthiumLang = new HashMap <String, String>();
    XanthiumLang.put("hello", "fohran");
    XanthiumLang.put("the", "krif");
    XanthiumLang.put("of", "ney");
    XanthiumLang.put("to", "dov");
    XanthiumLang.put("and", "ahrk");

    Scanner scan = new Scanner(System.in);
    String sentence = scan.nextLine();
    String[] result = sentence.split(" ");
      for(int i = 0; i < result.length; i++){
         if(XanthiumLang.containsKey(result[i])){
           result[i] = XanthiumLang.get(result[i]);
         }
         System.out.print(result[i]);
       }
     }
   }

More on the different formats here.

有关不同格式的更多信息,请点击此处