gpt4 book ai didi

java - 我的代码和/或逻辑有什么问题?

转载 作者:行者123 更新时间:2023-12-01 16:36:40 25 4
gpt4 key购买 nike

我在执行 APCS 分配时遇到一些问题。该程序应该从文本文件 test1.txt 中读取长度为 2 的字符串,并打印出以下内容的百分比:a) 女孩-女孩、男孩-男孩、男孩-女孩或女孩-男孩组合,以及 b)各个组的总数。

我已经尝试了一个小时来解决这个问题!尽管我对第 25 行中的 String 声明表示怀疑,但我没有办法确认这一点。此外,我担心我弄乱了 if-else-if-else 循环而没有提示编译器错误。

附上源码供大家引用。如果您需要任何其他信息,请随时询问。

由于我是新用户,声誉 < 10,请参阅附图:

http://imgur.com/7HpJF

详细说明什么不起作用。我截图并写了相关评论!

/**
* Family takes user input of sets of boys, girls, and boys + girls. Results are then
* tabulated and displayed in a percentage form to the user. The total number of
* individuals are also displayed.
*
* @E. Chu
* @Alpha
*/

import java.util.Scanner;
import java.io.File;
import java.io.IOException;

public class Family {

public static void main (String[] args) throws IOException {

int boyCount = 0;
int girlCount = 0;
double boyGroupCount = 0.0;
double girlGroupCount = 0.0;
int mixedGroupCount = 0;
int totalPersonCount = 0;
double totalGroupCount;
String currentToken = " ";
Scanner inFile = new Scanner (new File ("test1.txt"));

while (inFile.hasNextLine()) {
currentToken = inFile.nextLine( );
if (currentToken == "BG") {
boyCount++;
girlCount++;
mixedGroupCount++; }
else if (currentToken == "GB") {
boyCount++;
girlCount++;
mixedGroupCount++; }
else if (currentToken == "BB") {
boyCount += 2;
boyGroupCount++; }
else {
girlCount += 2;
girlGroupCount++; }
}

inFile.close();
totalPersonCount = boyCount + girlCount;
totalGroupCount = boyGroupCount + girlGroupCount + mixedGroupCount;
System.out.println("Sample Size: " + totalPersonCount);
System.out.println("Two Boys (%): " + boyGroupCount / totalGroupCount + "%");
System.out.println("One Boy, One Girl (%): " + mixedGroupCount + "%");
System.out.println("Two Girls (%): " + girlGroupCount / totalGroupCount + "%");

} // End of main method.

} // End of class Family.

最佳答案

currentToken == "BB" 应该是 currentToken.equals("BB")

不要使用==,而是使用equals方法

关于java - 我的代码和/或逻辑有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8321372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com