如何从循环if中调用值?
大家好!!我是 Java 新手。我希望你能帮助我解决这个问题,我认为这是一个愚蠢的问题,但我堆栈并且无法思考如何解决这个问题。
如果在 void main 处,如何从循环中调用 double d0 ?
public class Dist
{
public static void main(String[] args)
{
Point []pt=new Point [4];
for(int i = 0 ;i < pt.length ; i ++){
pt[i] = new Point();
}
pt[0].x=40; pt[0].y=40;
pt[1].x=40; pt[1].y=39;
pt[2].x=39; pt[2].y=40;
// pt[3].x=26; pt[3].y=36;
int N=4;
for (int n=0;n<N-2;n++){
int ux=pt[n].x-pt[n+1].x;
int uy=pt[n].y-pt[n+1].y;
int vx=pt[n+1].x-pt[n+2].x;
int vy=pt[n+1].y-pt[n+2].y;
int wx=pt[n].x-pt[n+2].x;
int wy=pt[n].y-pt[n+2].y;
int zx=ux-vx;
int zy=uy-vy;
int a=(ux*ux)+(uy*uy);
int b=(ux*vx)+(uy*vy);
int c=(vx*vx)+(vy*vy);
int d=(ux*wx)+(uy*wy);
int e=(vx*wx)+(vy*wy);
double dmin;
if(a==0||c==0||e==0) {
System.out.println("a"+a);
break;
}
int f = (a * c) - (b * b);
if(f <= 0){
int tc = d / b;
int d0x=wx+(tc*vx);
int d0y=wy+(tc*vy);
double d0=Math.sqrt((d0x*d0x)+(d0y*d0y));
System.out.println("dmin1:"+d0);
}
int s=(b*e)-(c*d);
int t=(a*e)-(b*d);
int d1x=wx+(s*ux)-(t*vx);
int d1y=wy+(s*uy)-(t*vy);
double d1=Math.sqrt((d1x*d1x)+(d1y*d1y));
System.out.println("d1:"+d1);
double h=Math.pow((ux-vx),2);
double j=Math.pow((uy-vy),2);
double k=h+j;
int tx=-wx*(ux-vx);
int ty=-wy*(uy-vy);
double ts=(tx+ty)/k;
double d2x=wx+(ts*(ux-vx));
double d2y=wy+(ts*(uy-vy));
double d2=Math.sqrt((d2x*d2x)+(d2y*d2y));
System.out.println("d2:"+d2);
if(d1<d2)
dmin=d1;
else
dmin=d2;
System.out.println("dmin:"+dmin);
}
}
}
Hye everyone!! I'm new to Java . I hope you can help me to solve this problem and I think this was a silly question but I stack and cannot think how to solve this..
How to call double d0 from loop if at void main?
public class Dist
{
public static void main(String[] args)
{
Point []pt=new Point [4];
for(int i = 0 ;i < pt.length ; i ++){
pt[i] = new Point();
}
pt[0].x=40; pt[0].y=40;
pt[1].x=40; pt[1].y=39;
pt[2].x=39; pt[2].y=40;
// pt[3].x=26; pt[3].y=36;
int N=4;
for (int n=0;n<N-2;n++){
int ux=pt[n].x-pt[n+1].x;
int uy=pt[n].y-pt[n+1].y;
int vx=pt[n+1].x-pt[n+2].x;
int vy=pt[n+1].y-pt[n+2].y;
int wx=pt[n].x-pt[n+2].x;
int wy=pt[n].y-pt[n+2].y;
int zx=ux-vx;
int zy=uy-vy;
int a=(ux*ux)+(uy*uy);
int b=(ux*vx)+(uy*vy);
int c=(vx*vx)+(vy*vy);
int d=(ux*wx)+(uy*wy);
int e=(vx*wx)+(vy*wy);
double dmin;
if(a==0||c==0||e==0) {
System.out.println("a"+a);
break;
}
int f = (a * c) - (b * b);
if(f <= 0){
int tc = d / b;
int d0x=wx+(tc*vx);
int d0y=wy+(tc*vy);
double d0=Math.sqrt((d0x*d0x)+(d0y*d0y));
System.out.println("dmin1:"+d0);
}
int s=(b*e)-(c*d);
int t=(a*e)-(b*d);
int d1x=wx+(s*ux)-(t*vx);
int d1y=wy+(s*uy)-(t*vy);
double d1=Math.sqrt((d1x*d1x)+(d1y*d1y));
System.out.println("d1:"+d1);
double h=Math.pow((ux-vx),2);
double j=Math.pow((uy-vy),2);
double k=h+j;
int tx=-wx*(ux-vx);
int ty=-wy*(uy-vy);
double ts=(tx+ty)/k;
double d2x=wx+(ts*(ux-vx));
double d2y=wy+(ts*(uy-vy));
double d2=Math.sqrt((d2x*d2x)+(d2y*d2y));
System.out.println("d2:"+d2);
if(d1<d2)
dmin=d1;
else
dmin=d2;
System.out.println("dmin:"+dmin);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题有点不清楚,但问题可能是您在
if
块内声明d0
。它将在该块之外不可见。尝试在其他地方声明它。Your question is a bit unclear, but maybe the problem is that you are declaring
d0
inside of anif
block. It will not be visible outside of that block. Try declaring it somewhere else.