0

こんにちは、私は初心者で、よくわからないため、この問題に非常に腹を立てています。問題は、NESTED LOOPS を使用してカレンダーを作成することです。だれか助けてもらえますか? カレンダーは以下のようになります。日付は曜日に対応し、カレンダーの外観 (スペース、構造など) に似ている必要があります。

      Su   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

私が知っているのは、「x」で満たされた列と行を作成する方法だけです。

public class sumfOddsInNumber
{
    public static void main(String[] args)
    {
        for (int r = 1; r <= 6; r++)
        {
            for(int c = 1; c <= 7; c++)
            {
                System.out.print("x");
            }
            System.out.println();
        }
    }
}
4

2 に答える 2

1

これは実際にはプログラミングの問題ではなく、ロジックの問題です。連続で約4分間集中すれば、それを理解できたでしょう. でも、もう宿題に時間をかけている人はいないと思います。これが悪いプログラマが生まれる方法です。缶切りよりも野心を持つことを学んでください。


私はあなたが求めていることを正確に実行する、小さくてスタイリッシュな例を作成しました。

コードが最適化されていません。思ったようにそのままにしておきました(うん、4分)。時間をかけてこの例を確認し、改善してください。すべてはコメントで説明されています。

/**
 * The parameters indicate where the month starts,
 * and where it ends.
 *
 * @author ggrec
 *
 */
public class Calendar 
{
    private static final String WEEKDAYS = "Su Mo Tu We Th Fr Sa";
    private static final String NEW_LINE = "\n";
    private static final String EMPTY_STRING = " ";
    private static final String TRIPLE_EMPTY_STRING = "   ";

    public static void main(final String[] args) 
    {
        final String calendarString = getFormattedCalendar(4, 6);

        System.out.println(calendarString);
    }

    private static String getFormattedCalendar(final int startDay, final int endDay)
    {
        // Create StringBuilder
        final StringBuilder calendar = new StringBuilder();

        // Append weekdays to string header
        calendar.append(WEEKDAYS).append(NEW_LINE);

        // This will keep track of days
        int day = 1;

        for (int i = 1; i <= 5; i++) // Week loop
        {
            for (int j = 1; j <= 7; j++) // Weekday loop
            {
                // If we are on the last week of the month,
                // and we've reached the endDay that we specified,
                // simply return the assembled string
                if (i == 5 && j == endDay + 1)
                    return calendar.toString();

                // These are the empty spaces for the beginning of
                // the first week
                if (i == 1 && j < startDay)
                {
                    // Just append empty space, then CONTINUE
                    // to next iteration (j++)
                    calendar.append(TRIPLE_EMPTY_STRING);
                    continue;
                }

                // Check if the day is a single or double digit
                if (day / 10 >= 1)
                    calendar.append(day++).append(EMPTY_STRING);
                else
                    // If this is the first week, then it means that
                    // we have single-digit days. Apply strings on each
                    // side of the day for proper spacing of digits
                    calendar.append(EMPTY_STRING).append(day++).append(EMPTY_STRING);
            }

            calendar.append(NEW_LINE);
        }

        return calendar.toString();
    }
}
于 2013-09-03T00:56:36.620 に答える
1

これは宿題の問題のようですので、コードを提供するつもりはありませんが、あなたは正しい方向に向かっています. まず、私は変わります

System.out.print("x");

System.out.print("  x"); //Add two spaces in front of the x

そのため、数字の間にスペースがあります。次に、x の代わりに実際の数値を生成するには、ループのint dayOfMonth = 1;上に置きます。次に、x の代わりにfor印刷する必要があります。私があなたに残す問題は、毎回dayOfMonthの値をどのように増加させるかです。dayOfMonth

于 2013-09-03T00:28:12.153 に答える