变量没有被使用但显然是! - Java

时间:2011-05-14 20:22:22

标签: java variables

所以我得到一些奇怪的错误,例如非法启动类型和变量,当我清楚地调用它们时,它们没有被使用。现在我正在使用最新版本的netbeans,我不是Java的专家,但我相信我的netbeans可能已损坏或有些不确定。

就我的逻辑而言,if循环一切都应该没问题,因此我不打算详细介绍该程序的用途。

无论如何这里的代码我将突出显示该区域的错误。

感谢提前问候-Skeng -

import javax.swing.*;
import java.io.*;

public class Envelope extends Parcel {

protected char[] size = {'S','M','L'};

public Envelope(){

ImageIcon imageES = new ImageIcon ("..\\Images\\envelope-small.png");
ImageIcon imageEM = new ImageIcon ("..\\Images\\envelope-medium.png");
    ImageIcon imageEL = new ImageIcon ("..\\Images\\envelope-large.png");


    double ChargeS = 4.50; 
    double ChargeM = 8.50;
    double ChargeL = 16.99;        
    double ChargeFinal = 0;   **//Variable not being used**

    if (size[0] == 'S') {
        ChargeFinal = ChargeS;
    } else if (size[1] == 'M') {
        ChargeFinal = ChargeM;
    } else if (size[2] == 'L')
        ChargeFinal = ChargeL;
    }

    int zone = 0; //Zone will equal whatever the user selects for their parcel size
    double zonecharge; //ZoneCharge will be the price depending on the zone


    if (zone == 1) {             **//Illegal Start of Type** 
        zonecharge = 0;          **//Illegal Start of Type**
     } else if (zone == 2) {     **//Illegal Start of Type**
        zonecharge = 1.5;        **//Illegal Start of Type**
     } else if (zone == 3) {     **//Illegal Start of Type**
        zonecharge = 2;          **//Illegal Start of Type**
     } 

     double EndPrice = ChargeFinal * zonecharge;  **//Cannot find Symbol "ChargeFinal"**
     System.out.println("Charge: £" + EndPrice);  **//Illegal Start of Type**


@Override
public String toString() {
return "ID: " + idNum + "Zone: " + zone + "Charge: " + charge + "Size: " + size;
}

@Override
ImageIcon getImage() {
    return image;
}
}

3 个答案:

答案 0 :(得分:3)

这是因为您在第一个else if

之后缺少一个左括号
if (size[0] == 'S') {
        ChargeFinal = ChargeS;
    } else if (size[1] == 'M') {
        ChargeFinal = ChargeM;
    } else if (size[2] == 'L') { // ADD THIS BRACKET
        ChargeFinal = ChargeL;
    }

答案 1 :(得分:2)

你在这一行的末尾有一个缺失的大括号:

    } else if (size[2] == 'L')

结果,括号两行后来终止了构造函数,并且“非法启动类型”警告即将到来,因为下一个代码块不在任何方法之内。当然,你不能在课堂范围内发表声明!

答案 2 :(得分:1)

} else if (size[2] == 'L')
    ChargeFinal = ChargeL;
}  // <-- move this bracket to the end of the constructor 

有一个额外的括号,即关闭构造函数。