我正在努力了解此代码中发生了什么。在我们为家庭作业提出的14个问题中,这些是我遇到的唯一2个问题而且只是在不知道他们如何得到答案的情况下放下答案。任何帮助将不胜感激,谢谢。
这是#1:
public void printDollarSign(int k)
{
int j;
if (k>0)
{
for (j=1; j<= k; j++)
System.out.print("$");
System.out.println();
printDollarSign(k-1);
}
}
如果呼叫是:printDollarSign(5); ?
$$$$$
$ $ $ $
$$$
$$
$
这是#2:
public void bbb(String s, int p)
{
if (p>= 0)
{
bbb(s,p-1);
System.out.print(s.charAt(p));
}
}
如果呼叫是:bbb(“January”,4),输出结果如何; ?
Janua
答案 0 :(得分:0)
解释 printDollarSign :
对于第一个电话:
答案 1 :(得分:0)
printDollarSign(k)首先打印k个$,然后打印换行,然后先打印printDollarSign(k-1)打印k-1个$,然后打印换行然后再打电话printDollarSign(k-1-1)......这一直持续到k = 0。当k = 0时,printDollarSign(0)不打印任何内容。
答案 2 :(得分:0)
你的第一个是创建一个带有int特定参数的函数,在这种情况下&#34; k&#34;。
代码:
public void printDollarSign(int k){ //Define function
int j;//Define j as an int
if (k>0){//Check if k is greater than 0
for (j=1; j<= k; j++)//Loop through k where j = 1
System.out.print("$");//Print $ by the amount of k
System.out.println();//Print a new line
printDollarSign(k-1);//Re run the function
}
}
你的第二个问题是创建一个带有两个参数string和int&#34; s&#34;和&#34; p&#34;
代码:
public void bbb(String s, int p){//Define Function
if (p>= 0){ //Check if p is greater than 0
bbb(s,p-1);//Rerun function
System.out.print(s.charAt(p));//Print character of string based on p
}
}
答案 3 :(得分:0)
添加代码注释以供解释:
1#
public void printDollarSign(int k)
{
// temporary variable j, it will be always initialized to 1 inside the for loop.
int j;
// only executed to be true if k is more than 0, that means if K is initially 5
// then it only works for 5,4,3,2,1 and not for 0.
if (k>0)
{
// always starts with 1 and goes to the value of k, that means if K is currently 5
// then it will print 5 dollars, if 4 then 4 dollars and so on
for (j=1; j<= k; j++)
System.out.print("$");
// a new line will be added once dollars are printed.
System.out.println();
// this will again call the same function and decrements the value of k, so next time
// k will have the value one less then the previous one, if this has printed 5 dollars
// in last iteration next time it will print 4 dollars and so on
printDollarSign(k-1);
}
}
2#
public void bbb(String s, int p)
{
// only print a character if p has a value grater than 0. in you case , p has a value 4 that
// mean only 4 characters will be printed at max
if (p>= 0)
{
// recuresively call same method by decrementing p, so it will be
// [bbb(s,3) prints 'a']-> [bbb(s,3) prints 'u']-> bbb(s,2) [prints 'n']-> [bbb(s, 1) prints 'a']> [bbb(s, 0) prints 'J']
// last character will be printed first
bbb(s,p-1);
// prints the character at p location
System.out.print(s.charAt(p));
}
}
答案 4 :(得分:0)
bbb 的说明:
执行:
现在弹出每个元素并在该位置打印char
答案 5 :(得分:0)
if you provide a string s as 'January', the way it stores is like this:
0th position = J,
1th position = a,
2th position = n,
3th position = u,
4th position = a,
5th position = r,
6th position = y,
Just like an array of characters.
When you provide p=4, you are setting the closing criteria for the condition check.
function bbb('January', 4){
if(4>=0){
bbb('January', 3); ....
function bbb('January', 3){
if(3>=0){
bbb('January', 2); ....
function bbb('January', 2){
if(2>=0){
bbb('January', 1); ....
function bbb('January', 1){
if(1>=0){
bbb('January', 0); ....
function bbb('January', 0){
if(0>=0){
bbb('January', -1); ....
function bbb('January', -1){
if(-1>=0){ Here condition check fails.. hence char at(-1) doec not print
return goes back to print of previous call and prints J as p=0, we have J
the p=1: a
p=2: n
p=3: u
p=4: a
和功能完成......
如果解释有用,请告诉我