12. oktober 2008 - 12:14Der er
4 kommentarer og 1 løsning
else-problem.. burde være hurtigt klaret
Hej. Jeg har stirret mig blind på hvorfor den sidder fejl ved else-funktionen. Det er sikkert noget parantes-værk. Nogle der kan svare på det?
Hilsen Michael
import javax.swing.JOptionPane;
public class AS61 {
/** * author: Michael Lunde */
public static void main (String args[]) {
double fahrenheit, celcius;
boolean waitingForInput = true;
do { fahrenheit = Integer.parseInt(JOptionPane.showInputDialog( "Please enter a value of Fahrenheit:")); if (Integer.parseInt(JOptionPane.showInputDialog())); { fahrenheit = Integer.parseInt(); waitingForInput = false; } else { fahrenheit = Integer.parseInt(JOptionPane.showInputDialog("Invalid value. Please re-enter!")); continue; } } while (waitingForInput);
celcius = ((fahrenheit-32)*5)/9;
System.out.println("This value is " + celcius + " in degrees Celcius");
Det er rettet... men nu siger den fejl ved både if og else. Hvad skal så gøres?
import javax.swing.JOptionPane;
public class AS61 {
/** * author: Michael Lunde */
public static void main (String args[]) {
double fahrenheit, celcius;
boolean waitingForInput = true;
do { fahrenheit = Integer.parseInt(JOptionPane.showInputDialog( "Please enter a value of Fahrenheit:")); if { (Integer.parseInt(JOptionPane.showInputDialog())); fahrenheit = Integer.parseInt(); waitingForInput = false; } else { fahrenheit = Integer.parseInt(JOptionPane.showInputDialog("Invalid value. Please re-enter!")); continue; } } while (waitingForInput);
celcius = ((fahrenheit-32)*5)/9;
System.out.println("This value is " + celcius + " in degrees Celcius");
Du skal fange NumberFormatException fra Integer.parseInt:
import javax.swing.JOptionPane;
public class AS61 {
/** * author: Michael Lunde */
public static void main (String args[]) {
double fahrenheit = 0, celcius;
boolean waitingForInput = true;
String message = "Please enter a value of Fahrenheit:"; do { try{ fahrenheit = Integer.parseInt(JOptionPane.showInputDialog(message)); waitingForInput = false; } catch(NumberFormatException e ){ message = "Invalid value. Please re-enter!"; } } while (waitingForInput);
celcius = ((fahrenheit-32)*5)/9;
System.out.println("This value is " + celcius + " in degrees Celcius");
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.