简体   繁体   English

搜索文本文件并随机选择?

[英]Searching through text file and randomly choosing?

my homework is this if more context is needed - I would explain it but it is pretty long to explain and the text files are provided on the site if people need to look at them: http://www.cis.upenn.edu/~cis110/hw/hw06/index.html 如果需要更多上下文,我的家庭作业是这样的-我会解释一下,但是解释起来很长,如果人们需要查看它们,文本文件会在网站上提供: http : //www.cis.upenn.edu/ 〜cis110 / hw / hw06 / index.html

Right now I am on Step 2 and stuck on randomly choosing from the three items associated with the treasure class, checking to see if they start with "tc". 现在,我处于第2步,坚持从与宝藏类关联的三个项目中随机选择,检查它们是否以“ tc”开头。 I can extract the treasure class from the monster.txt file and I have the monster. 我可以从monster.txt文件中提取宝藏类,并且我有怪物。 This is my method for finding the treasure class: 这是我找到宝藏类别的方法:

public static void getTreasureClass(Monster monGet)
    throws FileNotFoundException{
    Random rand = new Random();
    String tc=monGet.getTreasureClass();
    Scanner file=new Scanner(new File ("TreasureClassEx.txt"));
    System.out.println(tc);
    while(!file.next().equals(tc)){
        file.next();
        }
    tc=file.next();
    if (tc.startsWith("tc:")){

    }
    else {
        System.out.println("test");
        }   
    }

It is extremely incomplete, but I would appreciate some tips on where to go next in terms of randomly choosing from the three items, or if my code is bad. 它是非常不完整的,但是我希望您从随机选择这三个项目中或者如果我的代码不好的话,了解一些下一步的建议。 Thanks in advance! 提前致谢!

Makes sure that you import the things that I add, because you dont show imports i will not add them 确保您导入的是我添加的内容,因为您没有显示导入内容,因此我不会添加它们

public static void getTreasureClass(Monster monGet)
    throws FileNotFoundException{
    Random rand = new Random();
    String tc=monGet.getTreasureClass();
    Scanner file=new Scanner(new File ("TreasureClassEx.txt"));
    System.out.println(tc);
    List<String> list = new LinkedList<String>();
    while(!file.next().equals(tc)){
        file.next();
        }
    tc=file.next();
    if (tc.startsWith("tc:")){
    list.add(tc);
    }

    String treasure = list.get(rand.nextInt(list.size()));
    else {
        System.out.println("test");
        }   
    }

So in this I saved the example in the String value 'treasure' 所以在此我将示例保存在字符串值“ treasure”中

I dont feel good helping you with your homework -_- 我不能很好地帮助您完成作业-_-

So a 'Hell_Bovine' has a Treasure Class of "tc:Cow_(H)". 因此,“地狱牛”的宝藏等级为“ tc:Cow_(H)”。

So you're looking for this line in TreasureClassEx.txt 因此,您正在TreasureClassEx.txt中查找此行

tc:Cow_(H)  tc:Act_5_(H)_Equip_B    tc:armo3    tc:armo3

Then you'll choose one of the three options at random. 然后,您将随机选择三个选项之一。

And you'll keep reading the TreasureClassEx, finding the right line, and making a random choice for as long as the "treasure class" that you're looking for starts with "tc:". 而且,只要您要查找的“宝藏类”以“ tc:”开头,您就将继续阅读TreasureClassEx,找到正确的行,并做出随机选择。

For example, for "tc:Cow_(H)" you might choose "tc:armo3". 例如,对于“ tc:Cow_(H)”,您可以选择“ tc:armo3”。 For "tc:armo3", you might choose "Quilted_Armor". 对于“ tc:armo3”,可以选择“ Quilted_Armor”。 And then you would stop there. 然后您会停在那儿。

At least that's how I'm reading the instructions. 至少这就是阅读说明的方式。 ;-> ;->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM