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;
}
}
有什麼問題? – Pratik 2015-04-06 04:37:23
我想用數字[6]作爲值0開始。然後使用方法添加數字[4]和數字[5]。然後返回新值並將其存儲到數字[6]。 – 2015-04-06 05:01:29
這段代碼定義了服務器的使用方式,但不應該這樣使用。數組始終是數據組,在你的數組中,2個用於數量的東西,2個東西的價格,2個東西的總數等,所以這是不好的。在這裏你可以總結你的total()方法傳遞你的數值[4]和數字[5],並得到兩個數字的總和。 – Pratik 2015-04-06 05:18:07