【问题标题】:Eclipse won't print multiple lines of code to the consoleEclipse 不会将多行代码打印到控制台
【发布时间】:2015-12-22 02:50:07
【问题描述】:

我正在尝试编写一个示例程序来帮助我更好地理解 java,但问题是 Eclipse 只会为一行代码打印出 System.out.println() 中的任何内容,而不是全部。

class MyClass {
    public static void main(String[] args){
        System.out.println("I am learning Java");
        System.out.println("Hello World");      

        String fruit = "apple";    
        int sea =1;                
        double goaldifference = 2.54;  

        System.out.println(fruit);
        System.out.println(sea);
        System.out.println(goaldifference);

    } //this line of code is printed to the console 

    {
         int a , b , c;         
         a =4;
         b =6;
         c = a+b;
        System.out.println(c);

        int hen =17;               
        System.out.println(++hen);                    
    }//this line of code isn't printed to the console

    {                         
        int test = 6;                  

        if (test == 9){
            System.out.println("yes");
        }else{
            System.out.println("no");
            if  (test != 8){                                 
                System.out.println("no");                          
            }else{                                           
                System.out.println("try different number");  
            }

            int age = 14;   

            if(age < 16){
                System.out.println("still in high school");
            }else if (age > 16){
                System.out.println("in college");
            }//this line of code isn't printed to the console
        }
    }

Eclipse 不会将任何错误标记为错误,并且在打印到控制台时我没有收到任何错误,只是我的第二行和第三行代码无论出于何种原因都不会在控制台上打印任何内容。我从文本中删除了一些单行和多行 cmets,但图片中的代码仍未编辑,感谢您的帮助,如果我的代码看起来令人困惑,我们深表歉意。

【问题讨论】:

  • 对不起,我引用了任何图片,我试图包含来自 eclispe 的代码的一些图片,但网站不允许我包含我想要的数字,所以我只是删除了它们
  • 您有一些额外的花括号,因此并非所有内容都是您的主要方法的一部分。
  • 只有主方法内的 SOP 语句将被执行,根据您的代码,其他 SOP 语句不在主方法内,而是在实例块内。顺便说一句,代码将编译并运行良好。

标签: java string int


【解决方案1】:

在您完成消息后,只需使用\n。例如:

System.out.println("First line\n");
System.out.println("Next line\n");

\n 是一个特殊字符。每当您看到带有字符的前导反斜杠时,就意味着它做了一些特别的事情。 \n 表示转到下一行。

【讨论】:

  • System.out.println();它自己有\n
  • 可以请更详细地解释我到底在哪里使用 System.out.println("First line\n"); System.out.println("下一行\n");在我的代码中
  • 请查看 Bahramdun 的回答链接。它详细介绍了如何找出解决方案。
  • 正如我所说的链接不起作用,我对 System.out.println("First line\n"); 的位置感到困惑System.out.println("下一行\n") 可以使用
  • 它可以在任何地方使用。无论您将System.out.println() 放在哪里,您都可以在其中添加\n 添加一个新行。
猜你喜欢
  • 1970-01-01
  • 2020-07-12
  • 1970-01-01
  • 1970-01-01
  • 2011-08-06
  • 1970-01-01
  • 2011-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多