gpt4 book ai didi

java - 如何在单个方法中单独计算字符串中两个字符的出现?

转载 作者:行者123 更新时间:2023-12-01 20:22:29 25 4
gpt4 key购买 nike

我的程序正在读取的字符串的示例


BABAAAABAAAAAAABAAAABBAAAAAABAAAABABAABAAABABABAABAAAAAABAAAAAABAAAAAA


我目前有两个int数组int[] countA = new int[4]int[] countB = new int[4],其中每个数组中的四个元素对应于特定的维度(Alpha,Bravo,Charlie,Delta)。我试图将上面的String分成7个组,其中第一个字符对应于Alpha,接下来的两个字符对应于Bravo,接下来的两个对应于Charlie,最后两个对应于Delta。如果字符是A或B,则会为相应的尺寸更新数字

(例如:在第9、10、16等字符处检测到B时,EX:countB[2]++

目前,我的程序有两个单独的方法来计算两个字符的出现次数。我觉得这是一种多余的计数方式,因为两种方法中的代码几乎相同。我试图通过将两种计数方法压缩为一个方法来进一步优化程序。

当前程序代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
import java.util.Arrays;
//CSC 142 , Check personality, personalityTest.java
// June 12, 2015, Chloe Wake
// Reads through a file of results from the Keirsey Personality test and outputs the answers in a output file.

public class CountDimensions {
public static final int DIMENSIONS = 4;//the number of personality dimensions

public static void main(String[] args) throws FileNotFoundException{
//Scanner console = new Scanner(System.in);
userIntro(); //give an intro to the user
processFile(); //read in the file and store it so we can access it
}
//method that reads in a file and stores each line in an array
public static void processFile() throws FileNotFoundException {
Scanner console = new Scanner(System.in);//create scanner for user input
File fileName = inputFile(console); //store the file the user input typed in
Scanner read1 = new Scanner(fileName);//create scanner for the .txt file specified by user
Scanner inputFile = new Scanner(fileName); //make a new scanner to read the file now that we know it exists
PrintStream output = new PrintStream(outputFile(console));//create printStream object for output
// while loop to get the desired output
while (inputFile.hasNextLine()) { // while the file has text in the line read through it
String name = inputFile.nextLine(); // first line is the name of the person who took the test, go to the next line
String answers = inputFile.nextLine().toUpperCase(); // line after the name is the
int[] findA = countA(answers);
int[] findB = countB(answers);
int[] percentage = percentage(findB);
String[] type = type(percentage);
outputFile(name, percentage, type, findA, findB);
}

}
public static File inputFile(Scanner console) {//ask for file
System.out.print("Type the filename of the file you want to input: ");
File answer = new File(console.nextLine());
while (!answer.exists()) { //if it doesn't exist, throw an error
System.out.println("That file does not exist! Try again.");
answer = new File(console.nextLine()); //store the file the user input typed in
}
return answer;
}
//method that writes output back into a text file
public static File outputFile(Scanner console) {
System.out.print("output file name? ");// PrintStream to write to a user specified output file
File output = new File (console.nextLine());
return output;
}
//method to count how many B's the person answered and stores them in an array
public static int[] countB(String answers) { // takes the information from the user input that was stored in the answers variable
int[] countB = new int[DIMENSIONS]; // method declares the array of 4, searching through 4 at a time
for (int i = 0; i < answers.length(); i++) { // for every time i is less than the length of the answer continue on
char t = answers.charAt(i); // declare a char and make it set to the character at place i in the answers
if (t == 'B') { // if at that spot you find a B continue
if (i % 7 == 0) { // found a B is the spot it is at evenly divisible by 7 aka in the 7th spot?
countB[0]++; // ok then at it to the array in the first spot
}
if (i % 7 == 1 || i % 7 == 2) { //is the B found in
countB[1]++;// ok then at it to the array in the second spot
}
if (i % 7 == 3 || i % 7 ==4) {

countB[2]++;// ok then at it to the array in the third spot
}
if (i % 7 == 5 || i % 7 == 6) {
countB[3]++; // ok then at it to the array in the fourth spot
}

}
}
return countB;
}
// method counts the A's in each dimension and return the total as an array
public static int[] countA(String answers) {
int[] countA = new int[DIMENSIONS];
for (int i = 0; i < answers.length(); i++) {
char t = answers.charAt(i);
if (t == 'A') {
if (i % 7 == 0) {
countA[0]++;
}
if (i % 7 == 1 || i % 7 == 2) {
countA[1]++;
}
if (i % 7 == 3 || i % 7 ==4) {
countA[2]++;
}
if (i % 7 == 5 || i % 7 == 6) {
countA[3]++;
}
}
}
return countA;
}

最佳答案

要利用相似的代码,您可以尝试区分不同的部分并将其用作参数。在您的情况下,数组countAcountB是相似的,唯一的文字'A''B'会更改。此原理称为'Encapsulate what varies'

因此,新方法可能如下所示:

 public static int[] countA(String answers, char charToFind) {
int[] count = new int[DIMENSIONS];
for (int i = 0; i < answers.length(); i++) {
char t = answers.charAt(i);
if (t == charToFind) {
if (i % 7 == 0) {
count[0]++;
}
if (i % 7 == 1 || i % 7 == 2) {
count[1]++;
}
if (i % 7 == 3 || i % 7 == 4) {
count[2]++;
}
if (i % 7 == 5 || i % 7 == 6) {
count[3]++;
}
}
}
return count;
}


和呼叫分别是

 int[] findA = count(answers, 'A');
int[] findB = count(answers, 'B');


这也允许您使用任何其他字符。

关于java - 如何在单个方法中单独计算字符串中两个字符的出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58929901/

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