尝试使用get / set方法

时间:2015-07-13 03:18:27

标签: java methods get set accessor

看看Circle课程。每次if语句变为true时,我都希望计数器增加1。我试图让一套方法为我做这项工作,但是当我在主方法中检查计数器的值时,它没有计算。有小费吗?

package circle;

import java.awt.Color;
import java.awt.Graphics; 
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JApplet;


public class Shapes extends JApplet {

private int squareLength = 0;
private int count = 0;
private double areaSquares = 0;
private double areaCircle = 0;

public void setPixelDimOfSquare(int width) {squareLength = width;}

public void setCount(int n) {count = n++;}

public int getCount() {return count;}

@Override
public void paint(Graphics g) {

    Graphics2D g2 = (Graphics2D) g;

    //shapes
    //adds sphere (xcoord,ycoord,width of framing rect, height of framing rect)
    g2.draw(new Ellipse2D.Double(0, 0, 1000, 1000));

    //builds square grid and counts number of whole squares in the circle.
    for (int i = 0; squareLength*i <= 1000; i++){
        for (int j = 0; squareLength*j <= 1000; j++) {

         if(new Ellipse2D.Double(0,0,1000,1000).contains(new Rectangle2D.Double(squareLength*i,squareLength*j,squareLength,squareLength))){
             setCount(1);
             g2.setColor(Color.black);
             g2.fillRect(squareLength*i,squareLength*j,squareLength,squareLength);
         } else {
             g2.drawRect(squareLength*i,squareLength*j,squareLength,squareLength);
         }
        }
    } 
    System.out.println("Shape: There are " + getCount() + " squares in the circle.");
} 

public double areaSquares() {
    areaSquares = Math.pow(squareLength,2) * count;
    return areaSquares;
}

public double areaCircle() {

    areaCircle = Math.PI * Math.pow(1000, 2);
    return areaCircle;


}
}

现在为主

package circle;

import java.util.Scanner;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class Main {

public static void main(String[] args) {

    //Gets the desired number of boxes
    //Scanner sc = new Scanner(System.in);
    //System.out.print("Please enter number of boxes:");
    //double boxes = sc.nextDouble();

    // Builds frame for shapes.
    Shapes shape = new Shapes();
    shape.setPixelDimOfSquare(10);

    JFrame frame = new JFrame("Draw Shapes Demo");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    frame.getContentPane().add(shape);
    frame.pack();
    frame.setSize(1000,1000);
    frame.setVisible(true);


    System.out.println("Main: There are " + shape.getCount() + " squares in the circle.");
}
}

3 个答案:

答案 0 :(得分:0)

这一行是你的问题:

public void setCount(int n) {count = n++;}

当您说count = n++;时,它等同于

count = n;
n = n + 1;

因为n++是后增量运算符。 这可能不是您想要的行为。如果你把它放在

public void setCount(int n) {count+= n;}

每次调用该方法时,count都会增加n+=运算符只是count = count + n

的简写

另外,命名方法setCount有点误导,如果它实际上增加了给定数字的计数。这意味着它将设置count = n而不是count = count + n。我建议将其重命名为incrementCount

就个人而言,对于这个简单的事情,我甚至不会为吸气鬼和制定者而烦恼。只需在for循环之前定义int count,并在每次if语句求值为true时使用count++递增

答案 1 :(得分:0)

将您的setCount修改为:

public void setCount(){
  count++;
}

答案 2 :(得分:0)

尝试将setCount方法更改为:

    public void setCount() {count++;}

每次在count方法中调用时,您的代码都会将paint设置为2。