Witam. Kolejny raz podczas swoich początkowch strać z javą napotykam na drobny problem i nie umiem sobie z nim poradzić w prosty sposób, a mianowicie pozbywanie się zer.
actualOutput: 55.5000 83.5324, a chciałbym uzyskać coś takiego:
desirableOutput: 55.5 83.5324
Innymi słowy, żeby liczba która ma np. 5 miejsc po przecinku miała to swoje 5 miejsc po przecinku, a liczba ktora wychodzi 55.5 niech tak wyglada.
Przy printf %.liczba miejscf nie działa, tak samo jak klasa DecimalFormat, poniewaz nie wiem jaka wyjdzie mi liczba wiec nie moge jej sformatowac, potrzebuje czegos uniwersalnego, bo bawienie sie w castowanie tego na string i obcinanie zer nie bardzo mnie zadowala.
//package ex41;
import java.util.Scanner;
public class Grade {
public static void main(String[] args) {
int scores[][] = new int[2][2];
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter your math and science scores for 9th grade: ");
String scoreLine = scanner.nextLine();
//split the string to get an array of Strings which will
//hold the values entered by the user
String strScores[] = scoreLine.split(" ");
//convert the first number (which represents the score for math in 9th grade)
//from a String to an integer and store it in scores[0][0]
scores[0][0] = Integer.parseInt(strScores[0].trim());
//convert the second number (which represents the score for science in 9th grade)
//from a String to an integer and store it in scores[0][1]
scores[0][1] = Integer.parseInt(strScores[1].trim());
System.out.println("Please enter your math and science scores for 10th grade: ");
scoreLine = scanner.nextLine();
//split the string to get an array of Strings which will
//hold the values entered by the user
strScores = scoreLine.split(" ");
//convert the first number (which represents the score for math in 10th grade)
//from a String to an integer and store it in scores[1][0]
scores[1][0] = Integer.parseInt(strScores[0].trim());
//convert the second number (which represents the score for science in 10th grade)
//from a String to an integer and store it in scores[1][1]
scores[1][1] = Integer.parseInt(strScores[1].trim());
System.out.println("The average scores for math and science are: ");
///{
//write your code here
//start
double mathAverage = (scores[0][0]+scores[1][0])/2;
double sciAverage = ((double)scores[0][1]+scores[1][1])/2;
System.out.printf("Average Math Score Is: %f\nAverage Science Sore Is: %f", mathAverage, sciAverage);
//end
///}
}
}