gpt4 book ai didi

java - For 循环打印不正确 "."

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

我想做的是让它获取一个看起来像这样的java文件

public class test1 {
public static void main ( string[] args ) {
System.out.println( "This is Test 1." );
}
}

它应该输出一个具有适当间距和缩进的文本文件。到目前为止,我可以获得第一行的正确缩进。但是我在打印结束括号的空格的第二个 for 循环中遇到了麻烦。结束括号像前 3 行一样向外打印,而不是向内打印。抱歉,如果我的变量令人困惑。

这是我到目前为止的代码

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

for( int i = 1; i < 6; i++ ) {
justifyJava( "Test" + i + ".java",
"Justified" + i + ".txt",
4 );
}
}
public static void justifyJava( String inputFileName,
String outputFileName,
int tabSize )
throws FileNotFoundException {


int counter = 0;
int counter2 = 0;
int blah = 0;
File f = new File(inputFileName);
File p = new File(outputFileName);
if (p.exists())
p.delete();
Scanner input = new Scanner (f);
PrintStream name = new PrintStream(new File(outputFileName));

while (input.hasNextLine()) {
String line = input.nextLine();
Scanner lineScan = new Scanner(line);
if (line.contains("{") == true) {
name.print("{\r\n");
counter++;
for (int i = 1; i <= counter; i++) {
for (int j = 0; j <= tabSize; j++) {
name.print(" ");
}
}
System.out.println(counter);
} else if (line.contains("}") == true) {
name.print("\r\n");
counter--;
for (int x = 1; x <= counter; x++) {
for (int y = 1; y <= tabSize; y++) {
name.print(" ");
}
}
name.print("}");
System.out.println(counter);
} else {
name.print(line);
}
}
}

它给我的是

public class Test1 
{
public static void main( String[] args )
{
System.out.println( "This is Test 1." );
}
}

我想要的是这个

public class Test1 
{
public static void main( String[] args )
{
System.out.println( "This is Test 1." );
}
}

最佳答案

好吧,我重构了一些东西(而且是必要的),但它的工作原理完全符合您的预期(AFAIK!)。

代码如下:

public class JavaJustifier {
static int counter = 0;
static int tabSize = 4;

public static void main(String[] args) throws FileNotFoundException {
justifyJava("Test1.java", "Justified1.txt");
}

private static void processLine(PrintStream name, String line) {
if (line.contains("{")) {
String preText = line.substring(0, line.indexOf("{"));
String postText = line.substring(line.indexOf("{") + 1)
.replaceAll("\\n", "").replaceAll("\\r\\n", "");
printSpaces(name);
name.println(preText);
printSpaces(name);
name.println("{");
counter++;
processLine(name, postText);

} else if (line.contains("}")) {
String preText = line.substring(0, line.indexOf("}"));
String postText = line.substring(line.indexOf("}") + 1)
.replaceAll("\\n", "").replaceAll("\\r\\n", "");
name.println(preText);
counter--;
printSpaces(name);
name.println("}");
processLine(name, postText);
} else {
if (!line.equals("\r\n") && line.length() != 0) {
printSpaces(name);
name.print(line);
}
}
}

private static void printSpaces(PrintStream p) {
for (int i = 1; i <= counter; i++) {
for (int j = 0; j <= tabSize; j++) {
p.print(" ");
}
}
}

public static void justifyJava(String inputFileName, String outputFileName)
throws FileNotFoundException {

File f = new File(inputFileName);
File p = new File(outputFileName);
if (p.exists())
p.delete();
Scanner input = new Scanner(f);
PrintStream name = new PrintStream(new File(outputFileName));

while (input.hasNextLine()) {
String line = input.nextLine().trim();
processLine(name, line);
}
}
}

关于java - For 循环打印不正确 ".",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16595585/

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