I wrote a code to calculate the average of numbers. The problem is that you only can insert whole numbers. Does anybody know how I can change the code so that I can insert float numbers?
Here's the code i did with the help of a tutorial:
Dies ist der Code:
public class main_class {
public static void main(String[] args){
String sentinel = "";
int sum = 0;
int counter = 0;
double mean = 0.0;
Scanner NumScanner = new Scanner(System.in);
System.out.println("Enter numbers to add. Enter \"d\" when done.");
System.out.print("Enter number: ");
sentinel = NumScanner.next();
System.out.println();
while(!sentinel.equals("d") && !sentinel.equals("D")) {
sum += Integer.parseInt(sentinel);
counter++;
System.out.print("Enter number: ");
sentinel = NumScanner.next();
System.out.println();
}
mean = (sum*1.0)/counter;
System.out.println();
System.out.println("The arithmetic mean is: " + mean +".");
}
}