gpt4 book ai didi

java - 数组读取问题

转载 作者:行者123 更新时间:2023-12-01 22:35:36 27 4
gpt4 key购买 nike

美好的一天。我有以下部分代码,我正在努力使其工作。我有一个包含问题的文本文件,它的结构方式是文本文件中的每个问题占用 10 行,但我只想显示每个问题的前 6 行,然后隐藏其余的。

我在 texfile 中的问题如下所示:

Collections
Which of these is not an example of a "real-life" collection?
a. The cards you hold in a card game.
b. Your favorite songs stored in your computer.
c. The players on a soccer team.
d. The number of pages in a book.

d. /// answer


Multithreading
Indefinite postponement is often referred to as __________.
a. deadlock.
b. indigestion.
c. starvation.
d. None of the above.

c. /// answer

基本上它只是显示问题而不显示答案。这就是我到目前为止想要实现的目标。

任何帮助将不胜感激。

File filing = new File(file);


pattern.setFileName(filing.getAbsolutePath());

if(filing.getAbsoluteFile().exists())
{
try{

ReadFile myFile = new ReadFile(pattern.getFileName());

String[ ] aryLines = myFile.OpenFile( );

int i;


for ( i=0; i < aryLines.length; i++ )
{
System.out.println( aryLines[ i ] ) ;

if(i == 6)
i = i+3;
}

ReadFile类

import java.io.IOException;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class ReadFile
{
private String path;

///////////// set file name

public ReadFile(String filePath)
{
path = filePath;
}

public String[] OpenFile() throws IOException
{
FileReader fr = new FileReader(path);

BufferedReader textReader = new BufferedReader(fr);


int numberOflines = path.length();

String[] textData = new String[numberOflines];

int count;

for(count = 0; count < numberOflines; count++)
{
textData[count] = textReader.readLine();


}

textReader.close();
return textData;
}

int readLines() throws IOException
{
FileReader fileToRead = new FileReader(path);

BufferedReader br = new BufferedReader(fileToRead);

String aLine;

int numberOfLines = 0;

while ( ( aLine = br.readLine( ) ) != null )
{
numberOfLines++;
}

br.close();

return numberOfLines;
}
}

最佳答案

您可以尝试在嵌套的 for 循环中进行迭代,如下所示:

    for (int i = 0; i < aryLines.length; i += 10) {
// every iteration brings you to the beginning of a new question
for (int j = 0; j < 6; j++) {
System.out.println(aryLines[i + j]);
}
}

关于java - 数组读取问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26918856/

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