如何将输出格式化为列

时间:2016-06-07 12:18:53

标签: java

如何将输出格式化为列?我有一个基本的知识,但似乎停留在格式下来。我的目标是让它看起来像下面的格式。

Hour  Act1  Act2  Act3

1           Sleep

2           Sleep

3           Sleep

4           Sleep

5           Sleep

6           Sleep

7           Sleep

8           Sleep

9           Sleep

10

11

12    Eat

13    Eat

14    Eat

15

16                School

17                School

18                School

19                School

20                School

21                School

22                School

23

24

我的代码:

import java.util.Scanner;
public class DaySchedule {

    public static void main(String[] args) {
        // Day Schedule

        String activity1, activity2, activity3;
        int eat, sleep, school, eatstart, eatend, sleepstart, sleepend, schoolstart, schoolend;
        Scanner in = new Scanner (System.in);

        System.out.println("Enter first daily activity: ");
        activity1=in.next();
        System.out.println("Enter total hours for activity one: ");
        eat=in.nextInt();
        System.out.println("Enter starting hour for activity one: ");
        eatstart=in.nextInt();
        System.out.println("Enter ending hour for activity one: ");
        eatend=in.nextInt();
        System.out.println("Enter secound daily activity: ");
        activity2=in.next();
        System.out.println("Enter total hours for activity two: ");
        sleep=in.nextInt();
        System.out.println("Enter stating hour for activity two: ");
        sleepstart=in.nextInt();
        System.out.println("Enter ending hour for activity two: ");
        sleepend=in.nextInt();
        System.out.println("Enter third daily activity: ");
        activity3=in.next();
        System.out.println("Enter total hours for activity three: ");
        school=in.nextInt();
        System.out.println("Enter starting hour for activity three: ");
        schoolstart=in.nextInt();
        System.out.println("Enter ending hour for activity three: ");
        schoolend= in.nextInt();

        int hour;
        int mins;
        System.out.println("Day Schedule");
        System.out.format("%-3s%-10s%-10s%-10s","Hour ","Activity1", "Activity2", "Activity3");
        System.out.format("%n");
        for (hour=1; hour<=24; ++hour);
        {
             System.out.format("%3d",hour);

             for (mins=1; mins<=59; ++mins)
                {
                    switch(mins)
                    {
              case 1: if (hour==eat)
                          System.out.printf("%-3s%-10s","",activity1);
                      break;
              case 2: if (hour==sleep)
                          System.out.format("%-3s%-10s%-10s","","",activity2);
                      break;
              case 3: if (hour==school)
                          System.out.format("%-3s%-10s%-10s%-10s","","","",activity3);
                      break;     



        }

    }

        System.out.println();
        }
    }
}

0 个答案:

没有答案