为什么我的程序在遇到第一条错误消息后无法读取文本文件?

时间:2014-04-03 19:15:59

标签: java file loops text

import java.util.*; // needed for scanner
import java.io.*; // needed to read text file

public class MainC
{
        public static void main(String[] args) 
        {
        Scanner scan = new Scanner(System.in); // scanner is established

        System.out.println("Welcome to the Tourist Site Simulator. In this program, we will allow you to simulate a European experience by traveling to a country in Europe"
            + "and staying there for a set number of days. "); // general explanation

        int count = 0, time = 10; // for possible repetition of instructions

 try {
        BufferedReader file = new BufferedReader(new FileReader("Countries.txt"));

    while(count == 0){

        System.out.println("First of all, which of the fifty sovereign states of Europe would you like to visit?");
        String country = scan.next();

        while (file.ready()){

            String countryF = file.readLine();
            String cityF1 = file.readLine();
            String cityF2 = file.readLine();
            String cityF3 = file.readLine();
            String squiq = file.readLine();

            if(countryF.equalsIgnoreCase(country)){

            count++;
            int count2 = 0;
            while(count2 == 0){

                country = country.substring(0,1).toUpperCase() + country.substring(1);
                System.out.println("Country: " + country);
                System.out.println("");
                System.out.println("The largest cities at this nation are " + cityF1 + ", " + cityF2 + ", " +cityF3 + ". Which one would you like to visit?");
                String city = scan.next();

                if(cityF1.equalsIgnoreCase(city)||cityF2.equalsIgnoreCase(city)||cityF3.equalsIgnoreCase(city)){
                    count2++;
                    city = city.substring(0,1).toUpperCase() + city.substring(1);
                    System.out.println("");
                    System.out.println("Country: " + country);
                    System.out.println("City: " + city);

                    System.out.println("");

                    System.out.println("How many days will you be staying?");
                    int days = scan.nextInt();

                    System.out.println("");
                    System.out.println("Country: " + country + "                            Number of Days Left: " + days);
                    System.out.println("City: " + city);

                    System.out.println("");

                    System.out.println("What is the your rate of currency exchange to one Euro?");
                    double rate = scan.nextDouble();
                }else{
                    System.out.println("I must regretfully inform you that this city is not in our listed database. Please try again.");
                    System.out.println("");
                }
            }//while count == 2 
        }//while file.ready()
    }//while count == 0
        if(count == 0)
        {
        System.out.println("I must regretfully inform you that this country in not in our listed database. Please try again.");
        System.out.println("");
        }
    }

}
catch (IOException e)
{
        System.out.println(e);
}
    }

}

你好。我遇到了这段代码的问题。应该做的是,如果用户输入了错误的国家/地区,将启动循环并再次向用户询问该国家/地区。我的问题是,在出现此错误消息并再次向用户询问该国家之后,无论输入是否正确,它都会重复询问该问题。谢谢。

1 个答案:

答案 0 :(得分:0)

我想这可能是因为你有

if(countryF.equalsIgnoreCase(country))

然后从我所看到的是之后的if语句是

if(count == 0)

因此,如果错误的国家,你的计数永远不会增加。  试试这个

if(countryF.equalsIgnoreCase(country){

}else(instead of if count == 0){
      "print wrong country try again"
}

你真的不需要这个

while(count ==0)

尝试在else语句中添加第二个扫描捕获器。

if(countryF.equalsIgnoreCase(country){

}else(instead of if count == 0){
      "print wrong country try again"
       country = scan.next();
}