Java返回值不如预期

时间:2017-05-25 08:53:10

标签: java

我是新的Java程序员。我正在编写简单的程序来计算矩形的面积。你可以输入矩形的宽度和高度,但问题是我输入的值,区域值总是返回零。我该如何解决这个问题。请看看我的cde。

import java.util.Scanner;
public class Shape {
    private int area;
    private int width;
    private int length;
    private String name;

    public String shapeName() {
     Scanner scanner = new Scanner(System.in);
     System.out.print("Enter shape name: ");
     String name = scanner.nextLine();
     return name;
    }

    public int area() {
     Scanner scanner = new Scanner(System.in);
     System.out.print("Enter width: ");
     String width = scanner.nextLine();  
     System.out.print("Enter height: ");
     String height = scanner.nextLine();
     return this.width * this.length;       
    }
}
public class Example1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Shape shape = new Shape();
        System.out.println("Shape is " + shape.shapeName());
        System.out.println("It's area is " + shape.area());
    }
}

谢谢......祝你有个美好的一天! :)

6 个答案:

答案 0 :(得分:2)

您的问题是您没有将输入分配给类变量:

private int width;
private int length;

但是你的方法是局部变量String widthString length

因此,return this.width * this.length;行将返回0,因为this.widththis.length都未被更改,因此它们是0,因为在Java {{ 1}}默认初始化为int

您应该将输入分配给类变量。

0

注意:

使用 public int area() { Scanner scanner = new Scanner(System.in); System.out.print("Enter width: "); width = scanner.nextInt(); System.out.print("Enter height: "); length = scanner.nextInt(); return width * length; } 获取Scanner.nextInt()值而不是int,这将返回Scanner.nextLine(),否则您应该将这些字符串解析回String

答案 1 :(得分:0)

     String width = scanner.nextLine();  
     System.out.print("Enter height: ");
     String height = scanner.nextLine();
     return this.width * this.length;       
    }

在这里,您将宽度放在两个局部变量中,并返回属性width和height的mul,它们是不同的变量。试试

this.width = scanner.nextInt();
System.out.print("Enter height: ");
this.height = scanner.nextInt();
return this.width * this.length;  

答案 2 :(得分:0)

将值分配给本地新变量意味着

this.width和this.length未使用,因此返回0

public int area() {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter width: ");
    this.width = scanner.nextInt();  
    System.out.print("Enter height: ");
    this.height = scanner.nextInt();
    return this.width * this.length;       
}

答案 3 :(得分:0)

在Shape类中执行类似的操作。

您需要使用关键字初始化全局变量 width length

class Shape {
private int area;
private int width;
private int length;
private String name;

public String shapeName() {
 Scanner scanner = new Scanner(System.in);
 System.out.print("Enter shape name: ");
 String name = scanner.nextLine();
 return name;
}

public int area() {
 Scanner scanner = new Scanner(System.in);
 System.out.print("Enter width: ");
 int width = scanner.nextInt();  
 System.out.print("Enter lengtht: ");
 int length = scanner.nextInt();
 this.width = width;
 this.length=length;
 return this.width * this.length;       
}
}

答案 4 :(得分:0)

这应该解决你的问题兄弟!

public int area() {
     Scanner scanner = new Scanner(System.in);
     System.out.print("Enter width: ");
     int width = Integer.parseInt(scanner.nextLine());
     this.width = width;  
     System.out.print("Enter height: ");
     int height = Integer.parseInt(scanner.nextLine());
     this.height = height;
     return this.width * this.length;       
    }

答案 5 :(得分:0)

也使用此代码

public double area(){
     Scanner scanner = new Scanner(System.in);
     System.out.print("Enter width: ");
     int width = scanner.nextInt();  
     System.out.print("Enter height: ");
     int height = scanner.nextInt();
      double d=width*height;
     return d;       
    }