2013-09-03 2 views
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(); 
     } 
    } 
} 
+0

StringBuilder, append() 메서드 및 toString() 메서드입니다. – kosa

+0

수요일에 시작해야합니까? – nachokk

+0

예 wensdayy에서 시작 –

답변

1

이렇게 보이는 것이 숙제 문제이므로 코드를주지는 않겠지 만 올바른 방향으로 가고 있습니다. 첫째, 그래서 당신은 숫자 사이에 공간이

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

System.out.print("x"); 

을 변경할 것입니다. 다음으로, x 대신 실제 숫자를 생성하려면루프 위에 int dayOfMonth = 1;을 입력하십시오. 그러면 x 대신에 dayOfMonth을 출력하고 싶을 것입니다. 내가 당신에게 남긴 문제는 매번 dayOfMonth 값을 증가시키는 것입니다.

+0

System.out.print ("x");의 "공백"수는 하루의 숫자가 항상 같은 자릿수가 아니기 때문에 달라야한다고 생각합니다. – pinckerman

+0

사실 인쇄 할 정확한 숫자에 따라 다를 수 있지만 OP가해야 할 첫 번째 일은 숫자 인쇄를하고 그 다음에 올바른 날부터 인쇄하는 것입니다. 그 작업이 끝나고 숫자를 줄 때가되면'String.format()'이 유용 할 것입니다. –

+0

그가 중첩 루프를 간신히 수행 할 수 있다면 String.format()을 사용하는 방법을 배워야한다고 생각합니다. – pinckerman

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(); 
    } 
}