0

我写了一个Box有两个构造函数的类。一个是Box()默认构造函数,当用户输入空格时执行,另一个是Box(length, breadth, height)当用户实际上输入给定 Box 的输入时执行。所以我Box用以下方式编写了这个类:

class Box{

    private int length, breadth, height;

    //Default Constructor
    Box(){
        System.out.print("No Parameter given"); 
    }

    //Parameterized Constructor
    Box(int l, int b, int h){
        length=l; breadth=b; height=h;
    }

    int volume(){
        return breadth*height*length;
    }
}

所以,这是main()我试图实现代码的功能。我的意图是在输入为空格时调用默认构造函数,如果输入不为空,则由 sceond 构造函数计算体积。

class mybox{
    public static void main(String args[]) throws IOException{
            System.out .print("Enter length, breadth and height->>");
            Scanner scanner=new Scanner(System.in);
            int length1=scanner.nextInt();
            System.out.println("Length= "+length1);
            int breadth1=scanner.nextInt();
            System.out.println("Breadth= "+breadth1);
            int height1=scanner.nextInt();
            System.out.println("Height= "+height1);

            if( length1== Integer.parseInt(" ") 
                && breadth1== Integer.parseInt(" ") 
                && height1== Integer.parseInt(" ") ){
                Box samplebox=new Box();
            }
            else {
                Box samplebox=new Box(length1, breadth1, height1);
                try{
                    System.out.println("The volume of the box is " + samplebox.volume());
                } catch (ArithmeticException e){
                    e.printStackTrace();
                }
            }
     }
}

在 Eclipse 中,我在行中收到警告“未使用局部变量 samplebox 的值” Box samplebox=new Box()。那么代码中的错误在哪里呢?

4

4 回答 4

1
scanner.nextInt()

不能接受空字符串输入。它将等待有效的数字/字符输入。

Integer.parseInt(" ")将抛出异常,因为空格不是有效的整数字符串。

于 2013-11-06T04:02:03.727 回答
1

在这段代码中:

if(length1== Integer.parseInt(" ") && breadth1== Integer.parseInt(" ") && height1== Integer.parseInt(" "))
{
    Box samplebox=new Box();
}
else
{
    Box samplebox=new Box(length1, breadth1, height1);
    try{
        System.out.println("The volume of the box is "+ samplebox.volume());
    }
    catch (ArithmeticException e)
    {
        e.printStackTrace();
    }
}

您正在声明两个名为 的单独变量samplebox,在 . 的每个分支中都有一个if。第一个在超出范围之前没有被使用。为变量赋值不是“使用”该值;Eclipse 注意到分配的值不能在任何地方使用,并警告您代码可能存在问题。

一种解决方案是在分配值后简单地在分支samplebox内部做一些事情。if或者,您可以这样做:

Box samplebox;
if(length1== Integer.parseInt(" ") && breadth1== Integer.parseInt(" ") && height1== Integer.parseInt(" "))
{
    samplebox=new Box();
}
else
{
    samplebox=new Box(length1, breadth1, height1);
    try{
        System.out.println("The volume of the box is "+ samplebox.volume());
    }
    catch (ArithmeticException e)
    {
        e.printStackTrace();
    }
}
// . . . use samplebox

通过将声明本身移出if分支,您将分配给一个范围大于if语句本身的变量。

于 2013-11-06T04:02:04.213 回答
0
if(length1== Integer.parseInt(" ") && breadth1== Integer.parseInt(" ") && height1== Integer.parseInt(" "))
{
    // Here you assign a variable samplebox that is local (within the {})
    // but you don't use it for anything.
    // i.e. The value of the local variable samplebox is not used
    //
    Box samplebox=new Box();
}
于 2013-11-06T04:02:19.573 回答
0

如果您还想允许来自用户的任何字符串,则必须使用scanner.nextLine()该字符串并将其转换为整数。像这样的东西:

    System.out.print("Enter length, breadth and height->>");
    Scanner scanner = new Scanner(System.in);
    String len_str = scanner.nextLine();
    int length1 = 0;
    try {
        length1 = Integer.parseInt(len_str);
    } catch (Exception ef) {
        length1 = 0;
    }

    System.out.println("Length= " + length1);
    String br_str = scanner.nextLine();
    int breadth1 = 0;
    try {
        breadth1 = scanner.nextInt();
    } catch (Exception ef) {
        breadth1 = 0;
    }
    System.out.println("Breadth= " + breadth1);
    int height1 = 0;
    String he_str = scanner.nextLine();
    try {
        height1 = Integer.parseInt(he_str);
    } catch (Exception ef) {
        height1 = 0;
    }
    height1 = scanner.nextInt();
    System.out.println("Height= " + height1);
    if (length1 == 0 && breadth1 == 0 && height1 == 0) {
        Box samplebox = new Box();
    } else {
        Box samplebox = new Box(length1, breadth1, height1);
        try {
            System.out.println("The volume of the box is " + samplebox.volume());
        } catch (ArithmeticException e) {
            e.printStackTrace();
        }
    }
于 2013-11-06T04:18:03.720 回答