Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- do{ //Begin of loop
- try{ //Try this code
- System.out.println("Enter a number");
- double i = read.nextDouble(); //Reads user input
- double rn = r.nextInt(10); //Generates random number rn
- System.out.println(i + " divided by random number " + rn + " is " + (i/rn));
- }catch(InputMismatchException type_error){ //Catches error if there is a type mismatch
- //Example error: if user enters a letter instead of a double
- System.out.println("Error. You cannot divide a letter by a number!");
- break; //break stops the execution of the program
- }
- //using a continue statement here does not work
- }while(true); //Loop forever
- Scanner read = new Scanner(System.in);
- Random r = new Random();
- do { //Begin of loop
- try { //Try this code
- System.out.println("Enter a number");
- double i = read.nextDouble(); //Reads user input
- double rn = r.nextInt(10); //Generates random number rn
- System.out.println(i + " divided by random number " + rn + " is " + (i / rn));
- } catch (InputMismatchException type_error) { //Catches error if there is a type mismatch
- //Example error: if user enters a letter instead of a double
- System.out.println("Error. You cannot divide a letter by a number!");
- // Empty the scanner before the next iteration:
- read.next();
- }
- //using a continue statement here does not work
- } while (true); //Loop forever
- Scanner read = new Scanner(System.in);
- do {
- try {
- // trimmed out non-necessary stuff
- System.out.println("Enter a number");
- double i = Double.parseDouble(read.nextLine());
- System.out.println(i);
- // changed exception caught
- }
- catch (NumberFormatException type_error) {
- System.out.println("Error. You cannot divide a letter by a number!");
- continue;
- }
- } while (true);
- do{ //Begin loop
- try{ //Try this code
- System.out.println("Enter a number");
- double i = read.nextDouble(); //Reads user input
- double rn = r.nextInt(10); //Generates random number rn
- System.out.println(i + " divided by random number " + rn + " is " + (i/rn));
- }catch(InputMismatchException type_error){ //Catches error if there is a type mismatch
- //Example error: if user enters a letter instead of a double
- System.out.println("Error. You cannot divide " + read.next() + " by a number!");
- }
- } while(true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement