简体   繁体   中英

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; 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. Thank you!!

Do not use "hitTarget" as your while loop condition. 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. If the bullet hits the target or hits the grass, you can set bulletInFlight to false to exit the loop.

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:

  • 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 .

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:
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

PPS: Pasting code with tab characters into stackoverflow is likewise poor form. "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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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