我这里有一个代码,应该将摄氏温度转换为华氏温度,反之亦然,然后询问用户是否要再次运行该程序。但是有一个限制,即用户只能输入-100 - 100 范围内的数字。如果用户输入的数字大于或小于该范围,则必须要求用户输入新数字。我在程序结束时遇到问题,并要求用户在输入正确的数字后重新运行程序。有没有人有什么建议?谢谢!
import javax.swing.JOptionPane;
public class Temp3 {
public static void main(String[] args) {
// String sentence = "This is a Java program for converting
// temperatures.";
float option = JOptionPane.YES_OPTION;
while (option == JOptionPane.YES_OPTION) {
option = JOptionPane.showConfirmDialog(null, "Would you like to convert from Celsius to Fahrenheit?");
if (option == JOptionPane.YES_OPTION) {
c2f();
}
else if (option == JOptionPane.NO_OPTION) {
f2c();
}
else {
JOptionPane.showMessageDialog(null, "Goodbye");
System.exit(0);
}
option = JOptionPane.showConfirmDialog(null, "Would you like to run the program again?");
}// while loop
if (option == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null, "Goodbye");
} // main method
}
private static void c2f() {
String celVal = JOptionPane.showInputDialog("Please enter degrees in Celsius. ");
float cel = Float.parseFloat(celVal);
while (cel <= 100 && cel >= -100) {
double holder = ((cel * 1.8) + 32);
JOptionPane.showMessageDialog(null, "The temperature in Fahrenheit is " + holder);
break;
}
while (cel > 100) {
String celVal2 = JOptionPane.showInputDialog("That number is too high. Please enter a number lower than 100 and try again.");
float cel2 = Float.parseFloat(celVal2);
while (cel2 <= 100 && cel2 >= -100){
JOptionPane.showMessageDialog(null, "The temperature in Fahrenheit is " + ((cel2 * 1.8) + 32));
cel2 = 101;
}
}
}
// cel to fah
private static void f2c() {
String fahVal = JOptionPane.showInputDialog(
"You are now converting from Fahrenheit to Celsius. Please enter degrees in Fahrenheit.");
float fah = Float.parseFloat(fahVal);
while (fah < 100 && fah >-100){
JOptionPane.showMessageDialog(null, "The temperature in Celcius is " + ((fah - 32) / 1.8));
break;
}
if (fah > 100) {
JOptionPane.showMessageDialog(null,
"That number is too high. Please enter a temperature less than 100 and try again.");
}
else if (fah < -100) {
JOptionPane.showMessageDialog(null,
"That number is too low. Please enter a temperature more than -100 and try again.");
}
} // fah to cel
最佳答案
这是一个很好的解决方案:
import javax.swing.JOptionPane;
public class Temp3 {
public static void main(String[] args) {
// String sentence = "This is a Java program for converting
// temperatures.";
String [] options = {"Convert from Celsius to fahrenheit", "Convert from Fahrenheit to Celsios","Exit"};
int choice=0;
do{ //do while is used so that the loop will run AT LEAST ONCE
choice = JOptionPane.showOptionDialog(null,"What do you want to do?",
"Choose an option", JOptionPane.YES_NO_CANCEL_OPTION, //option type
JOptionPane.QUESTION_MESSAGE,
null, options, options[0]);
convertTemp(choice);
}while(choice!=2);
}
private static void convertTemp(int choice) {
// TODO Auto-generated method stub
String from = "", to = "";
if(choice==0){ //set to and form so that you don't need to type the units again and again
from="Celsius";
to ="Fahrenheit";
}else if(choice==1){
to="Celsius";
from ="Fahrenheit";
}else{
JOptionPane.showMessageDialog(null,"Goodbye","Exit",JOptionPane.INFORMATION_MESSAGE);
//I often put message type to make it more presentable
return; //exit method :)
}
Float tempFrom= Float.parseFloat(JOptionPane.
showInputDialog("You are now converting from "+from+" to "+ to+"."
+ "Please enter degrees in "+from +".")); //get input
while(tempFrom<-100 ||tempFrom>100){ //check if input is valid
if(tempFrom>100){
tempFrom = Float.parseFloat(JOptionPane.showInputDialog(null, "That number is too high. "
+ "Please enter a temperature between -100 & 100 degress "+from , "Temperature too high"
, JOptionPane.ERROR_MESSAGE));//inform the user about the error and ask for the input at the same time
}else{
tempFrom = Float.parseFloat(JOptionPane.showInputDialog(null, "That number is too low. "
+ "Please enter a temperature between -100 & 100 degress "+from , "Temperature too low"
, JOptionPane.ERROR_MESSAGE));//inform the user about the error and ask for the input at the same time
}
}
double answer =0;
if(choice==0){ //celsius to fahrenheit
answer = (tempFrom * 1.8) + 32;
}else{ //fahrenheit to celsius
answer = (tempFrom- 32) / 1.8;
}
JOptionPane.showMessageDialog(null, "The temperature in "+ to +" is " +
answer
,to,
JOptionPane.INFORMATION_MESSAGE);
}
}
基本上,我使用 showOptionDialog 而不是 showConfirmDialog,以便我可以编辑按钮的标题。我们需要至少运行一次应用程序,因此do while循环是完美的。
关于java - java新手,while循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31395796/