gpt4 book ai didi

java - (Java,mad-libs 游戏)我在一行上收到两个提示输入问题,而它们应该各有自己的一行

转载 作者:行者123 更新时间:2023-12-02 03:42:10 24 4
gpt4 key购买 nike

这是华盛顿大学类(class)中的一个疯狂的游戏。我不是那里的学生,只是跟随。

我从中获取信息的文本文件如下所示:

One of the most <adjective> characters in fiction is named
"Tarzan of the <plural-noun> ." Tarzan was raised by a/an
<noun> and lives in the <adjective> jungle in the
heart of darkest <place> . He spends most of his time
eating <plural-noun> and swinging from tree to <noun> .
Whenever he gets angry, he beats on his chest and says,
" <funny-noise> !" This is his war cry. Tarzan always dresses in
<adjective> shorts made from the skin of a/an <noun>
and his best friend is a/an <adjective> chimpanzee named
Cheetah. He is supposed to be able to speak to elephants and
<plural-noun> . In the movies, Tarzan is played by <person's-name> .

这是我遇到问题的输出:

Please type a/an plural noun: Please type a/an noun:

我希望它们位于自己单独的行上(显然)。仅当两个“< >”位于同一行时才会发生。

这是我正在使用的代码部分:

Scanner search = new Scanner(file);

while (search.hasNextLine()) {
String prompt = search.next();

if (prompt.startsWith("<") && prompt.endsWith(">")) {
prompt = prompt.replace('<', ' ');
prompt = prompt.replace('>', ':');
prompt = prompt.replace('-', ' ');
System.out.print("Please type a/an" + prompt + " ");
String funnyText = console.next();
output.print(funnyText + " ");
} else {
output.print(prompt + " ");
}
}

希望你们中的一个人能够解决这个问题并帮助我。

我尝试过:

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

public class Homework {
public static void main(String[] args) throws FileNotFoundException {
Scanner console = new Scanner(System.in);

System.out.println("Welcome to the game of Mad Libs.");
System.out.println("I will ask you to provide various words");
System.out.println("and phrases to fill in a story.");
System.out.println("The result will be written to an output file.");
System.out.println();

Boolean menuLoop = false;

while (menuLoop == false) {
System.out.print("(C)reate mad-lib, (V)iew mad-lib, (Q)uit? ");
String input = console.next();
if (input.toLowerCase().startsWith("c")) {
create(console);
menuLoop = true;
} else if (input.toLowerCase().startsWith("v")) {
System.out.println("Game will play soon.");
menuLoop = true;
} else if (input.toLowerCase().startsWith("q")) {
System.out.println("Thanks for playing.");
menuLoop = true;
} else {
menuLoop = false;
}
}
}

public static void create(Scanner console) throws FileNotFoundException {
System.out.print("Input file name: ");
String inputFile = console.next();
File file = new File(inputFile);

while (!file.exists()) {
System.out.print("File not found. Try again: ");
inputFile = console.next();
file = new File(inputFile);
}
System.out.print("Output file name: ");
String outputFile = console.next();
PrintStream output = new PrintStream(new File(outputFile));
System.out.println();

Scanner search = new Scanner(file);

while (search.hasNextLine()) {
String prompt = search.next();

if (prompt.startsWith("<") && prompt.endsWith(">")) {
prompt = prompt.replace('<', ' ');
prompt = prompt.replace('>', ':');
prompt = prompt.replace('-', ' ');
System.out.print("Please type a/an" + prompt + " ");
String funnyText = console.next();
output.print(funnyText + " ");
} else {
output.print(prompt + " ");
}
}

}

}

这是我的输出:

Welcome to the game of Mad Libs. I will ask you to provide various words and phrases to fill in a story. The result will be written to an output file.

(C)reate mad-lib, (V)iew mad-lib, (Q)uit? c Input file name: tarzan.txt Output file name: output

Please type a/an adjective: sadgs Please type a/an plural noun: sagsd sdagsd Please type a/an noun: Please type a/an adjective:

最佳答案

改用System.out.println。这会将输出打印到新行。

关于java - (Java,mad-libs 游戏)我在一行上收到两个提示输入问题,而它们应该各有自己的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56829937/

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