gpt4 book ai didi

java - 我怎样才能以某种方式打印我的阵列?

转载 作者:行者123 更新时间:2023-11-29 05:33:10 25 4
gpt4 key购买 nike

<分区>

所以我想在列表中打印我的数组。看起来像这样。

      Word:        Count:
Myths 2
Of 15
Babylonia 25

我似乎无法弄清楚如何以正确的方式打印它,这是我目前拥有的代码。感谢任何帮助,谢谢!

package program6;

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;

public class Program6 {

static String[] stringArray = new String[100];
static int[] intArray = new int[100];
static String fileName = "myths.txt";
static int currentWordIndex = 0;

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

Scanner input = new Scanner(new File(fileName));

while (input.hasNext()){
String word = input.next();
boolean alreadyExists = false;
for (int i = 0; i < currentWordIndex; i++) {

if(stringArray[i].equals(word)){
alreadyExists = true;
intArray[i]++;
break;
}
}


if(!alreadyExists && currentWordIndex <100){
stringArray[currentWordIndex] = word;
intArray[currentWordIndex++] = 1;
}
}

System.out.println("Myths of Babylonia and Assyria");
System.out.println("Word: Count:");
System.out.println(Arrays.toString(stringArray));
System.out.println();
System.out.println(Arrays.toString(intArray));
}
}

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