尝试使用 get/set 方法
Trying to use get/set methods
在圈子里看看class。每次 if 语句评估为真时,我希望计数器递增 1。我正在尝试获取一个 set 方法来为我完成这项工作,但是当我在 main 方法中检查计数器的值时,它没有计数。有小费吗?
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.");
}
}
这一行是你的问题:
public void setCount(int n) {count = n++;}
当你说count = n++;
时,相当于
count = n;
n = n + 1;
因为 n++
是 post 递增运算符。
这可能不是您想要的行为。相反,如果你把
public void setCount(int n) {count+= n;}
每调用一次该方法就会count
增加n
。 +=
运算符只是 shorthand 对于 count = count + n
此外,将方法命名为 setCount
有点误导,如果它实际上增加了给定的计数。这意味着它将设置 count = n
而不是 count = count + n
。我建议将其重命名为 incrementCount
.
就我个人而言,对于这么简单的事情,我什至不会为 getter 和 setter 而烦恼。只需在 for 循环之前定义一个 int count
并在每次 if 语句评估为 true
时使用 count++
递增它
将您的 setCount 修改为:
public void setCount(){
count++;
}
尝试将您的 setCount
方法更改为:
public void setCount() {count++;}
每次在 paint
方法中调用时,您的代码都会将 count
设置为 2。
在圈子里看看class。每次 if 语句评估为真时,我希望计数器递增 1。我正在尝试获取一个 set 方法来为我完成这项工作,但是当我在 main 方法中检查计数器的值时,它没有计数。有小费吗?
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.");
}
}
这一行是你的问题:
public void setCount(int n) {count = n++;}
当你说count = n++;
时,相当于
count = n;
n = n + 1;
因为 n++
是 post 递增运算符。
这可能不是您想要的行为。相反,如果你把
public void setCount(int n) {count+= n;}
每调用一次该方法就会count
增加n
。 +=
运算符只是 shorthand 对于 count = count + n
此外,将方法命名为 setCount
有点误导,如果它实际上增加了给定的计数。这意味着它将设置 count = n
而不是 count = count + n
。我建议将其重命名为 incrementCount
.
就我个人而言,对于这么简单的事情,我什至不会为 getter 和 setter 而烦恼。只需在 for 循环之前定义一个 int count
并在每次 if 语句评估为 true
count++
递增它
将您的 setCount 修改为:
public void setCount(){
count++;
}
尝试将您的 setCount
方法更改为:
public void setCount() {count++;}
每次在 paint
方法中调用时,您的代码都会将 count
设置为 2。