import java.util.Scanner;
public class Question5 {
public static void main(String [] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a number: ");
int x = keyboard.nextInt();
System.out.print("Enter a char: ");
String c1 = keyboard.nextLine();
System.out.print("Enter a char: ");
String c2 = keyboard.nextLine();
printChars(x, c1, c2);
}
public static void printChars(int x, String c1, String c2) {
for (int i = 1 ; i <= x ; i++) {//
for (int z = 1 ; z <= x - i ; z++)//
System.out.print(c1);
for (int j = 1 ; j <= x ; j++)//
System.out.print(c2);
System.out.println();
}
}
}
它是我现在得到的输出
----jGRASP exec: java Question5
Enter a number: 8
Enter a char:
Enter a char:
6
66666666
66666666
66666666
66666666
66666666
66666666
66666666
66666666
----jGRASP: operation complete.
但它应该像这样打印
Enter a number: 5
Enter a char: %
Enter a char: $
% % % % %
% % % % $
% % % $ $
% % $ $ $
% $ $ $ $
$ $ $ $ $
请帮助我,这是我在这个论坛上的第一个问题
答案 0 :(得分:1)
不要使用keyboard.nextLine()
,而只需使用keyboard.next()
来阅读字符串。
答案 1 :(得分:0)
这看起来像是一个家庭作业问题;不要指望这里的作业答案。相反,尝试自己调试,它更有启发性。
首先,尝试并清楚地向自己(或其他人)描述什么行为确实是'错误的'(例如捕获输入或处理捕获的输入)。它也可能有助于将程序分解为两个部分,例如
printChars
方法的内容也出乎意料。确定首先要解决的部分(就个人而言,我首先要解决printChars
方法,因为我确切地知道了我对它的期望)并有条不紊地进行处理。
P.S。你真的很亲密,所以不要放弃。