System.out.print 与 System.out.println(最后一句话)。

huangapple go评论90阅读模式
英文:

System.out.print vs. System.out.println (Last sentence)

问题

我是新手Java,还有很多问题。
有人能解释一下为什么这段代码可以工作,但加上这行:

System.out.print("\\");

在结尾的时候却不能工作吗?(看倒数第二行)

顺便说一句:我需要它显示这样:

\    /
  \  /
   \/
   /\
  /  \
/    \
英文:

I'm new to java and still have a lot of questions.
Can someone explain to me why this:

public class Programa02 {
  public static void main(String[] args ) {
       for (int lín = 1; lín <= 3; lín++) {
           for (int k = 0; k < (lín -1); k++) {
              System.out.print(" ");}
           
              System.out.print("\\");
              
           for (int m = 0; m < (6 - 2*lín); m++) {
              System.out.print(" ");}
           
              System.out.print("/");
              
             System.out.println(); }
              
       for (int lí = 1; lí <= 3; lí++) {
           for (int e = 0; e < (3 - lí); e++) {
              System.out.print(" ");}
           
              System.out.print("/");
              
           for (int i = 0; i < (2*lí - 2); i++) {
              System.out.print(" ");}
           
              System.out.println("\\");
}}}

works, but with this:

System.out.print("\\");

at the end, doesn't? (look at the second-to-last line)

BTW: I need it to show this:

\    /
  \  /
   \/
   /\
  /  \
/    \

答案1

得分: 1

printlnprint方法之间唯一的区别在于,在打印所需结果后,println会将光标移动到下一行,而print方法会保持光标在同一行上。

更多信息

英文:

The only difference between println and print method is that println throws the cursor to the next line after printing the desired result whereas print method keeps the cursor on the same line.

More information

答案2

得分: 0

我写了关于这个的内容,并在以下链接中提供了一些示例:

https://creatingcoder.com/2020/08/07/println-vs-print/

基本上,print方法不会移动光标,而println会创建新的一行。

英文:

I wrote about it and have some examples at the following link:

https://creatingcoder.com/2020/08/07/println-vs-print/

Basically, the print method doesn't move the cursor, whereas the println creates a new line.

huangapple
  • 本文由 发表于 2020年9月23日 23:46:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64031674.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定