Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java程序输出_Java_Calendar_Format - Fatal编程技术网

Java程序输出

Java程序输出,java,calendar,format,Java,Calendar,Format,我正在编写一个代码,其中必须显示类似于日历视图的输出 Sun Mon Tue Wed Thu Fri Sat 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 我的密码在这里 System.out.println("Sun Mon Tue Wed Thu Fri Sat"); int cur

我正在编写一个代码,其中必须显示类似于日历视图的输出

Sun Mon Tue Wed Thu Fri Sat
     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 
我的密码在这里

System.out.println("Sun Mon Tue Wed Thu Fri Sat");
int currentDay = 0;
for(int i = 0; i<randDay; i++){
  System.out.print("    ");
  currentDay++;
}
for(int i = 0; i< month.length; i++){
  if(month[i]!=null){
    System.out.printf("%3s" + "*", (i+1));
    currentDay++;
  }else{
    System.out.printf("%3s", (i+1));
    currentDay++;
  } 
   if(currentDay==7){
     currentDay=0;
     System.out.println();
  }

您可以看到,数字格式设置已关闭。您正在使用三位数字和可选的
*
进行格式化,这意味着有时组合宽度是3,有时是4。那排不上队

但格式实际上是:

<2-place number><space or '*'><space or newline>

这是一个由三部分组成的格式,尽管您可能应该单独进行

System.out.printf("%2d", i + 1);
System.out.print(month[i] != null ? '*' : ' ');
if (++currentDay < 7) {
    System.out.print(' ');
} else {
    System.out.println();
    currentDay = 0;
}
System.out.printf(“%2d”,i+1);
系统输出打印(月[i]!=null?'*':'';
如果(++当前日期<7){
系统输出打印(“”);
}否则{
System.out.println();
currentDay=0;
}

您可以看到,数字格式设置已关闭。您正在使用三位数字和可选的
*
进行格式化,这意味着有时组合宽度是3,有时是4。那排不上队

但格式实际上是:

<2-place number><space or '*'><space or newline>

这是一个由三部分组成的格式,尽管您可能应该单独进行

System.out.printf("%2d", i + 1);
System.out.print(month[i] != null ? '*' : ' ');
if (++currentDay < 7) {
    System.out.print(' ');
} else {
    System.out.println();
    currentDay = 0;
}
System.out.printf(“%2d”,i+1);
系统输出打印(月[i]!=null?'*':'';
如果(++当前日期<7){
系统输出打印(“”);
}否则{
System.out.println();
currentDay=0;
}

看起来您希望日历中的每一天都有3个字符长,中间有一个空格。此外,看起来单位数更喜欢居中,双位数更喜欢左对齐,任何带星号的都应该左对齐

我注意到的第一件事是这些行分别是4个字符和3个字符

System.out.printf("%3s" + "*", (i+1));
System.out.printf("%3s", (i+1));
因此,任何带有星号的字符都将是4个字符,没有星号的字符都将是3个字符。仅此一点就将导致它们未对齐


相反,我会选择%2而不是3。您还需要有条件地在后面添加空格或星号,具体取决于项目是否为空。这将确保它们每个都是3个字符,然后您可以始终在循环中的每个项目之间添加一个空格。

看起来您希望日历中的每一天都是3个字符长,并且在它们之间有一个空格。此外,看起来单位数更喜欢居中,双位数更喜欢左对齐,任何带星号的都应该左对齐

我注意到的第一件事是这些行分别是4个字符和3个字符

System.out.printf("%3s" + "*", (i+1));
System.out.printf("%3s", (i+1));
因此,任何带有星号的字符都将是4个字符,没有星号的字符都将是3个字符。仅此一点就将导致它们未对齐

相反,我会选择%2而不是3。您还需要有条件地在后面添加空格或星号,具体取决于项目是否为空。这将确保它们每个为3个字符,然后您可以始终在循环中的每个项目之间添加一个空格。

在System.out.print中“?”做什么?@Saad请参阅:“?”在System.out.print中做什么?@Saad请参阅: