我必須編寫一個程序,需要輸入兩個字符,並使用方法將它們打印出x
次。到目前爲止,它會輸出數字而不是字符。我該如何解決它?如何輸入字符並輸出一定次數?
int length;
char ch1;
char ch2;
System.out.print("Enter a character: ");
ch1 = input.nextLine().charAt(0); //input refers to scanner.
System.out.print("Enter second character: ");
ch2 = input.nextLine().charAt(0); //input refers to scanner.
System.out.print("Enter the length of the line: ");
length = input.nextInt(); //input refers to how many times the characters ar$
draw_line(length, ch1, ch2);
//Method starts here.
public static void draw_line(int length, char ch1, char ch2){
for (int i = 0; i < length; ++i){
System.out.print(ch1 + ch2);
}
}
下一次當你發佈一個問題,也請嘗試使它成爲一個完整的工作代碼 - 你顯然削減了它的一部分程序,並有不平衡的括號。 – sashkello
@sashkello對不起! –