让两个方法来回调用有什么缺点吗?

问题描述 投票:0回答:1

在进行学校练习时,我发现自己来回调用两个方法,直到用户激活停止标志。检查老师的答案,他们使用了 while-loop 来代替循环这两个方法。

我想知道以我的方式这样做是否有缺点(也许在资源方面?)。

免责声明:我在这段代码中有一些不好的做法(混合语言,调用两种方法来启动脚本),请不要批评我,因为我知道这些。

有问题的代码是: exe() -> 切换情况 1 或 2 -> exe() 再次被调用 -> ...

package cui;

import java.util.Scanner;
import domein.*;

public class VormApplicatie
{
int iterations;
int legalRectangles;
int legalTriangles;

    public static void main(String[] args)
    {
         new VormApplicatie().beginProcess();
    }
    
    private void beginProcess()
    {
        iterations = 0;
        legalRectangles = 0;
        legalTriangles = 0;
        System.out.println("Rechthoeken en driehoeken\n-------------------------");
        exe();
    }
    
    private void exe()
    {
        Scanner input = new Scanner(System.in);
        
        int choice;
        
        do
        {
            System.out.printf("0: quit\n1: rectangle\n2: triangle\nPlease input choice:");
            choice = input.nextInt();
        
        }while(choice < 0 && choice > 2);
        
        switch(choice)
        {
        case 0 -> finishProcess();
        case 1 -> rectangleFunc(input);
        case 2 -> triangleFunc(input);
        default -> {System.out.println("Unexpected error occured."); finishProcess();}
        }
        input.close();
    }
    
    private void rectangleFunc(Scanner input)
    {
        int length, width;
        
        System.out.print("Please input length: ");
        length = input.nextInt();
        
        System.out.print("Please input width: ");
        width = input.nextInt();
        
        iterations++;
        Rechthoek localRect = new Rechthoek(length, width);
        legalRectangles += localRect.berekenOppervlakte() > 50 ? 1 : 0;
        
        exe();
        
        /*int choice = makeChoiceToContinue(input);
        
        switch(choice)
        {
        case 0 -> System.out.println("Process stopped.");
        case 1 -> finishProcess();
        case 2 -> exe();
        }*/
    }
    
    private void triangleFunc(Scanner input)
    {
        int first,second,third;
        
        System.out.print("Please input length of first side: ");
        first = input.nextInt();
        System.out.print("Please input length of second side: ");
        second = input.nextInt();
        System.out.print("Please input length of third side: ");
        third = input.nextInt();
        
        iterations++;
        Driehoek localTri = new Driehoek(first, third, second);
        legalTriangles += localTri.isRechthoekig() == true ? 1 : 0;
        
        exe();
        /*
        int choice = makeChoiceToContinue(input);
        
        switch(choice)
        {
        case 0 -> System.out.println("Process stopped.");
        case 1 -> finishProcess();
        case 2 -> exe();
        }*/
    }
    
    private void finishProcess()
    {
        System.out.printf(
                "Overzicht vormen:\n\n"
                + "Totaal aantal vormen: %d\n\n"
                + "Aantal rechthoeken met opp > 50: %d\n\n"
                + "Aantal rechthoekige driehoeken: %d",
                iterations, legalRectangles, legalTriangles);
    }
    
    /*private int makeChoiceToContinue(Scanner input)
    {
        int choice;
        do
        {
            System.out.print("Do you want to stop(0) or continue(1)?\nInput corresponding number: ");
            choice = input.nextInt();
        }while(choice != 0 && choice != 1 && choice != 2); 
        return choice;
    }*/

}
java loops methods
1个回答
0
投票

是的。当一个方法返回时,它会返回到调用它的地方。因此,系统需要记住

return;
应该去哪里。在你的代码中,你永远不会真正返回(
exe()
永远不会返回;相反,它会调用例如
triangleFunc
triangleFunc()
永远不会返回;相反,它会调用
exe()
) - 但这并不重要,系统不知道这些永远不会回来。所以,一定要记住。每次你调用一个方法时,你调用它的位置from都会写在一个小便条上,并且该便条会添加到堆栈中,这就像你将订单粘贴到上面的那些旧时代的大图钉之一。每当方法返回时,都会从 pin 中取出顶部音符以知道返回到哪里。

您的代码永远不会返回,因此图钉上的那叠笔记会不断增加。最终,它长得太大,以至于销钉没有空间了。此时,将会出现

StackOverflowError
并且您的应用程序将崩溃。

这有可能发生吗?呃,你可能需要按 1/2 几千次。但是,关键是,由于这个原因,这是“糟糕的代码”,除了学术/只是为了学习之外,没有人为任何项目以这种方式编写它。使用这种来回调用的方式不会通过代码审查,或者在许多情况下实际上最终会导致实际的麻烦。因此,这根本不是它的做法。如果我是你的老师,我会给你不少减分。

© www.soinside.com 2019 - 2024. All rights reserved.