所以我目前正在试图弄清楚我的代码如何读取我的txt文件。我的目标是提示我有什么用于初始化,然后让我输入一个数字但是0来获取我写在我的txt文件上的消息。然后最后通过键入0完成并获得我所拥有的完成消息。我读过在线文章,但我还是有麻烦。这就是我到目前为止所做的:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.ArrayList;
public class FortuneFile
{
static Scanner keyboard;
static int inputLine;
static Scanner inputFile;
static boolean done;
public static void main(String[] args) throws Exception
{
initialization();
while (inputFile.hasNext())
while (!done)
{
mainLoop();
}
finish();
}
public static void initialization() throws Exception
{
Scanner inputFile = new Scanner(new File("FortuneCookie.txt"));
keyboard = new Scanner(System.in);
done = false;
System.out.println("");
System.out.println("Welcome to the Command Box FortuneCookie game!");
System.out.println("====================================================================");
System.out.println("Dare to try your luck?... You could be a Winner or a Looser!");
System.out.println("");
System.out.println("Enter \"0\" if you are scared, or if you are brave, try any number: ");
System.out.println("====================================================================");
}
public static void mainLoop() throws Exception
{
inputLine = inputFile.nextInt();
i++;
if (keyboard.equals("0"))
{
done = true;
}
else
{
{
System.out.println("");
}
System.out.print("Care to try again? ");
System.out.println("");
}
}
public static void finish()
{
System.out.println("====================================================================");
System.out.println("Thanks for playing along. I hope you are not traumatised!");
}
}
谢谢!! :)
答案 0 :(得分:0)
试试这个:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.ArrayList;
public class FortuneFile
{
static Scanner keyboard;
static int inputLine;
static Scanner inputFile;
static boolean done;
static String myMessage;
public static void main(String[] args) throws Exception
{
initialization();
//create an object that reads integers:
Scanner Cin = new Scanner(System.in);
inputLine = Cin.nextInt();
while(inputLine != 0){
System.out.println("");
System.out.print("Care to try again? ");
System.out.println("");
System.out.println("Enter another integer: ");
inputLine = Cin.nextInt();
}
System.out.println(myMessage);
finish();
}
public static void initialization() throws Exception
{
Scanner inputFile = new Scanner(new File("C:/FortuneCookie.txt"));
//keyboard = new Scanner(System.in);
//Read the first line
myMessage = inputFile.nextLine();
//System.out.println(myMessage);
done = false;
System.out.println("");
System.out.println("Welcome to the Command Box FortuneCookie game!");
System.out.println("====================================================================");
System.out.println("Dare to try your luck?... You could be a Winner or a Looser!");
System.out.println("");
System.out.println("Enter \"0\" if you are scared, or if you are brave, try any number: ");
System.out.println("====================================================================");
}
public static void finish()
{
System.out.println("====================================================================");
System.out.println("Thanks for playing along. I hope you are not traumatised!");
}
}
如果需要,您可以删除我的主循环并将其放入另一个过程中。我用0检查输入。如果它为零,则结束。如果没有,它将保持在循环中。我把我的文件放在驱动器c中。您可以将地址更改为文件位置。这是结果:
这是我的文本文件的正文:
我希望这能解决你的问题。如果您有任何其他问题,请与我们联系。
答案 1 :(得分:0)
您正在使用keyboard.equals()这是错误的,因为键盘是Scanner对象而不是String。您应该使用keyboard.nextLine()来获取用户的输入并将其存储在String中。所以你会有类似的东西, String holder = keyboard.nextLine();
更简单的方法是使用类似的东西读取整数, int holder = keyboard.nextInt();
然后使用 ==
将此整数与0进行比较有关Java中扫描仪的更多信息,请查看此链接; http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
同样在initialize()中,您正在制作您在方法中使用的新本地Scanner inputFile。这样做的问题是,当你在initialize()之外调用inputFile时,你会遇到问题,因为initialize()之外的inputFile没有被定义为对你正在使用的文件进行操作。这是范围解决问题。 您只想这样做, inputFile = new Scanner(新文件(" FortuneCookie.txt"));
还要确保此文本文件与项目位于同一目录中,否则您必须描述完整路径。 确保您了解范围分辨率,因为这可能会导致各种问题。
我希望这有用!
祝你好运!