I'm getting an error "Choice has not been initialized" on my code but I have already initialized it just before while. The goal is for the file to loop to the first option if the user entered 'y'. I really need your expert help. The code is fine without the do while its just not looping but with it, I'm getting the uninitialized error.
import java.util.Scanner;
/**
*
* @author Arren
*/
public class Math {
/**
* @param args the command line arguments
*/
@SuppressWarnings("empty-statement")
public static void main(String[] args) {
// TODO code application logic here
int numerator;
int denominator;
char Choice;
String showFractionForm;
int determineLowestTerm;
float determineDecimalEquivalent;
String determineFractionType;
{
do {
Scanner input = new Scanner(System.in);
System.out.print("Enter the numerator ==> ");
numerator = input.nextInt();
System.out.print("Enter the denominator ==> ");
denominator = input.nextInt();
showFractionForm = (numerator + "/" + denominator);
determineDecimalEquivalent = ((float) numerator) / denominator;
System.out.println("***************OUTPUT***************");
System.out.println("NUMERATOR : " + numerator);
System.out.println("DENOMINATOR : " + denominator);
System.out.println("FRACTION : " + showFractionForm);
int smaller = numerator < denominator ? numerator : denominator;
int HCF = -1;
for (int i = smaller; i > 0; --i) {
if (numerator % i == 0 && denominator % i == 0) {
HCF = i;
System.out.println("LOWEST TERM : " + (numerator / HCF) + "/" + (denominator / HCF));
System.out.println("DECIMAL EQUIVALENT : " + determineDecimalEquivalent);
if (numerator < denominator) {
System.out.println("FRACTION TYPE : PROPER FRACTION");
} else if (numerator > denominator) {
System.out.println("FRACTION TYPE : IMPROPER FRACTION");
} else {
System.out.println("FRACTION TYPE : WHOLE NUMBER");
}
System.out.println("");
System.out.println("Input again? [y/n] --> ");
Choice = input.next().charAt(0);
}
while (Choice != 'n');
}
}
}
}
}
I just formatted your program well and I am able to run it without any issue. Given below is the formatted program:
import java.util.Scanner;
/**
*
* @author Arren
*/
public class Math {
/**
* @param args the command line arguments
*/
@SuppressWarnings("empty-statement")
public static void main(String[] args) {
// TODO code application logic here
int numerator;
int denominator;
char Choice;
String showFractionForm;
int determineLowestTerm;
float determineDecimalEquivalent;
String determineFractionType;
do {
Scanner input = new Scanner(System.in);
System.out.print("Enter the numerator ==> ");
numerator = input.nextInt();
System.out.print("Enter the denominator ==> ");
denominator = input.nextInt();
showFractionForm = (numerator + "/" + denominator);
determineDecimalEquivalent = ((float) numerator) / denominator;
System.out.println("***************OUTPUT***************");
System.out.println("NUMERATOR : " + numerator);
System.out.println("DENOMINATOR : " + denominator);
System.out.println("FRACTION : " + showFractionForm);
int smaller = numerator < denominator ? numerator : denominator;
int HCF = -1;
for (int i = smaller; i > 0; --i) {
if (numerator % i == 0 && denominator % i == 0) {
HCF = i;
System.out.println("LOWEST TERM : " + (numerator / HCF) + "/" + (denominator / HCF));
System.out.println("DECIMAL EQUIVALENT : " + determineDecimalEquivalent);
}
if (numerator < denominator) {
System.out.println("FRACTION TYPE : PROPER FRACTION");
} else if (numerator > denominator) {
System.out.println("FRACTION TYPE : IMPROPER FRACTION");
} else {
System.out.println("FRACTION TYPE : WHOLE NUMBER");
}
}
System.out.println("");
System.out.println("Input again? [y/n] --> ");
Choice = input.next().charAt(0);
} while (Choice != 'n');
}
}
Sample run:
Enter the numerator ==> 12
Enter the denominator ==> 4
***************OUTPUT***************
NUMERATOR : 12
DENOMINATOR : 4
FRACTION : 12/4
LOWEST TERM : 3/1
DECIMAL EQUIVALENT : 3.0
FRACTION TYPE : IMPROPER FRACTION
FRACTION TYPE : IMPROPER FRACTION
LOWEST TERM : 6/2
DECIMAL EQUIVALENT : 3.0
FRACTION TYPE : IMPROPER FRACTION
LOWEST TERM : 12/4
DECIMAL EQUIVALENT : 3.0
FRACTION TYPE : IMPROPER FRACTION
Input again? [y/n] -->
y
Enter the numerator ==>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.