2016-11-01 5 views
-1

@Rogueここでは、私が設定した新しい方法があります。現在、 "シンボルを見つけることができません"というエラーが表示されています。私はダブルチェックと私のオブジェクト(rectangle4)が宣言されました。 アイデアは何故ですか?おかげで再び仲間...メソッドのパラメータに関する問題... Java

package rectanglewithprivatedatafields; 

class RectangleTestFile 
{ 
public static void main(String[] args) 
{ 
    double lengths = rectangle4.getLengths(); 
    double widths = rectangle4.getAreas(); 

//rectangle object 4 
RectangleWithPrivateDataFields rectangle4 = new RectangleWithPrivateDataFields(); 
System.out.println("\nUser Specified Rectangle 4"); 
System.out.println("Please enter the appropriates sizes for your rectangle: \n"); 
System.out.println(rectangle4.getWidths()); 
System.err.println(rectangle4.getLengths()); 
System.out.println(rectangle4.getPerimeters(rectangle4.getLengths(), rectangle4.getWidths()));  
System.out.println(rectangle4.getAreas(rectangle4.getLengths(),rectangle4.getWidths())); 
}//main 
} 
+1

完全なエラーメッセージを含めます。あなたが話していることを理解できるかどうかを確認してください。 – John3136

+0

あなたはあなたの 'main'内のどこにでも' lengths'または 'widths'を宣言していません。これらの変数はそのスコープ内に存在しません。 'double lengths = rectangle4.getLengths();'をメインの上部に向かって配置します。 'widths'に似ています – trooper

+0

@trooperあなたの助けを歓迎します – duckHunter18

答えて

0

代わりのrectangle4.getPerimeters(lengths, widths)あなたがrectangle4.getPerimeters(rectangle4.getLengths(), rectangle4.getWidths())を意味するのですか?

ユーティリティクラスにはプライベートメンバー変数はなく、パブリックアクセサメソッドのみがあります。

0

lengthswidthsあなたのmainメソッド内の変数ではありませんので、それらを作る:

int lengths = rectangle4.getLengths(); 
int widths = rectangle4.getWidths(); 

これらのメソッドは、入力を読んでいるので、あなたは一度だけそれらを呼び出す必要があります。さらに私が覚えていることから、はSystem.inストリームを消費するので、一度しか作成しません。

//Simple, short class names are easier to work with 
public class Rectangle { 

    //length and width is data, or "state" of our rectangle, so store it 
    private final int length; 
    private final int width; 

    //Allow creating it without requiring input 
    public Rectangle(int width, int length) { 
     this.width = width; 
     this.length = length; 
    } 

    //We create ourselves given a Scanner which we can take input from 
    public Rectangle(Scanner input) { 
     System.out.print("Length: "); 
     this.length = input.nextDouble(); 
     System.out.print("Width: "); 
     this.width = input.nextDouble(); 
    } 

    //Calculate based around known information 
    public double getArea() { 
     return this.length * this.width; 
    } 

    //Calculate based around known information 
    public double getPerimeter() { 
     return 2 * (this.length + this.width); 
    } 

    //getter 
    public int getLength() { 
     return this.length; 
    } 

    //getter 
    public int getWidth() { 
     return this.width; 
    } 

} 

は、このようにそのようにそれの呼び出しを行う:(オブジェクト指向プログラミングのために)今後、私はコンストラクタがScannerが自分自身を構築するために受け入れ、その後、私は必要な値を返すメソッドを持っているになるだろう:

public static final void main(String... args) { 
    Scanner input = new Scanner(System.in); //make once 
    Rectangle rect = new Rectangle(input); //let our rectangle do all the work 
    //Simple! 
    System.out.println("Your rectangle has a width of " + rect.getWidth() 
      + ", a length of " + rect.getLength() 
      + ", the perimeter " + rect.getPerimeter() 
      + ", and an area of " + rect.getArea()); 
} 

ここで重要なアイデアれているデータと役割の分離 - Rectangle情報と自身の創造のすべての側面を管理します。あなたがlengthとを手動で提供するか、またはその情報を入手する手段を提供するかどうか(Scanner)。 Rectangleは、必要な情報を保存し、必要な情報(私たちの方法)を返します。これにより、クラスの計算と使用が非常に簡単になります(入力を渡すと、外部の観点から作業が行われます)。

+0

ありがとう、@ログ...私が宣言すると私の変数 – duckHunter18

+0

のように "double lengths = rectangle4.getLengths();私はエラーが発生..."シンボルが見つかりません " – duckHunter18

+0

という意味、' rectangle4'変数がないことを意味します。 – Rogue

関連する問題