0
반올림에 문제가 있습니다. 웬일인지 나는 testP
를 10 분의 1 자리로 돌릴 수 없다. 예를 들어 첫 번째 예제 (Alex Smith)에서 82.0의 답을 얻었지만 82.6이어야합니다. 당신이 현재 정수 나누기를 int
이있다반올림 오류는 나를 잘못 입력했습니다
double testP =num/numT;
double testP = ((double) num)/numT;
num
으로
및 numT
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.math.*;
import java.util.Scanner;
public class Average {
public static void main(String[] args) {
int num = 0, temp =0;;
String fileName="AverageInput.txt";
String fileName2="AverageOutput.txt";
Scanner inputStream = null;
PrintWriter outputStream = null;
double homeworkP = 0;
//read
try{
inputStream = new Scanner(new File(fileName)); //try to open the file
}
catch(Exception e){
System.out.println("Could not open the file named "+ fileName); // if it doesn't find it, tell them
System.exit(0); // and then exit.
}
//write
try{
outputStream = new PrintWriter(new FileOutputStream(fileName2,true)); //try to create the file
}
catch(Exception e){
System.out.println("Could not open the file named "+ fileName2); // if it doesn't find it, tell them
System.exit(0); // and then exit.
}
int numH = inputStream.nextInt();
int numT = inputStream.nextInt();
int[] arrayH = new int[numH];
outputStream.println("Averages");
String blank = inputStream.nextLine();
while (inputStream.hasNextLine()){
String line = inputStream.nextLine().replaceAll(" H","").replaceAll(" T","");
String[] nameParts = line.split(" ");
String name = nameParts[0] +" "+ nameParts[1];
for (int i =0, p=2; i < numH; i++, p++){ //collects homework grades.
arrayH[i] = Integer.valueOf(nameParts[p]);
temp = temp + arrayH[i];
}
homeworkP = (temp - arrayMin(arrayH))/(numH-1) ; //gets percentage as rounded decimal.
//System.out.println("homeP: " + homeworkP);
temp =0;
num = 0;
for (int p = numH; p < numT + numH; p++){ //collects test grades.
num = num + Integer.valueOf(nameParts[p]);
}
double testP =num/numT; //gets percentage as decimal.
System.out.println("homep: "+ homeworkP + "TestP: "+ testP);
double TotalP = (homeworkP * 40) + (testP * 60);
System.out.println(name + " " + TotalP);
}
outputStream.close();
inputStream.close();
}
public static int arrayMin(int[] arr) {
int i = 0;
int min = Integer.MAX_VALUE;
if (arr == null) {
return 0;
} else {
while (i < arr.length) {
if (arr[i] < min) {
min = arr[i];
}
i++;
}
}
return min;
}
}
"나는 'testP'를 열 번째 자리로 반올림 할 수 없습니다. '라는 말은 여러분이 시도한 것을 의미합니다. 너 뭐 해봤 니? 무엇이 잘못 되었습니까? 힌트 : 추론 기능을 시도하려고합니다. – Colleen
더 이상 설명하지 못해 죄송합니다. 이것은 여러 번 편집 한 코드 조각입니다. 나는 Math.round와 다른 것들을 사용하기 전에 시도했다. 네가 더 친절하길 제안 할 수 있겠 니? –