int [] num={1,2,3,4,5};
LabExercise2 pass = new LabExercise2 ();
for(i=0; i<num.length; i++){
System.out.print("Please Enter Your Hours: ");
BufferedReader hr = new BufferedReader(new InputStreamReader(System.in));
hour = hr.readLine();
hours = Integer.parseInt(hour);
if(hours >= 30)
{
wage = 26*hours;
totalonemonth = wage*4;
System.out.println("Total Wages for 1 week: RM"+wage);
System.out.println("Total Wages for 1 month: RM"+totalonemonth+"\n");
你/任何人可以帮助我吗?我在这里有一个代码,但问题是我需要将所有结果全部加在控制结构(totalonemonth)中,但是它没有用。有什么不对?
答案 0 :(得分:1)
您应该将总变量初始化为0并每次添加到该变量。
int totalonemonth = 0;
for(i=0; i<num.length; i++){
...
if(hours >= 30)
{
wage = 26*hours;
totalonemonth += wage*4;
...
另一件事,你不需要一个数组进行循环迭代。你可以这样做:
for (int i = 0; i < 5; i++) {
...
}