gpt4 book ai didi

java - 方法打印出错误的百分比整数

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

在这个程序中,我创建一个数组并查找字母字符的总数,并查找 A-D 字母总数的百分比。我创建了一个方法,使我能够找到字符在序列中出现的百分比。

例如,对于 AABD 数组,字符 A 将占数组的 50%。我已经测试过这个方法,它基本上有效,直到我尝试字符 D,其中我莫名其妙地得到了 26 而不是 30%,这让我很困惑。

我使用的数据来自此数据文本,它忽略了不在 A-D 之间的字母我试图从该方法中打印出 30 的百分比,但我一直得到 26,我不确定为什么

示例#1:

AAAAABBBBBCCCCCDDDDD
AAEBCBAFBBCDCECDADDEFEEFFF

示例#2:

AAATAABTBBBBCCCCTCDDTDDD
AASAABBSBBCCSCCDSDDDEEEAEEFBFFFDDF

我的代码:

import java.io.*;
import java.util.*;

public class findSequence {
public static int findPercentages(int perNum, int totalNum) {
int percent = (int) Math.round(perNum * 100.0 / totalNum);
return percent;
}

public static void main(String[] args) {
File inputFile = new File("ShortData.txt");
File outputFile = new File("newDNAOutput");
PrintStream outputStream = null;

Scanner fileScan = null;

try {
fileScan = new Scanner(inputFile);
outputStream = new PrintStream(outputFile);
} catch(Exception e) {
System.out.println("File not found " + e);
System.out.println("Exiting program");
System.exit(1);
}

int numA = 0;
int numB = 0;
int numC = 0;
int numD = 0;

while(fileScan.hasNextLine()) {
String theLine = fileScan.nextLine();
Scanner lineScan = new Scanner(theLine);

if(theLine.length() > 10) {
theLine = theLine.toUpperCase();
char[] dnaArray = new char[theLine.length()];

for(int j = 0; j < dnaArray.length; j++) {
dnaArray[j] = theLine.charAt(j);
}

Arrays.sort(dnaArray);

for(int i = 0; i < dnaArray.length; i++) {
if(dnaArray[i] == 'A') {
numA = numA + 1;

}

if(dnaArray[i] == 'B') {
numB = numB + 1;
}

if(dnaArray[i] == 'C') {
numC = numC + 1;
}

if(dnaArray[i] == 'D') {
numD = numD + 1;
}
}

int totalSum = numA + numB + numC + numD;

int numAPer = findPercentages(numA, totalSum);
int numBPer = findPercentages(numB, totalSum);
int numCPer = findPercentages(numC, totalSum);
int numDPer = findPercentages(numD, totalSum);

outputStream.println(numDPer);
}
}
}
}

最佳答案

76 个字符中有 20 个 D(不包括单词 Example #1Example #2 中的两个 a)。这是以下百分比:26.31578947368421,大约为 26%。如果你想占 30%,那么总共应该有大约 66 个字符。

要获得每行的百分比,您只需将字母计数的声明移动到 while 循环内即可:

while(fileScan.hasNextLine()) {
int numA = 0;
int numB = 0;
int numC = 0;
int numD = 0;

String theLine = fileScan.nextLine();

if(theLine.length() > 10) {
theLine = theLine.toUpperCase();
char[] dnaArray = new char[theLine.length()];

for(int j = 0; j < dnaArray.length; j++) {
dnaArray[j] = theLine.charAt(j);
}

Arrays.sort(dnaArray);

for (char c : dnaArray) {
switch (c) {
case 'A':
numA += 1;
break;
case 'B':
numB += 1;
break;
case 'C':
numC += 1;
break;
case 'D':
numD += 1;
break;
}
}

int totalSum = numA + numB + numC + numD;

int numAPer = findPercentages(numA, totalSum);
int numBPer = findPercentages(numB, totalSum);
int numCPer = findPercentages(numC, totalSum);
int numDPer = findPercentages(numD, totalSum);

outputStream.println(numDPer);
}

}

这将在每次循环运行时清除值。

关于java - 方法打印出错误的百分比整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62272116/

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