Devidebyzerowithoutnoexception--from "Java How to Program" (Early Objects), 10th

Source: Internet
Author: User

1 //a beautiful example2 ImportJava.util.Scanner;3 Importjava.util.InputMismatchException;4 5  Public classdevidebyzerowithoutnoexception1{6    Public Static intQuotient (intNumerator,intdenominator)7     throwsArithmeticException8   {9     returnnumerator/denominator;Ten   } One  A    Public Static voidMain (string[] args) { -Scanner scanner=NewScanner (system.in); -  the     Booleancontinueloop=true; -  -      Do { -      Try +      { -System.out.print ("Please enter an int numerator:"); +        intNumerator=scanner.nextint (); A  atSystem.out.print ("Please enter a in denominator:"); -        intDenominator=scanner.nextint (); -  -        intresult=quotient (numerator,denominator); -  -System.out.printf ("%nresult:%d/%d=%d%n", Numerator,denominator,result); in  -continueloop=false; to  +      } -  the     Catch(inputmismatchexception inputmismatchexception) *     { $System.err.printf ("%nexception:%s%n", inputmismatchexception);Panax Notoginseng scanner.nextline (); -System.out.printf ("%nyou must enter an integer number. Please try again.%n%n "); the     } +  A     Catch(arithmeticexception arithmeticexception) the     { +System.err.printf ("%nexception:%n%s", arithmeticexception); -System.out.printf ("%nzero is an invalid denom inator! Please try again.%n%n "); $     } $  -  -} while(continueloop); the  - Wuyi   } the}

Devidebyzerowithoutnoexception--from "Java How to Program" (Early Objects), 10th

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.