gpt4 book ai didi

java - 识别文件中的某个字符并将其隐藏以防止执行

转载 作者:行者123 更新时间:2023-12-02 08:41:02 25 4
gpt4 key购买 nike

我是java初学者,我正在尝试做一种“谁想成为百万富翁”游戏。我用 10 个级别来做。

我有 1 个文件夹,其中包含 5 个级别的 txt 文件(每个 txt 文件中有 1 个问题)。因此,如果您重复游戏一两次,您可能不会再遇到同样的问题。文本文件具有与此相同的结构。

What is the minimum of players in a footbal game?0 1)80 2)100 3)91 4)7  

The boolean before the answear options points the correct answear but when i read the file. I want to read the file and identify the correct answear(but not print it) to tell if the option the player choose is right or not.

How can I do it? Is there another way to the same? Thanks for helping :)

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

Scanner input=new Scanner (System.in);
int option;
boolean menu;
double[] balance = new double[0];

//::::::::::::::::::::MENU:::::::::::::::::::::::::::::
System.out.println("<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>");
System.out.println("<<<Who wants to be millionaire?>>>");
System.out.println("<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>");

System.out.println("whats your name?");
String name = input.nextLine();
System.out.println("Welcome"+ name);

menu = true;
while(menu==true){
System.out.println("<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>");
System.out.println("<<<<<<<<<< 1 - Play >>>>>>>>>>");
System.out.println("<<<<< 2 - Last Score >>>>");
System.out.println("<<<< 3 - Best 10 scores >>>>");
System.out.println("<<<<<<<<<<< 4 - Exit >>>>>>>>>>");
System.out.println("<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>");

System.out.println("choose your option: ");
opcao=input.nextInt();

//:::::::::::::::::::: 1 - Play :::::::::::::::::::::::::::::
switch(option){
case 1:
//random char to create the file name
Random r = new Random();
char c = (char)(r.nextInt(5) + 'a');
String LetterFile=String.valueOf(c);
System.out.println(letterFile);
String nameFile = letterFile + ".txt";


BufferedReader read = new BufferedReader(new FileReader(nameFile));
String line;
while((line = read.readLine()) != null)
{
System.out.println(line);
}
ler.close();

System.out.println("Whats the answear?: ");
String answear1 = input.nextInt();
System.out.println("Escolheu a resposta: "+answear1 );

break;

//:::::::::::::::::::: 4 - EXIT :::::::::::::::::::::::::::::
case 4:

System.out.println("THANKS FOR PLAYINGr!");
System.exit(0);

}//while
} //switch case
}//main

}

Structure of questions file - 5 files for each folder named a.txt, b.txt, c.txt, d.txt, e.txt

最佳答案

使用下面的 JSON 结构。每个文件都会有一个 json。您选择第一个文件,读取 json 并将其填充到您的 java 对象中(您可以使用 jackson 库,它会自动为您执行此操作,您只需要创建要映射的 POJO 结构)。

所以

当您只需要显示选项时:使用 json 中的选项数组对象填充 java 对象。

当您还需要显示答案时:您可以从 java 对象中选择答案键,并从 json 填充

JSON:

{
"GameName": "millionaire game",
"level": 1,
"questions": [
{
"question": "What is the minimum of players in a footbal game?",
"options": [
8,
10,
9,
7
],
"answer": "7"
},
{
"question": "Who scored maximum goal footbal game?",
"options": [
"Jhon",
"Pitty",
"Richard",
"Mike"
],
"answer": "Mike"
},
{
"question": "What is the maximum of players in a footbal game?",
"options": [
8,
10,
9,
7
],
"answer": "7"
}
]
}

始终使用 JSON 来执行此类任务。 Json 更易于维护和管理。

关于java - 识别文件中的某个字符并将其隐藏以防止执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61389051/

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