这段代码有什么问题? [英] What is the problem in this code?

查看:91
本文介绍了这段代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Lab2 {
int car1,car2,car3,m,n;
public void setNumber(int a) {
	n=a;
}
public void setSpeed(int a,int c ,int b) {
	car1=a;
	car2=c;
	car3=b;
}
public void setMax(int a) {
	m=a;
}
public int getNumber() {
	 return car1;
}
int getSpeed() {
	int [] myArray = new int[3];
        myArray[0] = car1;
        myArray[1] = car2;
        myArray[2] = car3;
        for(int i=1;i<myArray.length;i++)
      return myArray[i];
}
public int getMax() {
	 return car1;
}
public void display()
{
	
	System.out.println("Speed of cars\n \t"+getSpeed());
	System.out.println("N.o of cars:"+getNumber());
	System.out.println("Max speed:"+getMax());
}
}
class dehmi
{public static void main (String args[])
{
	Lab2 obj1= new Lab2();
    obj1.setNumber(3);
    obj1.setMax(90);
    obj1.getNumber();
    obj1.getMax();
    obj1.display();
}
}





我的尝试:



我正在使用jcreator它说的问题是在快速的braket,帮助我PLZ我在这里死了):



What I have tried:

i'm using jcreator it's saying that the problem is in getspeed braket, help me plz i'm dying here ):

推荐答案

我认为你已经忘记了你的 getSpeed 函数:为什么你的循环的是正确的只是回归?这将永远只返回 car2的速度



编译器告诉你的可能性很大about:有一个不符合return语句的方法的逻辑路径 - 如果array.Length小于或等于1 - 好的,实际上它不会发生,但编译器不知道,所以它正确地抱怨。



我怀疑你需要多考虑一下你在这里想要做什么,这段代码看起来很糟糕没有一个很多前瞻性规划...
I think you have forgotten something in your getSpeed function: why do you have a for loop whose body is just a return? And that will always just return the speed of car2

The chances are that is what the compiler is telling you about: there is a logical path through the method that does not meet a return statement - if the array.Length was less than or equal to one - OK, in practice it can't happen, but the compiler doesn't know that, so it rightly complains.

I suspect that you need to think a little more about what you are trying to do here, that code looks awfully "thrown together" without a lot of forward planning...


int getSpeed() {
  int [] myArray = new int[3];

  myArray[0] = car1;
  myArray[1] = car2;
  myArray[2] = car3;
  
  for(int i = 1; i < myArray.length; i++)
    return myArray[i];
}



当您格式化 getSpeed 方法时,它看起来像这样...

它看起来非常错误,可能不是你的意思...你的for循环实际上是空的(因为它在第一次迭代时返回)...它也以一个很好的1开始,你的数组索引来自零...

您必须根据您为此方法为自己设定的规格重新设计...


When you are formatting your getSpeed method it looks like this...
It looks very wrong, and probably not what do you meant... your for loop is actually empty (as it returns on the first iteration)... It also starts with a nice 1 where you array indexed from zero...
You have to redesign it according the specification you set for yourself for this method...


这篇关于这段代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆