为什么控制没有到达循环?

时间:2013-11-06 03:58:54

标签: java

我写了一个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()

所以,这是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(); } } } } 函数,我试图实现代码。我的意图是当输入是空格时调用默认构造函数,如果输入不为空,则由sceond构造函数计算voulme。

Box samplebox=new Box()

在Eclipse中,我在行{{1}}中收到警告“未使用局部变量samplebox的值”。那么代码中的错误在哪里?

4 个答案:

答案 0 :(得分:1)

scanner.nextInt()

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

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

答案 1 :(得分: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语句本身的变量。

答案 2 :(得分: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();
}

答案 3 :(得分:0)

如果您还想允许用户使用任何字符串,则必须使用scanner.nextLine(),然后将此字符串转换为Integer。像这样:

    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();
        }
    }