C递归程序崩溃

时间:2016-04-07 13:14:23

标签: c recursion crash

我是整个编程工作的初学者,我正在从一本书中进行编程练习。我已经到了本书的一部分,我应该编写递归函数。有些程序崩溃,我不知道我做错了什么。请比较这两个:

  1. 这应该从键盘读取一个数字并返回号码! (数字因子)。我尝试输入数字

    后崩溃了
    #include <stdio.h>
    
    int number, x;
    int factorial (int a);
    
    int main() {
    
    printf("Input a nonnegative whole number: \n");
    scanf("%d", &number);
    
    int x = factorial(number);
    printf("Factorial of %d equals %d", number, x);
    
    return 0;
    }
    
    int factorial (int a){
    if(a=0) {
            return 1;
        } else {
            return a*factorial(a-1);
        }
    }
    
  2. 这应该从键盘读取n并返回字符串中第n个元素的值。这个不会崩溃。

    #include <stdio.h>
    
    int n;
    int string (int a);
    
    int main() {
    
        printf("Input a nonnegative whole number: \n");
        scanf("%d", &n);
        int x = string(n);
        printf("%d element of the string equals %d", n, x);
    
        return 0;
    }
    
    int string (int a) {
        int value;
        if (a<=1) {
            value = 1;
        } else {
            value = string(a-1) + 2*string(a-2) + 3;
        }
        return value;
    }
    
  3. 这两个程序对我来说几乎是一样的。我无法弄清楚为什么第一个崩溃而第二个没有崩溃。请帮助,提前谢谢。

1 个答案:

答案 0 :(得分:2)

您使用了if(a==0)但这里的等号将0分配给a而不是比较a和0。 请改用@Entity @Table(name = "customer_data") public class BsslpCustomerInfo { @Id @Column(name = "id", length = 10) @GeneratedValue(strategy = GenerationType.AUTO) private int id; @Column(name = "service_Id", length = 50, unique = true, nullable = false) private String serviceId; @Column(name = "quarter", length = 255, nullable = false) private String quarter; @Column(name = "year") private int year; @Column(name = "activated_Customer_Name") private String activatedCustomerName; @Column(name = "deactivated_Customer_Name") private String deactivatedCustomerName; @Column(name = "quarter_Start_Date") private String quarterStartDate; @Column(name = "quarter_End_Date") private String quarterEndDate; public String getServiceId() { return serviceId; } public void setServiceId(String serviceId) { this.serviceId = serviceId; } public String getQuarter() { return quarter; } public void setQuarter(String quarter) { this.quarter = quarter; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getActivatedCustomerName() { return activatedCustomerName; } public void setActivatedCustomerName(String activatedCustomerName) { this.activatedCustomerName = activatedCustomerName; } public String getDeactivatedCustomerName() { return deactivatedCustomerName; } public void setDeactivatedCustomerName(String deactivatedCustomerName) { this.deactivatedCustomerName = deactivatedCustomerName; } public String getQuarterStartDate() { return quarterStartDate; } public void setQuarterStartDate(String quarterStartDate) { this.quarterStartDate = quarterStartDate; } public String getQuarterEndDate() { return quarterEndDate; } public void setQuarterEndDate(String quarterEndDate) { this.quarterEndDate = quarterEndDate; } public int getId() { return id; } public void setId(int id) { this.id = id; } }