美好的一天!我有这个代码,找到一个点的轨迹方程。我可以输出我的输出,看它是否可以被某个数字整除,如果它可以除以它?
这是我的代码:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//DecimalFormat fmt = new DecimalFormat("+##");
int x1 , y1, x2, y2,
inverted_x1, inverted_y1, inverted_x2, inverted_y2,
x1Times2, x2Times2, y1Times2, y2Times2,
x1raised, y1raised, x2raised, y2raised,
computeX, computeY, computeS,
x2inverted, y2inverted, invertedx2times2, invertedy2times2;
try{
x1 = Integer.parseInt(jTextField1.getText());
y1 = Integer.parseInt(jTextField2.getText());
x2 = Integer.parseInt(jTextField3.getText());
y2 = Integer.parseInt(jTextField4.getText());
int[] array = new int[4];
array[0] = x1;
array[1] = x2;
array[2] = y1;
array[3] = y2;
inverted_x1 = x1 *= -1;
inverted_y1 = y1 *= -1;
inverted_x2 = x2 *= -1;
inverted_y2 = y2 *= -1;
jTextField5.setText("= (x" + formatSign(inverted_x1) + ")² + (y" + formatSign(inverted_y1) + ")²"
+ " = (x" + formatSign(inverted_x2) + ")² + (y" + formatSign(inverted_y2) + ")²" );
x1Times2 = inverted_x1*2;
y1Times2 = inverted_y1*2;
x2Times2 = inverted_x2*2;
y2Times2 = inverted_y2*2;
x1raised = inverted_x1*inverted_x1;
y1raised = inverted_y1*inverted_y1;
x2raised = inverted_x2*inverted_x2;
y2raised = inverted_y2*inverted_y2;
jTextField9.setText("= x²" + formatSign(x1Times2) + "x" + formatSign(x1raised) + "+y²" + formatSign(y1Times2)
+ "y" + formatSign(y1raised) + "= x²" + formatSign(x2Times2) + "x" + formatSign(x2raised) + "+y²"
+ formatSign(y2Times2) + "y" + formatSign(y2raised));
x2inverted = x2raised *= -1;
y2inverted = y2raised *= -1;
invertedx2times2 = x2Times2 *= -1;
invertedy2times2 = y2Times2 *= -1;
computeX = x1Times2 + invertedx2times2;
computeY = y1Times2 + invertedy2times2;
computeS = x2inverted + y2inverted + x1raised + y1raised;
jTextField17.setText("= " + formatSign(computeX) + "x" + formatSign(computeY) + "y" + formatSign(computeS) + "=0");
XYLineChart_AWT chart = new XYLineChart_AWT("Locus of Point Graph", "", array);
chart.pack( );
RefineryUtilities.centerFrameOnScreen( chart );
chart.setVisible( true );
}catch(Exception e){
JOptionPane.showMessageDialog(null, "Please fill necessary inputs");
}
这是我的示例输出:
红色是我的输出,并且所有变量都可以被6整除。我是否可以检查它是否可以被某个数字整除,如果它可以除以?
提前谢谢!
答案 0 :(得分:0)
是的,您正在寻找的是%运算符,它将为您提供余数。
创建一个方法,该方法获取所有3个数字并检查ID为GCD(最大公约数)。
private int gcd(int xcoef,int ycoef, int constant) {
int gcd = 1;
for(int i=2; i<=xcoef && i<=ycoef && i<=constant;i++) {
if(xcoef%i==0 && ycoef%i==0 && constant%i==0) {
gcd = i;
}
}
return gcd;
}
在您的代码中
int gcd = gcd(computeX, computeY, computeS);
jTextField17.setText("= " + formatSign(computeX/lcm) + "x" + formatSign(computeY/lcm) + "y" + formatSign(computeS/lcm) + "=0");