2017-05-25 115 views
1

我是新的Java程序員。我正在編寫簡單的程序來計算矩形的面積。您可以輸入矩形的寬度和高度,但問題是我輸入的任何值,面積值總是返回零。我該如何解決這個問題。請看看我的CD。Java返回值與預期不符

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

謝謝...祝您有美好的一天! :)

回答

2

你的問題是,你不分配輸入到你的類變量:

private int width; 
private int length; 

但你方法的局部變量String widthString length

所以,因爲這兩個this.widththis.length沒有改變所以他們0因爲在Java int是默認初始化爲0return this.width * this.length;將返回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,否則你應該分析這些字符串回int

+2

講出了我的嘴:) –

+0

@arcee的 - 你將需要輸入從字符串轉換爲長。 Long.parseLong(inputString)應該這樣做。 –

+1

這個網站有很多忍者 –

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

這裏您將寬度放在兩個局部變量中,並返回屬性寬度和高度的多個不同變量。試着用

this.width = scanner.nextInt(); 
System.out.print("Enter height: "); 
this.height = scanner.nextInt(); 
return this.width * this.length; 
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

這不起作用,因爲scanner.nextLine();返回一個字符串,寬度爲int。使用scanner.nextInt() –

+0

已更改,感謝您的評論。 – PSo

0

做這樣的事情在類shape 。

您需要使用關鍵字

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