简体   繁体   English

如何在没有中断的情况下停止while循环

[英]How to stop while loop witouth break

Can someone help me.有人能帮我吗。

A bullet fires from a cannon and it flies till it will hit the ground.If it hits the target(red) need to out print "Target has been hit",If no "shot off the target".子弹从大炮中发射,一直飞到地面。如果它击中目标(红色)需要输出“目标已被击中”,如果没有“击中目标”。

Battle area战区

But main problem that I just cant stop the loop,if I misset the target,I cant use break;但主要问题是我无法停止循环,如果我错过了目标,我不能使用break; at all,only while,if,else,or else if.根本,只有当,如果,否则,否则如果。

No hit and Hit the target没有命中命中目标

public static void main(String[] args) {
  Scanner sc=new Scanner(System.in);
  double g=8.86,a=40,delta_t=0.05; // a=angle
  double v0,x,y,t,x0,y0;
  boolean hitTarget = false;

  System.out.println("191RDB107 Vladislavs Fedotovs");
  System.out.println("While operators,07,Urans");

  System.out.print("v0=");
  if (sc.hasNextDouble()) {
    v0 = sc.nextDouble();
  } else {
    System.out.println("input-output error");
    sc.close();
    return;
  }

  sc.close();
  System.out.println("result:");
  System.out.println("t \t x \t y");
  t = 0.1;
  while(hitTarget!=true) {

    x =v0*t*Math.cos(Math.toRadians(a));
    y =v0*t*Math.sin(Math.toRadians(a))-(g*Math.pow(t, 2))/2;
    System.out.printf("%3.2f\t%7.3f\t%7.3f\n", t, x, y); // red target
    if ((x >= 12  && x <= 17) && (y <=-2 && y >=-4)) {
      hitTarget=true;
      System.out.print("the target was destroyed");
      t+=0.1;
    } else if (x<=10 && y<=0 || x>=10 && x<=12 && y>=-4 || x>17 && y>=-4) { // green "grass"
      System.out.print("shot off the target");
      t+=0.1;
    } else {
      System.out.print("shot is in the air");
    }

    t+=0.1;
}

Formula which I'm using我正在使用的公式

Should be something like that.应该是这样的。

**Basicaly after the bullet is hitting the ground(y) it should stop the loop. **基本上在子弹击中地面(y)后,它应该停止循环。 Thank you!!谢谢!!

Do not use "hitTarget" as your while loop condition.不要使用“hitTarget”作为您的 while 循环条件。 Change it to a separate variable that simply lets you know if the bullet is in flight.将其更改为一个单独的变量,让您知道子弹是否在飞行中。 When the bullet hits the target, you can then set "hitTarget" to true to use later on.当子弹击中目标时,您可以将“hitTarget”设置为 true 以供以后使用。 If the bullet hits the target or hits the grass, you can set bulletInFlight to false to exit the loop.如果子弹击中目标或击中草,您可以将 bulletInFlight 设置为 false 以退出循环。

boolean bulletInFlight = true;
boolean hitTarget = false;
while (bulletInFlight) {
    x =v0*t*Math.cos(Math.toRadians(a));                    
    y =v0*t*Math.sin(Math.toRadians(a))-(g*Math.pow(t, 2))/2;
    System.out.printf("%3.2f\t%7.3f\t%7.3f\n", t, x, y); // red target
    if ((x >= 12  && x <= 17) && (y <=-2 && y >=-4)) {
        bulletInFlight = false;
        hitTarget = true;
        System.out.print("the target was destroyed");
        t+=0.1;
    } else if (x<=10 && y<=0 || x>=10 && x<=12 && y>=-4 || x>17 && y>=-4) {
        bulletInFlight = false;
        System.out.print("shot off the target");
        t+=0.1;
    } else {
        System.out.print("shot is in the air");
        t+=0.1;
    } 
}
// Now do whatever you need to do once you know the target has been hit or not hit.

You can leave any loop in Java in one of five ways:您可以通过以下五种方式之一在 Java 中留下任何循环:

  • break;
  • throw new <whatever>;
  • return; (possibly with a value depending on your function) (可能具有取决于您的功能的值)
  • change your variables such that the while condition is false and if you're not at the bottom of the loop, continue .更改您的变量,使 while 条件为假,如果您不在循环的底部,请continue

This is almost certainly some homework type thing with artificial constraints on what you can do.这几乎可以肯定是一些家庭作业类型的事情,对你能做什么有人为的限制。

  • The final and just-as-bad-as-the-tweak-and-continue-thing way to get out of a while loop is sometimes termed double testing, and is quite similar to tweak-and-continue:退出 while 循环的最终和同样糟糕的方式有时被称为双重测试,并且与调整和继续非常相似:
boolean foo = true;
while (foo) {
   DoSomeStuff();

   if (SomeTestOrOther()) {
     foo = false;
   }

   if (foo) {
     DoMoreStuff();
   }
}

Note that the latter two are considered poor practice, particularly when you can just 'break'.请注意,后两者被认为是不好的做法,尤其是当您可以“休息”时。 This sort of constraint on your programming is probably intended to teach you that there are many ways to solve any given problem, but it will often teach Poor Programming Habits too.这种对你的编程的约束可能是为了告诉你有很多方法可以解决任何给定的问题,但它通常也会教你不良编程习惯。 Boo hiss.嘘嘘。

PS: Constants in your code like that if (x <= 10... is also poor form PS:像if (x <= 10...这样的代码中的常量也是不好的形式

PPS: Pasting code with tab characters into stackoverflow is likewise poor form. PPS:将带有制表符的代码粘贴到 stackoverflow 中同样是糟糕的形式。 "Pastes poorly on stackoverflow" is just another shovel full of dirt on top of the coffin of the tab character as far as I'm concerned.就我而言,“在 stackoverflow 上粘贴效果不佳”只是制表符棺材顶部的另一把泥土。 Spaces good.空间不错。 Tabs bad.标签不好。 Not that I want you to hit the space bar a lot when indenting your code, but every code editor worthy of the name has a setting that allows you to control what happens when you hit the tab key.并不是说我希望您在缩进代码时经常按空格键,而是每个名副其实的代码编辑器都有一个设置,允许您控制按 Tab 键时发生的情况。 Make sure "insert spaces instead of tabs" (or whatever its called) is turned on.确保“插入空格而不是制表符”(或任何名称)已打开。 Many impudent fools will argue differently, but they are both impudent and foolish.许多无耻的傻瓜会以不同的方式争论,但他们既无耻又愚蠢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM