回复之前的question,我对这个问题的初步答案已经解决了,但是当它归结为循环时我遇到了另一个问题,后来通过简单地使用for循环解决了这个问题。
然而,我的问题是我不希望用户在处理异常后不断地重新启动程序,而是希望它将相同的开始问题循环给用户。我已经尝试在返回语句之后放置print语句,并尝试在try catch之后完全复制逻辑代码,但是,意识到这不会导致用户无限循环该异常。此外,在旁注上是的,我之前的问题有很好的答案,然而,没有人设法回答我的反复出现的问题,这就是为什么没有人得到他们答案的复选标记。
import java.io.*;
import java.text.DecimalFormat;
public class Test
{
public static void main(String[] args) throws IOException
{
double x;
x = circlemethods(0.0, 0.0, 0.0, 1.0);
}
public static double circlemethods(double volume, double surfacearea,
double area, double radius) throws IOException
{
BufferedReader myInput = new BufferedReader(new InputStreamReader(System.in));
String numInput;
String reqInput;
String amountStr;
double numInt = 0;
double num = 0;
double answer = 0;
double amount = 0;
double answer2 = 0;
double answer3 = 0;
double answer4 = 0;
for (double i = 0; i < 999; i++)
;
try
{
// for (double i = 0; i < 999; i++);
// while (numInt != 999) {
System.out.println("This program will ask for a given user radius, then proceed to calculate the user input");
System.out.println("The program will use four methods to achieve this, all calling back to the main method");
System.out.println("Press any key to continue");
numInput = myInput.readLine();
System.out.println("First, what would you like to calculate?");
System.out.println("Enter '1' for Circumference, '2' for area, '3' for volume, or '4' for surface area");
reqInput = myInput.readLine();
numInt = Double.parseDouble(reqInput);
System.out.println("Now enter the radius of the required shape(Half of diameter)");
numInput = myInput.readLine();
num = Double.parseDouble(numInput);
DecimalFormat nextAmount = new DecimalFormat("0.00");
amountStr = nextAmount.format(amount);
if (numInt == 1)
{
System.out.println("You chose to calculate circumference, given the radius :" + num);
answer = (3.14) * (2) * (num);
System.out.print("The circumference of that sphere is :");
System.out.println(answer + "cm³");
return answer;
}
else if (numInt == 2)
{
System.out.println("You chose to calculate area, given the radius :" + num);
answer2 = (3.14) * 2;
System.out.print("The area of the circle is :");
System.out.println(answer2 + "cm²");
return answer2;
}
else if (numInt == 3)
{
System.out.println("You chose to calculate volume, given the radius :" + num);
answer3 = 4 / 3 * (3.14) * (num) * (3) * (3) * (3);
System.out.print("The volume of that sphere is : cm³");
System.out.println(answer3 + "cm³");
return answer3;
}
else
// if (numInt == 4)
{
System.out.println("You chose to calculate surface area, given the radius :" + num);
answer4 = 4 * (3.14) * (num) * (2) * (2);
System.out.print("The Surface area of that sphere is :");
System.out.println(answer4 + "cm²");
return answer4;
}
} catch (Exception e)
{
System.out.println("Please do not enter any string values, next time input enter a number ");
return 0;
// how to loop this untill the user inputs a number????
}
}
}
答案 0 :(得分:0)
您需要循环,直到获得有效值。一种方法是循环,直到布尔值设置为true。尝试类似的东西:
boolean inputIsValid = false;
while (!inputIsValid) { ...
然后,当您确定有有效输入时,请添加以下行:
inputIsValid = true;
您的循环将继续,直到inputIsValid变为true。
你也可以创建一个无尽的while循环。然后,当您收到有效输入时,break
退出循环:
while(true) {
//when valid input is received
break;
}
答案 1 :(得分:0)
首先,使用while(true)
循环而不是循环999次。 (考虑到它后面有分号,该循环实际上并没有做任何事情。)
然后,您从return 0;
阻止中移除catch
。
所以它会是
while(true) {
try {
.... //your code
}
catch {...}
} //while loop end bracket
这样,循环只有在到达你的一个返回语句时才会结束。