开发者

Trying to sentinel loop this program [closed]

开发者 https://www.devze.com 2023-01-03 00:47 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.
  switch ( choice )
    {
       case '+':
          System.out.开发者_Python百科printf( "The answer is: %1$.4f.\n", first + second );
          break;
       case '-':
          System.out.printf( "The answer is: %1$.4f.\n", first - second );
          break;
       case '*':
          System.out.printf( "The answer is: %1$.4f.\n", first * second );
          break;
       case '/':
          if( second != 0 )
            System.out.printf( "The answer is: %1$.4f.\n", first / second );
          else
            System.out.println( "Can't divide by zero." );
          break;
       default :
          System.out.println( "You have entered an invalid operation.  Try again." );
          break;
    }


while (true)
{
    System.out.println("What type of Employee? Enter 'o' for Office " +
    "Clerical, 'f' for Factory, or 's' for Saleperson.  Enter 'x' to exit." );  
    String response= inp.nextLine().toLowerCase();

    // validate input, do you switch statement, return; on x     
}


You can use the return; statement to terminate the function

For example:

case 'x':
    return;

You also need to wrap it all in while(true) { ... } to make it loop forever (until return;)

0

精彩评论

暂无评论...
验证码 换一张
取 消