Getting rid of extra output in JAVA While Loop -


i have question program wrote class. functioning way need except 1 minor detail. while-loop adding "+" on end of print statement.

public class fractions {     public static void main (string[] args)     {         //-----declare variables-----         int numoffractions  = 0,             numerator   = 0;         double total    = 0;         decimalformat df1 = new decimalformat("#0.##");         scanner stdin = new scanner(system.in);          //----welcome msg------         system.out.println("");         system.out.println("\t* * * welcome fractions * * * ");         system.out.println("");          //-----get numoffractions------         system.out.print("enter number of fractions: ");         numoffractions = stdin.nextint();          //----begin loop-------         numerator = 1;          while(numerator <= numoffractions)         {              while(numoffractions > 0)             {                 system.out.print("" + numerator + "/" + numoffractions + " " + "+" + " ");                 total += (double)numerator / numoffractions;                 numoffractions--;                 numerator++;             }         }          system.out.println("");         system.out.println("");         system.out.print("the total: " + df1.format(total));         system.out.println("");         system.out.println("\nthanks using fraction adder program\n");       } // end main } // end pgm 

here picture of output

there many ways simplest approach can using if/else condition, try this:

public class fractions {     public static void main(string[] args)     {         // -----declare variables-----         int numoffractions = 0, numerator = 0;         double total = 0;         decimalformat df1 = new decimalformat("#0.##");         scanner stdin = new scanner(system.in);          // ----welcome msg------         system.out.println("");         system.out.println("\t* * * welcome fractions * * * ");         system.out.println("");          // -----get numoffractions------         system.out.print("enter number of fractions: ");         numoffractions = stdin.nextint();          // ----begin loop-------         numerator = 1;          while (numerator <= numoffractions)         {              while (numoffractions > 0)             {                 if (numoffractions > 1)                 {                     system.out.print("" + numerator + "/" + numoffractions + " " + "+" + " ");                 }                 else                 {                     system.out.print("" + numerator + "/" + numoffractions + " ");                 }                  total += (double) numerator / numoffractions;                 numoffractions--;                 numerator++;             }         }          system.out.println("");         system.out.println("");         system.out.print("the total: " + df1.format(total));         system.out.println("");         system.out.println("\nthanks using fraction adder program\n");      } // end main } // end pgm 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -