[英]Java - Variable not initialized for do-while loop
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'.
如果用户输入“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.
没有do的代码很好,而它只是没有循环,但是有了它,我得到了未初始化的错误。
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 ==>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.