2017-10-31 1 views
0

가능한 경우 배열이없는 메서드, 매개 변수 및 루프를 사용하여 사용자의 길이와 너비를 사용하는 별표 상자를 만들고 싶습니다. 왜냐하면 나는 아직 그것을 배웠지 않았기 때문이다.) 지금까지 별표 중 한 줄만 표시했는데,이 시점을 지나서 무엇을해야할지 모르겠다. 도와주세요!사용자에게 길이와 너비를 묻는 메시지를 표시하고 별표 상자를 표시하는 방법 displaybox 응용 프로그램을 만드는 방법

public static void drawBar(int length, String mark) { 

    for (int i = 1; i < length; i++) { 
     System.out.print(mark); 
    } 
} 

public static void main(String[] args) { 

    Scanner input = new Scanner(System.in); 

    System.out.println("Enter a height: "); 
    int length1 = input.nextInt(); 
    System.out.println("Enter a width: "); 
    int length2 = input.nextInt(); 

    for (int j = 1; j < length1; j ++) { 
     drawBar(j, "*"); 
    } 

} 

}

+0

행을 쓰는 루프를 감싸는 높이를 관리하려면 두 번째 루프가 필요합니다. – MadProgrammer

+0

@madprogrammer 그는 이미 서로 랩하는 두 개의 루프를 가지고 있습니다. 코드는 조금 이상하게 구성되어 있습니다. @ 자키 : 길이 변수의 범위가 올바르지 않습니다. 너는 length2 대신에 drawBar에'j'를 전달한다. 나는 당신이 그것을 고치면 올바르게 작동 할 것이라고 생각합니다. drawBar의 for 루프를 첫 번째 메서드에 직접 배치 할 수도 있습니다. 이렇게하면 코드가 읽고 제어 흐름을 더 쉽게 이해할 수 있습니다. –

+0

@MadProgrammer Java에 대해 잘 알고 있으므로 높이 관리를 위해 두 번째 루프를 설정하는 방법을 보여줄 수 있습니다. – trashprogrammer

답변

1

는이 같은 것을 사용할 수 있습니다

public static void main(String[] args) throws UIException { 

    Scanner input = new Scanner(System.in); 

    System.out.println("Enter a height: "); 
    int height = input.nextInt(); 
    System.out.println("Enter a width: "); 
    int width = input.nextInt(); 

    String mark = "*"; 

    for (int i = 0; i < height; i++) {//Number of lines it will repeat the process 
     System.out.println(new String(new char[width]).replace("\0", mark));// "\0" is nul character that will be replace the N number of times needed to fill the width. 
    } 
} 

당신은 또한 당신이 항상 0

에 카운트 시작 프로그래밍 언어의 필요, 0 for 루프를 시작해야
관련 문제