java - Finding the percentage in my while loop isn't working correctly. Is nested while loop ok? -


i trying learn java while loops. i'm trying make program count passing test scores group of students , output total number of scores inputted, number of passing tests score above 69, , display percentage of tests passed.

the problem running can't seem percentage output correctly. keeps displaying 0.0. following best code came far.

is coding style have nested while loop? there simpler way shorten program? thanks

    import java.util.scanner;     import java.text.decimalformat;      public class countpassingscores {       public static void main(string[] args) {         scanner scan = new scanner(system.in);          // formats percentage output 1 decimal place.         decimalformat df = new decimalformat("###,##0.0");          // counts how many times score entered. passing score not         // considered here.         int count = 0;         // score user enters.         int score = 0;         // percent of class passed test. passing score 70 ,         // above.         double percentofclasspassed = 0.0;         // total number of tests passed. passing score 70 , above.         int numberoftestspassed = 0;          system.out.println("this program counts number of passing "                 + "test scores. (-1 quit)\n");          while (score != -1) {             system.out.print("enter first test score:  ");             score = scan.nextint();              while (count != -1 && score > 0) {                 system.out.print("enter next test score:  ");                 score = scan.nextint();                 count++;                  if (count == -1)                     break;                 else if (score > 69)                     numberoftestspassed++;                 percentofclasspassed = (numberoftestspassed / count);             }          }          system.out.println("\nyou entered " + count + " scores.");         system.out.println("the number of passing test scores "                 + numberoftestspassed + ".");         system.out.println(df.format(percentofclasspassed)                 + "% of class passed test.");     } } 

your code doesn't consider 92 test passed score haven't incremented value of numberoftestspassed in first while loop. hereby few changes made in code snippet:

    while (score != -1) {         system.out.print("enter first test score:  ");         score = scan.nextint();         if(score > 69)         numberoftestspassed++;         while (count != -1 && score > 0) {             system.out.print("enter next test score:  ");             score = scan.nextint();             count++;             if (score == -1)                 break;             else if (score > 69)                 numberoftestspassed++;         }          percentofclasspassed = ((double)numberoftestspassed * 100 / count);      } 

it gives correct output inputs.


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 -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -