私はそれを考え出した。カウンターは double ではなく整数でした。
public class Family
{
public static void main(String[] args) throws IOException
{
String token = "";
File fileName = new File("MaleFemaleInFamily.txt");
Scanner inFile = new Scanner(fileName);
double counterGG = 0;
double counterBG = 0;
double counterBB = 0;
double totalCounter = 0;
while (inFile.hasNext())
{
token = inFile.next();
System.out.println(token);
if (token.equals("GG")) {
counterGG++;
totalCounter++;
} else if (token.equals("BG")) {
counterBG++;
totalCounter++;
} else if (token.equals("GB")) {
counterBG++;
totalCounter++;
} else if (token.equals("BB")) {
counterBB++;
totalCounter++;
}
}
System.out.println();
System.out.println("Sample Size: " + totalCounter);
System.out.println("Two Boys: " + (counterBB / totalCounter) + "%");
System.out.println("One Boy One Girl: " + (counterBG / totalCounter) + "%");
System.out.println("Two Girls: " + (counterGG / totalCounter) + "%");
inFile.close();
}
}
私はすべてを正しくカウンターしましたが、との計算をすると、(counterGG / totalCounter)
ゼロになります。私は間違って何をしましたか?(counterBG / totalCounter)
(counterBB / totalCounter)