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);}}
最佳答案
我冒昧地格式化了您的代码。这是该方法的样子:
double suma (int num [ ]) {
int i=num.length;
int j=0;
int s=0;
for(j=0;j < i;j++) {
return (double)(s);
}
}
我怀疑您试图编写一个 sum-method,从一个 stub 开始,但没有让它编译。这可能是您的想法:
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);
}
Java 编译器只能在非常简单的情况下推断语句(例如 return
)是可达的。 (为了说明:此方法确实编译。但仍然不是很有用!)
double suma (int num [ ]) {
int i=num.length;
int j=0;
int s=0;
for(j=0; true; j++) {
return (double)(s);
}
}
关于java - 数组 "missing return statement"出现 1 个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5164522/