0
public double Convertir(double Number) {
    Number = nombre;
    while ((Number - 365) >= 0) {
        annee += 1;   //this calculates the number of years    
    }
    return annee;
    double nombreSemaine = Number - (annee * 365);
    while ((nombreSemaine - 7) >= 0) {
        semaine = semaine + 1;
    }//this calculates the number of weeks
    return semaine;
    double nombreJour = Number - (annee * 365) - (semaine * 7);
    nombreJour = jour;
    return jour;
}

使用此代码,我试图将用户编写的数字(天数)转换为它所产生的年数、周数和天数。例如,数字 365 应返回 1 年 0 周 0 天。

4

7 回答 7

5

return annee; returns annee so anything after this expression in the method won't get executed.

Perhaps you could return an Array instead:

public double[] Convertir(double Number) {
    Number = nombre;
    double[] all = new double[3];
    while ((Number - 365) >= 0) {
        annee += 1;   //this calculates the number of years    
    }
    all[0] = annee;
    double nombreSemaine = Number - (annee * 365);
    while ((nombreSemaine - 7) >= 0) {
        semaine = semaine + 1;
    }//this calculates the number of weeks
    all[1] = semaine;
    double nombreJour = Number - (annee * 365) - (semaine * 7);
    nombreJour = jour;
    all[2] = jour;

    return all
}

or something similar. An ArrayList would probably be better...but it's the same general concept.

于 2013-03-12T14:20:16.923 回答
5

The code below return annee; won't be executed.

It looks like you want to return 3 values. You can only return 1 value, a double in this case.

Solution 1 (Global variables):

int annee, semaine, jour; //global variables

public void Convertir(int Number) { //I guess number should be an Int too, unless it's possible to pass 567.28 days...
    //Number = nombre; Useless since this is a parameter
    annee = (int)(Number/365);
    semaine = (int)((Number - annee * 365)/7);
    jour = Number - annee * 365 - semaine * 7;
}

Solution 2 (return an array):

public int[] Convertir(int Number) { //I guess number should be an Int too, unless it's possible to pass 567.28 days...
    //Number = nombre; Useless since this is a parameter
    int[] anneeSemaineJour = new int[3];
    anneeSemaineJour[0] = (int)(Number/365);
    anneeSemaineJour[1] = (int)((Number - anneeSemaineJour[0] * 365)/7);
    anneeSemaineJour[2] = Number - anneeSemaineJour[0] * 365 - anneeSemaineJour[1] * 7;

    return anneeSemaineJour;
}

You will then use it like this (Solution 2):

int[] resultat = convertir(822); // convertir(nombre) in your case I guess
System.out.println("Annee = " + resultat[0] + " Semaine = " + resultat[1] + " Jour = " + resultat[2]);
于 2013-03-12T14:21:08.757 回答
4

The problem is you have code (including another return) after a return statement. A return statement stops the function at that place and returns the value. Anything after that is unreachable.

于 2013-03-12T14:20:36.057 回答
1

您的代码存在许多问题。

除了其他人所说的(下面的所有内容都return不会被执行)你应该小心你的while循环,它们是无限循环:

while ((Number - 365) >= 0) {
        annee += 1;   //this calculates the number of years    
}

如果Number - 365 >= 0那时你在里面while并且你正在添加1annee,这不会停止循环,因为Number - 365 >= 0会继续得到满足。

你的第二个循环也是如此。

于 2013-03-12T14:28:08.050 回答
0

“return”退出该方法。如果您想返回所有这些(年、月、日),您可以使用数组。您的代码通常有很多错误,并且多次对 nombre(或您拥有的 Number)进行了类似的操作。我试图使代码可运行。

public double[] Convertir(double nombre) {
    double[] yearsWeeksAndDays = new double[3];
    double annee = 0;
    double semaine = 0;
    while (nombre >= 365) {
        annee += 1;   //this calculates the number of years
        nombre -= 365;    
    }
    yearsWeeksAndDays[0] = annee;
    while (nombre >= 7) {
        semaine = semaine + 1;
        nombre -= 7;
    }//this calculates the number of weeks
    yearsWeeksAndDays[1] = semaine;
    yearsWeeksAndDays[2] = nombre;
    return yearsWeeksAndDays;
}
于 2013-03-12T14:27:38.707 回答
0

您需要将 3 个返回值包装成 aclass并返回它。像这样的东西应该可以工作:

public static void main(String[] args) {
    System.out.println(convertir(365));
    System.out.println(convertir(366.5));
    System.out.println(convertir(456));

}

static class Duration {

    int years;
    int weeks;
    double days;

    @Override
    public String toString() {
        return years + " years, " + weeks + " weeks and " + days + " days.";
    }
}

public static Duration convertir(double total) {
    final Duration duration = new Duration();
    duration.years = (int) (total / 365);
    total = total % 365;
    duration.weeks = (int) (total / 7);
    total = total % 7;
    duration.days = total;
    return duration;
}

输出:

1 years, 0 weeks and 0.0 days.
1 years, 0 weeks and 1.5 days.
1 years, 13 weeks and 0.0 days.

显然它需要一点翻译,但法语不是我的强项。

于 2013-03-12T14:29:23.977 回答
-1

它抛出无法访问的代码,因为编译器知道当控件到达该返回语句时,它会返回并且不会执行更多代码。要解决这个问题,您可以做的是,将返回语句放在条件块中,就像我有如下所示,但同样,这个程序不会返回你想要的结果。它只会返回一年。如果你想要整个结果,即。年数 + 周数 + 天数 我建议您对单个字符串做出整个答案并返回。

public double Convertir(double Number) {
        // Number = nombre;
        double annee = 0;
        double semaine = 0;
        double jour = 0;
        while ((Number - 365) >= 0) {
            annee += 1; // this calculates the number of years
        }
        if (annee > 0) {
            return annee;
        }
        double nombreSemaine = Number - (annee * 365);
        while ((nombreSemaine - 7) >= 0) {
            semaine = semaine + 1;
        }// this calculates the number of weeks
        if (semaine > 0)
            return semaine;

        double nombreJour = Number - (annee * 365) - (semaine * 7);
        nombreJour = jour;
        return jour;
    }
于 2013-03-12T14:33:42.920 回答