【发布时间】:2014-12-29 01:01:45
【问题描述】:
我无法让我的程序减去用户在下一行输入的字符,直到它达到零。例如,用户输入 5 作为长度和字符 Y,程序应在第一行打印五个“Y”,然后在第二行打印四个“Y”......直到零。像这样...
ln(YYYY)
ln(YYYY)
ln(YYY)
ln(YY)
ln(Y)
我不能让程序超过我会得到的第一行:
ln(YYYY
ln(Y)
ln(Y)
ln(Y)
ln(Y)
我有什么:
int length;
char d; // tried using only char 'd' but scanner has a hard time with chars, so I used String
String UserChar;
//scanner is needed
Scanner sc = new Scanner(System.in);
//get user data and initialize variables
System.out.println("Please input a positive whole number.");
length = sc.nextInt();
sc.nextLine();
System.out.println("Please input one character.");
UserChar = sc.next();
sc.nextLine();
sc.close();
//do computation
for(int a = length; a > 1 ; a = a - 1) //prints user input on first line
{
System.out.print(UserChar);
}
for(int i = 0; i < length; i = i + 1) //how many lines get printed
{
System.out.println(UserChar);
}
// print results (occurs in previous step)
}
}
【问题讨论】:
标签: for-loop input char subtraction