New to java, while loop? -


i have code here supposed convert celsius fahrenheit , vice versa, , ask user if want run program again. there boundary that user can enter number ranges -100 - 100. if user enters number greater or less range, has ask user enter new number. i'm having trouble program ending , asking user rerun program after enter correct number. have suggestions? thanks!

import javax.swing.joptionpane; public class temp3 {          public static void main(string[] args) {             // string sentence = "this java program converting             // temperatures.";              float option = joptionpane.yes_option;              while (option == joptionpane.yes_option) {                 option = joptionpane.showconfirmdialog(null, "would convert celsius 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 run 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 " + holder);                 break;             }              while (cel > 100) {                  string celval2 = joptionpane.showinputdialog("that number high. please enter number lower 100 , try again.");                 float cel2 = float.parsefloat(celval2);              while (cel2 <= 100 && cel2 >= -100){                 joptionpane.showmessagedialog(null, "the temperature in fahrenheit " + ((cel2 * 1.8) + 32));                 cel2 = 101;                  }              }              }            // cel fah          private static void f2c() {             string fahval = joptionpane.showinputdialog(                     "you converting fahrenheit celsius. please enter degrees in fahrenheit.");             float fah = float.parsefloat(fahval);             while (fah < 100 && fah >-100){                 joptionpane.showmessagedialog(null, "the temperature in celcius " + ((fah - 32) / 1.8));                 break;             }             if (fah > 100) {                 joptionpane.showmessagedialog(null,                         "that number high. please enter temperature less 100 , try again.");             }              else if (fah < -100) {                 joptionpane.showmessagedialog(null,                         "that number low. please enter temperature more -100 , try again.");               }           } // fah cel 

this solution:

import javax.swing.joptionpane; public class temp3 {      public static void main(string[] args) {             // string sentence = "this java program converting             // temperatures.";             string [] options = {"convert celsius fahrenheit", "convert fahrenheit celsios","exit"};             int choice=0;             do{ //do while used loop run @ least once                  choice = joptionpane.showoptiondialog(null,"what want do?",                          "choose 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 = "", = "";         if(choice==0){ //set , form don't need type units again , again              from="celsius";              ="fahrenheit";         }else if(choice==1){             to="celsius";              ="fahrenheit";         }else{             joptionpane.showmessagedialog(null,"goodbye","exit",joptionpane.information_message);             //i put message type make more presentable             return; //exit method :)          }         float tempfrom= float.parsefloat(joptionpane.                 showinputdialog("you converting "+from+" "+ to+"."                         + "please enter degrees in "+from +".")); //get input          while(tempfrom<-100 ||tempfrom>100){ //check if input valid              if(tempfrom>100){                 tempfrom = float.parsefloat(joptionpane.showinputdialog(null, "that number high. "                         + "please enter temperature between -100 & 100 degress "+from , "temperature high"                         , joptionpane.error_message));//inform user error , ask input @ same time              }else{                 tempfrom = float.parsefloat(joptionpane.showinputdialog(null, "that number low. "                         + "please enter temperature between -100 & 100 degress "+from , "temperature low"                         , joptionpane.error_message));//inform user error , ask input @ same time             }         }             double answer =0;             if(choice==0){ //celsius fahrenheit                  answer = (tempfrom * 1.8) + 32;             }else{ //fahrenheit celsius                  answer = (tempfrom- 32) / 1.8;             }             joptionpane.showmessagedialog(null, "the temperature in "+ +" " +                     answer                       ,to,                     joptionpane.information_message);     } } 

basically used showoptiondialog instead of showconfirmdialog can edit caption of buttons . need run application at least once, do while loop perfect.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -