简体   繁体   English

读取文件并设置为变量

[英]Reading file and setting to variable

I'm new to programming, so I was wondering if someone could help me. 我是编程新手,所以我想知道是否有人可以帮助我。 What I'm trying to is make the user input a number, and if the number is in the file, it'll spit something back out, and if it's not in the file, it'll keep asking for a number. 我想做的是让用户输入一个数字,如果该数字在文件中,它将吐出一些东西,如果它不在文件中,它将不断询问一个数字。 I don't know exactly how to do this, but if someone could point me in the right direction, that would be great. 我不知道该怎么做,但是如果有人可以指出正确的方向,那就太好了。 Thanks in advance! 提前致谢!

  • First read the file and store it (as a Collection , or as a big String , the exact type depends on how you actually trying to achieve this). 首先读取文件并将其存储(作为Collection或Big String ,确切的类型取决于您实际尝试实现的方式)。
    Reading a file can be done in numerous ways, some of them are using a Scanner , BufferedReader or File.readAllLines() 读取文件的方式有很多种,其中一些使用ScannerBufferedReaderFile.readAllLines()
  • Now, create a while loop - that repeatidly asks the user for a number, until the condition that the number is in your data is met. 现在,创建一个while循环 -反复询问用户一个数字,直到满足数据中存在该数字的条件为止。
  • Next, you need to process and yield the desired output. 接下来,您需要处理并产生所需的输出。

Bonus: 奖金:
Try to do each of the bullets in a different method and only combine them in your main() - it will help you to learn about basic methods design as well. 尝试用不同的方法来处理每个项目符号,并且仅将它们组合在main() -这也将帮助您了解基本方法的设计。

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

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