简体   繁体   English

如何仅向一行添加空格

[英]How to add a space to only one line

I have been trying to make a calendar and have this coded so far: 到目前为止,我一直在尝试制作日历并将其编码:

import java.util.*;
public class MyOwnCalendar {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);


        System.out.print("Enter the number of days in month: ");
        int days = keyboard.nextInt();

        System.out.print("Enter which day the first day is on: ");
        int firstDay = keyboard.nextInt();

        System.out.println("S   M   T   W   Th  F   Sa");

        int ctr = 1;

        if (days == 31 && firstDay == 1)
        {
            while (ctr <= days)
            {
                System.out.print(ctr);
                System.out.print("  ");
                ctr++;
                if (ctr == 8 || ctr == 15 || ctr == 22 || ctr == 29)
                {
                    System.out.println();
                }
            }
        }else if (days == 31 && firstDay == 2)
        {
            while (ctr <= days)
            {
                System.out.print("\t");
                System.out.print(ctr);
                System.out.print("  ");
                ctr++;
                if (ctr == 7 || ctr == 14 || ctr == 21 || ctr == 28)
                {
                    System.out.println();
                }
            }
        }

Everything was going well until here. 一切顺利,直到这里。 If I enter 31 for days , and 2 for firstDay , this is the output: 如果我输入31 days ,而2输入firstDay ,则输出:

Enter the number of days in month: 31
Enter which day the first day is on: 2
S   M   T   W   Th  F   Sa
    1   2   3   4   5   6  
    7   8   9   10  11  12  13              
    14  15  16  17  18  19  20  
    21  22  23  24  25  26  27  
    28  29  30  31  

While the output for 31 days , and 1 for firstDay : 输出为31 days ,而firstDay为1:

Enter the number of days in month: 31
Enter which day the first day is on: 1
S   M   T   W   Th  F   Sa
1   2   3   4   5   6   7   
8   9   10  11  12  13  14  
15  16  17  18  19  20  21  
22  23  24  25  26  27  28  
29  30  31  

How would I indent only the first line for something like the first calendar I inputted? 我如何只缩进第一行,例如我输入的第一个日历? Also, if there is a more efficient way to code a calendar, instead of doing it like this, I would also greatly appreciate that. 另外,如果有一种更有效的日历代码编写方法,而不是像这样做,我也将不胜感激。 I am quite new to programming. 我是编程新手。 I've only been learning for a couple weeks or so. 我只学习了几个星期左右。

The while loop is what loops through every line, so just move your new line to only happen for the first time through the loop. while循环是循环遍历每行的内容,因此只需将新行移动到仅在循环中第一次发生即可。 (Move System.out.print("t"); above the while loop) (将System.out.print("t");移动到while循环上方)

if (days == 31 && firstDay == 2)
{
    System.out.print("\t");
    while (ctr <= days)
    {

import java.util.*; 导入java.util。*; public class MyOwnCalendar { 公共类MyOwnCalendar {

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);


    System.out.print("Enter the number of days in month: ");
    int days = keyboard.nextInt();

    System.out.print("Enter which day the first day is on: ");
    int firstDay = keyboard.nextInt();

   System.out.printf("%4c%4c%4c%4c%4c%4c%4c%n",'S','M','T','W','T','F','S');

    int ctr = 1;

    if (days == 31 && firstDay == 1)
    {
        while (ctr <= days)
        {
            System.out.printf("%4d",ctr);
           // System.out.print("  ");
             ctr++;
            if (ctr == 8 || ctr == 15 || ctr == 22 || ctr == 29)
            {
                System.out.println();
            }

        }
    }else if (days == 31 && firstDay == 2)
    {
        while (ctr <= days)
        {
            //System.out.print("\t");
            System.out.printf("%4d",ctr);
            //System.out.print("  ");

            if (ctr == 7 || ctr == 14 || ctr == 21 || ctr == 28)
            {
                System.out.println();
            }
              ctr++;
        }
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM