• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Ending a do while statement in this situation?

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I end do while statement in this situation: I have a switch statement and in one of my cases I do a calculation. The condition t says enter a number, if it is positive you go on. If it is negative it continues back to the top to enter a positive variable. Then I have my while statement says that condition <= 0 then it go on and to the next do while statement. So then I enter the second number for the calculation it suppose to do the same thing if it is negative tells me to enter positive variable it does not do that. Then on the last while I have the same condition <= 0. Then right under the while statement I have formula.
I have tired all kinds of things to stop the last while statement to make sure the number is positive before it reads the formula. I entered a break then is says statement not reachable. Or is there some way to make sure it looks at the last while before moving on to the formula.
Sorry this is so long but if any one can help a little that would be greatly appreciated also if any go web sites to look java help on.
 
jon ladd
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would it help if I put the program in here?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but keep it to the essentials.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible that you are using the wrong variable in the last while loop to check the terminating condition? It could be possible because you are using many while loops.
 
jon ladd
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a piece of the code, the last do while statement should also check to see if number is negtive. Instead it takes whatever number postive or negtive for width and just takes the number and does equation.
case RECTANGLE: {
double area;
double length;
double width;
Utility.separator(80, '*');
do {
System.out.print(" Enter length: ");
length = Keyboard.readDouble();
Utility.skip();
if (length <= 0 ) {
System.out.println("Invalid
length");

continue;
}
}
while (length <= 0 );
do {

System.out.print(" Enter width: ");
width = Keyboard.readDouble();
Utility.skip();
if (width <= 0) {
System.out.println("Invalid
width");
continue;
}
}
while ( width <= 0);

area = length * width;
System.out.println(" Rectanlge area
= " + area);
Utility.skip();
Utility.separator(80, '*');
break;
}
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I modified your code so that I could actually run it and took away the redundant continue statements:

The output is what I expected:


Invalidlength -4.0
Invalidlength -3.0
Invalidlength -2.0
Invalidlength -1.0
Invalidlength 0.0
Invalidwidth -4.0
Invalidwidth -3.0
Invalidwidth -2.0
Invalidwidth -1.0
Invalidwidth 0.0
length = 1.0 width = 1.0
Rectangle area = 1.0


No problem
[ February 21, 2003: Message edited by: Barry Gaunt ]
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic