java中必需的日历程序帮助
下面是我的日历程序,您可以看到:java中必需的日历程序帮助,java,loops,for-loop,calendar,Java,Loops,For Loop,Calendar,下面是我的日历程序,您可以看到: public class calendar { public static void main (String args[]) { new calendar (); } public calendar () { int day = IBIO.inputInt ("Enter day: "); int numOfDays = IBIO.inputInt ("Enter the number
public class calendar
{
public static void main (String args[])
{
new calendar ();
}
public calendar ()
{
int day = IBIO.inputInt ("Enter day: ");
int numOfDays = IBIO.inputInt ("Enter the number of days in the month: ");
System.out.println ("S M T W T F S");
if (day==1)
{
for (int i=1; i<=7; i++)
{
System.out.print (i + " ");
}
}
else if (day==2)
{
System.out.print (" ");
for (int i = 1; i <=6; i++)
{
System.out.print (i+" ");
}
}
else if (day==3)
{
System.out.print (" ");
for (int i = 1; i <=5; i++)
{
System.out.print (i+" ");
}
}
else if (day==4)
{
System.out.print (" ");
for (int i = 1; i <=4; i++)
{
System.out.print (i+ " ");
}
}
else if (day==5)
{
System.out.print (" ");
for (int i = 1; i <=3; i++)
{
System.out.print (i+" ");
}
}
else if (day==6)
{
System.out.print (" ");
for (int i = 1; i <=1; i++)
{
System.out.print (i + " ");
}
}
else
{
System.out.print (" ");
for (int i = 1; i <= 1; i++)
{
System.out.print (i+ " ");
}
}
}
}
公共类日历
{
公共静态void main(字符串参数[])
{
新日历();
}
公历()
{
int day=IBIO.inputInt(“输入日期:”);
int numOfDays=IBIO.inputInt(“输入月份的天数:”);
System.out.println(“S M T W T F S”);
如果(天==1)
{
对于(inti=1;i据我所知,您正试图通过将天数和开始日期作为用户的输入来打印日历
使用您编写的逻辑,它将只打印第一行,因为没有循环
或递归
。您应该做的是使用循环
打印所有行
以下是代码片段:
public class calendar
{
public static void main (String args[])
{
new calendar ();
}
public calendar ()
{
int day = IBIO.inputInt ("Enter Day (Number): ");
int numOfDays = IBIO.inputInt ("Enter the number of days in the month: ");
StringBuilder sb = new StringBuilder();
System.out.println("S M T W T F S");
char[] spaces = new char[(day - 1) * 3];
Arrays.fill(spaces,' ');
sb.append(spaces);
for(int i = 1; i <= numOfDays; i++) {
if(sb.length() > 19) {
System.out.println(sb.toString());
sb = new StringBuilder();
}
sb.append(i);
if(i / 10 < 1) {
sb.append(" ");
} else {
sb.append(" ");
}
}
System.out.println(sb.toString());
}
}
输出:
S M T W T F S
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
你的代码真的很复杂。我也不认为我的代码是最好的,但它是:
public calendar() {
int day = 2;
int numOfDays = 28;
System.out.println("S M T W T F S");
for(int i = 1; i < day; i++){
System.out.print(" ");
}
for(int i = 1; i <= numOfDays; i++){
System.out.print(addSpaces(i + "", 3));
if((i + day - 1) % 7 == 0){
System.out.print("\n");
}
}
}
public String addSpaces(String s, int length){
while(s.length() < length){
s+= " ";
}
return s;
}
这是另一个变体
static void cal( int day, int numOfDays ){
System.out.println(" S M T W T F S");
for( int d = 2 - day; d <= numOfDays; ++d ){
System.out.print( (d <= 0 ? " " : d <= 9 ? " " + d : d)
+ " " );
if( d%7 == (8-day)%7 ) System.out.println();
}
System.out.println();
}
静态无效校准(整数天,整数天){
System.out.println(“S M T W T F S”);
对于(intd=2-day;d您是否注意到代码中有一种可以简化的模式?这很有效,谢谢:)没问题,我正在度假,真的很无聊^^你能帮我修复一下我的nim程序吗?对不起,我一点都不了解nim。如果问题是逻辑问题,我可以试试,但我对语法一无所知。这是java游戏,哈哈。我只是在代码中的name变量上遇到了问题。
S M T W T F S
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
static void cal( int day, int numOfDays ){
System.out.println(" S M T W T F S");
for( int d = 2 - day; d <= numOfDays; ++d ){
System.out.print( (d <= 0 ? " " : d <= 9 ? " " + d : d)
+ " " );
if( d%7 == (8-day)%7 ) System.out.println();
}
System.out.println();
}