我的system.out.println垂直出来了

时间:2014-04-28 19:42:26

标签: java

一切都很完美,我知道或多或少的java sout格式,但我一直在关注这本书,它已经解释了逻辑是如何工作的。我似乎无法弄清楚的一件事是为什么我的输出是垂直的并且在书中它是完全水平的?我的格式完全相同

这是我的java代码

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package b.elsestatements;

import java.util.*;

/**
 *
 * @author willc86
 */
public class Clock {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Calendar now = Calendar.getInstance();
        int hour = now.get(Calendar.HOUR_OF_DAY);
        int minuite = now.get(Calendar.MINUTE);
        int month = now.get(Calendar.MONTH) + 1;
        int day = now.get(Calendar.DAY_OF_MONTH);
        int year = now.get(Calendar.YEAR);


        if (hour > 17){
            System.out.println("Good evening");
        } else if (hour < 12) {
            System.out.println("Goodmorning");
        } else {
            System.out.println("Good afternoon");
        }

        System.out.println("It is ");
        if (minuite != 0) {

            System.out.println("" + minuite + " ");

//            if (minuite != 1) {
//                System.out.println("minuites");
//            } else {
//                System.out.println("minuite");
//            }
            System.out.println((minuite != 1) ? "minuites" : "minuite ");
            System.out.println("Past");
            System.out.println((hour > 12) ? (hour - 12) : hour);
            System.out.println("O clock on ");

            //month case
            switch (month) {
                case 1:
                    System.out.println("Jan");
                    break;
                case 2:
                    System.out.println("Feb");
                    break;
                case 3:
                    System.out.println("Mar");
                    break;
                case 4:
                    System.out.println("Apr");
                    break;
                case 5:
                    System.out.println("May");
                    break;
                case 6:
                    System.out.println("Jun");
                    break;
                case 7:
                    System.out.println("Jul");
                    break;
                case 8:
                    System.out.println("Aug");
                    break;
                case 9:
                    System.out.println("Sept");
                    break;
                case 10:
                    System.out.println("Oct");
                    break;
                case 11:
                    System.out.println("Nov");
                    break;
                case 12:
                    System.out.println("Dec");

            }

            System.out.println("On " + year + " day " + day);
        }

    }

}

1 个答案:

答案 0 :(得分:2)

也许在你的书上它没有说:System.out.println也许它说:System.out.print

System.out.print("" + minuite + " ");
System.out.print((minuite != 1) ? "minuites" : "minuite ");
System.out.print("Past");
System.out.print((hour > 12) ? (hour - 12) : hour);
System.out.print("O clock on ");

也许这应该可以解决你的问题。

我的意思是将System.out.println更改为System.out.print

您的输出应如下所示:

Good afternoon
It is 52 minuitesPast2O clock on Apr
On 2014 day 28

System.out.println会在最后一行创建一个新行(ln),所以如果你把它取出来,它应该水平打印而不创建新行。

System.out.print上创建新行的另一种方法是使用与"\n"相同的转义字符System.out.println

这种情况:

System.out.println("Hello"); //Will create a new line
System.out.println("World"); //Will create a new line

System.out.print("Hello"); //Won't create a new line
System.out.print("World"); //Won't create a new line

System.out.print("Hello \n"); //Will create a new line
System.out.print("World"); //Won't create a new line

以上3种情况的输出将是:

Hello
World

Hello World

Hello
World