【问题标题】:Functional Decomposition功能分解
【发布时间】:2013-12-01 18:23:48
【问题描述】:

您好,我编写了一个程序来为 CS 课程打印钻石;我被要求使用函数分解(我认为是全局变量?),但我不知道该怎么做;一些见解会帮助我;我的代码不是很漂亮;但它确实有效。

   public class SquareDiamondMid
 {
    public static void main(String args[]){
    System.out.println("printing Diamond");
    int number = BIO.getInt();
    int dotsTop = (number / 2) - 1;
    int count = 0;
    int LineN = 1;
    int DotsMiddle = 0;
    int mainCount = (number / 2)+1;
    int count1 = 0;
    int dot = (number / 2) - 1;

    while(count1<=dot){
        System.out.print(".");
        count1 = count1 + 1;
    }
    System.out.print("*");
    count1 = 0;
    while(count1<=dot){
        System.out.print(".");
        count1 = count1 + 1;
    }
    System.out.println(""); 

    while(mainCount<number){
        while(count<dotsTop){
            System.out.print(".");
            count++;
        }
        System.out.print("*");
        while(DotsMiddle<LineN){
            System.out.print(".");
            DotsMiddle++;
        }
        System.out.print("*");
        count = 0;
        while(count<dotsTop){
            System.out.print(".");
            count++;
        }
        System.out.println("");
        LineN = LineN + 2;
        DotsMiddle = 0;
        dotsTop--;
        mainCount++;
        count=0;
    }
    //bottom
    int bottomLineN=1;
    int numberOfBottom = (number / 2) - 2;
    int bottomCount = 0;
    int mainBottomCount=0;
    int bottomMidCount = 0;
    int bottomMidDot = number - 4;
    int bottomCount2 = 0;

    while(mainBottomCount<=numberOfBottom){

        while(bottomCount<bottomLineN){
            System.out.print(".");
            bottomCount++;
        }
        System.out.print("*");
        while(bottomMidCount<bottomMidDot){
            System.out.print(".");
            bottomMidCount++;
        }
        System.out.print("*");

        while(bottomCount2<bottomLineN){
            System.out.print(".");
            bottomCount2++;
        }

        bottomCount2 = 0;
        bottomMidCount = 0;
        bottomMidDot = bottomMidDot - 2;
        System.out.println("");
        mainBottomCount++;
        bottomCount=0;
        bottomLineN = bottomLineN + 1;
    }

    int count3=0;
    while(count3<=dot){
        System.out.print(".");
        count3 = count3 + 1;
    }
    System.out.print("*");
    count3 = 0;
    while(count3<=dot){
        System.out.print(".");
        count3 = count3 + 1;
    }
    System.out.println(""); 
}

提前谢谢`。

【问题讨论】:

  • 我猜你老师的意思是把所有这些代码分解成类中执行部分工作的更小的方法。
  • 提示:不是全局变量。
  • 是的 Luiggi 我必须将程序缩减为方法

标签: java decomposition


【解决方案1】:

功能分解意味着将其分解为流程执行的多个离散步骤。

例如,泡一杯茶可能涉及:

  • 拿一杯
  • 将茶包放入杯中
  • 往水壶里放水
  • 烧开水
  • 在装满茶包的杯子里加水

这些步骤中的每一个都可以封装在一个函数中,该函数接受输入(比如说一个空杯子)并返回一个结果(一个装有茶包的杯子)。

现在查看您的代码。它执行哪些步骤?它们是否可以表示为某种东西,它接受世界的输入或初始状态并做一些工作以产生输出?

打印出一些点可能符合条件...这需要什么输入?该函数需要对该输入做什么?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    • 2021-12-13
    • 2012-10-31
    • 2014-07-05
    相关资源
    最近更新 更多