2015-04-06 122 views
0

我正在研究产品(无特定项目)程序的某些代码。这里是我需要帮助的学习:方法中的数组元素并返回数组返回值

我需要两个数组元素(数字[4]和数字[5]),创建一个方法,将它们加在一起,然后返回并将它们存储回数组元素数[6]。

如何创建一个可以读取已存储在数组中的信息的方法?

这里是我的代码:

// This is an inventory program part 1 "Section 1" 
public class inventory { 

    public static void main(String[] args) { 

     String[] inventory = new String[8]; 

     inventory[0] = "hammer"; 
     inventory[1] = "shovel"; 
     inventory[2] = "Product#"; 
     inventory[3] = "Product Name"; 
     inventory[4] = "Units In Stock"; 
     inventory[5] = "Price"; 
     inventory[6] = "Total Value Of Product"; 
     inventory[7] = "Total Value of Entire Inventory"; 

     float[] numbers = new float[7]; 

     numbers[0] = 1585;// hammers in stock 
     numbers[1] = 900;// shovels in stock 
     numbers[2] = (float) 7.48;// hammer price 
     numbers[3] = (float) 9.98;// shovel price 
     numbers[4] = (numbers[0] * numbers[2]);// total value of hammers in inventory 
     numbers[5] = (numbers[1] * numbers[3]);// total value of shovels in inventory 
     numbers[6] = (numbers[4] + numbers[5]);// total value of products in inventory 

     int[] productNum = new int[2]; 
     productNum[0] = 5211;// hammer part # 
     productNum[1] = 5212;// shovel part # 

     System.out.print(numbers[6]); 
    } 
    public static float total(){ 
     return 0; 
    }   
} 
+1

有什么问题? – Pratik 2015-04-06 04:37:23

+0

我想用数字[6]作为值0开始。然后使用方法添加数字[4]和数字[5]。然后返回新值并将其存储到数字[6]。 – 2015-04-06 05:01:29

+0

这段代码定义了服务器的使用方式,但不应该这样使用。数组始终是数据组,在你的数组中,2个用于数量的东西,2个东西的价格,2个东西的总数等,所以这是不好的。在这里你可以总结你的total()方法传递你的数值[4]和数字[5],并得到两个数字的总和。 – Pratik 2015-04-06 05:18:07

回答

-1

如果你只是想你的总的方法来添加两个元素,然后将其更改为:

public static float total(float x, float y) 
{ 
    return x + y; 
} 

,你会调用这个如下

numbers[6] = total(numbers[4], numbers[5]);