average - I am getting error messages concerning my coding. I know Scanf is not a java function, so I am hoping to get help converting it -


i know scanf not java function, i'm hoping can me understand how convert this. research on topic difficult piece together.

this code:

import java.util.scanner;  public class average {      scanner scanner = new scanner (system.in);      int main (){         int counter;         int number;         int total;          float average;          total = 0;         counter = 0;          system.out.println("enter number 0 end: ");         scanf("%d", &number);          while (number != 0) {             total = total + number;             counter = counter + 1;             system.out.println("enter number 0 end: ");             scanf("%d", &number);         }          if(counter != 0) {             average = (float) total / counter;             system.out.println("average %.2f\n", average);         } else {             system.out.println("no valid numbers have been entered.");             return 0;         }     } } 

these changes done code:

import java.util.scanner; public class 1 {       public static void main(string[] args) {     scanner scanner = new scanner (system.in);          int counter;         int number = 1;         int total;         float average;         total = 0;         counter = 0;            while(number != 0){              system.out.println("enter number 0 end: ");              number = scanner.nextint();              system.out.printf("%d", number);              total = total + number;             counter = counter + 1;       }         if(counter != 0){          average =  ((float)total /(float)counter);               system.out.printf("average %.2f\n", average);         }         else{             system.out.println("no valid numbers have been entered.");             //return 0;          }      } } 

first scanner takes value user entered. number = scanner.nextint(); must done inside while loop since 1 check condition.

the next thing changed average = (float) total / counter;

this casts total value float. use brackets both ends.

average = ((float)total /(float)counter); this


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -