开发者

1 error with an array "missing return statement"

import javax.swing.JOptionPane;

public class Mate {

double suma (double x1,double x2)
    {return x1+x2开发者_开发百科;}

double suma (double x1,double x2,double x3)
    {return x1+x2+x3;}

double suma (int num [ ])

    {int i=num.length;
     int j=0;
     int s=0;

for(j=0;j < i;j++)

{return (double)(s);}}} // here appears the error "missing return statement"

class AplicacionMate

{public static void main (String arg [])

    {int n[ ]={5,4,3,2,1};
     double r=0.0;
     Mate m=new Mate ( );
     r=m.suma(5,4);
     JOptionPane.showMessageDialog(null,"La suma 1="+r);
     r=m.suma(5,5,4);
     JOptionPane.showMessageDialog(null,"La suma 2="+r);
     r=m.suma(n);
     JOptionPane.showMessageDialog(null,"La suma del arreglo="+r);
     System.exit(0);}}


I took the liberty of formatting your code. This is how the method looks:

double suma (int num [ ]) {
    int i=num.length;
    int j=0;
    int s=0;

    for(j=0;j < i;j++) {
        return (double)(s);
    }
}

I suspect you attempted to write a sum-method, started with a stub, and didn't get it to compile. This is probably what you had in mind:

double suma (int num [ ]) {
    int i=num.length;
    int j=0;
    int s=0;

    for(j=0;j < i;j++) {
        // here you probably want s += num[j];
    }

    return (double)(s);

}

The Java compiler can only deduce that a statement (such as a return) is reachable in very simple cases. (To illustrate: this method does compile. Still not very useful though!)

double suma (int num [ ]) {
    int i=num.length;
    int j=0;
    int s=0;

    for(j=0; true; j++) {
        return (double)(s);
    }
}


for(j=0;j < i;j++)

{return (double)(s);}}} // here appears the error "missing return statement"

There might be case when it doesn't get into loop so it won't return anything.

You need to make sure that it should return for all the case

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜