- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在研究数据结构问题。下面列出了作业和我迄今为止所做的事情。
import java.util.Scanner;
import java.util.Random;
/**
* Recursive methods for fun & profit
*
* @author (your name)
* @version (a version number or a date)
*/
public class DSLab2
{
/**
* Prints 2 lines of text for each recursive call, indicating call number
* (a value >= 1, and <= value of numCalls, as illustrated below. Each
* level of recursion should be indicated by indenting the input line by
* r spaces. For example, if numCalls is 3, the method should print:
* call 1
* call 2
* call 3
* back 3
* back 2
* back 1
* @param r the level of method calls
* @param numCalls the number of intended levels
*/
public static void stairSteps (int r, int numCalls)
{
System.out.println("call "+r);
System.out.println("back "+r);
}
我尝试了以下方法,但它只输出阶梯要求的下半部分。我无法弄清楚如何递归调用该方法,以便在每次调用中打印这两行而不是彼此相邻
。
public static void stairSteps (int r, int numCalls)
if(numCalls>0)
{
for ( int i = 0; i <numCalls ; i++){
System.out.print(" ");
}
System.out.println("back " + r);
stairSteps(r-1, numCalls-1);
}
else
return;
有什么建议吗?
最佳答案
public static void stairSteps (int r, int numCalls)
{
if ( numCalls==0 )
return ;
for (int i=0; i<r; i++)
System.out.print(" ");
System.out.println("call "+r);
stairSteps(r+1, numCalls-1) ;
for (int i=0; i<r; i++)
System.out.print(" ");
System.out.println("back "+r);
}
public static void main(String argv[]) {
stairSteps(1, 4) ;
}
call 1
call 2
call 3
call 4
back 4
back 3
back 2
back 1
关于java - 使用阶梯模式为每次调用递归打印两行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49268630/
有没有办法在下面的代码中避免 if..else 条件并在一行中执行此操作? var myObject = {}, data = ["Chennai", "Thoothukkudi", "Mad
我有一个客户想要在他们的主页上使用一个特定的设计来匹配他们在杂志封面上看到的内容。封面的基本阶梯设计从未改变,但图像偶尔会改变。如果不使用 1 个大图像,我无法设计出一种方法来覆盖这种格式。 这是一个
我正在尝试解决 HackerRank 中的一个问题,但我的提交有问题。我的代码在 PyCharm 中有效,但 HackerRank 不接受我的提交。 这是我要解决的问题:https://www.hac
嗨,我是个新手,正在尝试通过 hackerrank 来提高,我正在进行楼梯练习 staircase excercise 但是我的输出与问题不同,因为我的楼梯似乎在结果前面有一个额外的空间,从而使其不正
我正在尝试解决 this使用 std::cout 的 C++ 问题并使用 setw和 setfill 我原来的代码是这样的: void staircase(int n) { for(int i
问题可用here .我的 Python 代码是 def solution(A, B): if len(A) == 1: return [1] ways = [0] *
这是我创建的 IF -Else 阶梯,用于将第一个可见控件聚焦在我的表单上。根据要求,任何控件都可以隐藏在表单上。所以我必须找到第一个可见控件并聚焦它。 if (ddlTranscriptionMe
我正在尝试构建一个系统,用户可以在其中轻松创建各种类型的锦标赛并与其他用户一起参与。它不针对特定游戏,而是一种通用工具,用于在任何设备上尽快查看和更新锦标赛结果,交互最少,因此实际应用程序不会妨碍
我正在使用 ORACLE 数据库,并且正在尝试为以下要求创建 SQL 查询。 以下是我的表快照: ID STATUS ---------- 1 WORKING 1 QUEUIN
我在用, JPA 2.0 Mojarra 2.1.9 JSF 组件库,Primefaces 3.5。 MySQL 5.6.11 我在 MySQL 数据库中有一个名为 state_table 的表以三列
我是一名优秀的程序员,十分优秀!