import java.util.Scanner; public class Division2 { public static void main (Stri
ID: 3627815 • Letter: I
Question
import java.util.Scanner;public class Division2
{
public static void main (String[] args)
{
Scanner stdIn = new Scanner(System.in);
double n;
int d;
System.out.print("Enter numerator: ");
n = stdIn.nextDouble();
System.out.print("Enter divisor: ");
d = stdIn.nextInt();
System.out.println(n / d);
}// end main
} end Division2 class
A) Rewrite the program so that it still employs a double numerator and int
demonitor, but if the value for the denominator is zero, it refuses to perform the
division operation, and keeps asking for the denominator until the user supplies
something other than zero.
B) Rewrite the part of the program from question A so that if the user inputs an
improper format for either numerator or denominator, the entire input query
repeats until both formats are OK. Hint: Put "try" and "catch" blocks in a loop that
executes "while" (OK == false), and set OK = true after all the critical operations in
the "try" block have succeeded. Note: If the scanned format is bad, you'll get
infinite looping unless you re-instantiate "stdIn in each iteration, or use a two-step
operation for each input(input a string and then parse it)
Explanation / Answer
import java.util.Scanner; public class Division2 { public static void main (String[] args) { Scanner stdIn = new Scanner(System.in); double n; int d; System.out.print("Enter numerator: "); n = stdIn.nextDouble(); System.out.print("Enter divisor: "); d = stdIn.nextInt(); if(d==0) System.out.println(" Cant Perform Division."); else System.out.println(n / d); } // end main }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.