java.util.*;
import java.io.*;
public class Sites
{
String country2, city2;
int days2, count = 0, time = 10;
double rate2, scost = 0;
public Sites(String province, String city, int days, double rate)
{
country2 = province;
city2 = city;
days2 = days;
rate2 = rate;
}
public void Sites()
{
{
Scanner scan = new Scanner(System.in);
try
{
while(count == 0)
{
BufferedReader file2 = new BufferedReader(new FileReader ("Sites.txt"));
while(file2.ready())
{
String cityS = file2.readLine();
String site1 = file2.readLine();
String cost1 = file2.readLine();
double cost1a = Double.parseDouble(cost1);
double tcost1 = cost1a * rate2;
String site2 = file2.readLine();
String cost2 = file2.readLine();
double cost2a = Double.parseDouble(cost2);
double tcost2 = cost2a * rate2;
String site3 = file2.readLine();
String cost3 = file2.readLine();
double cost3a = Double.parseDouble(cost3);
double tcost3 = cost3a * rate2;
String wiggle = file2.readLine();
if (cityS.equalsIgnoreCase(city2))
{
System.out.println("Province/Territory: " + country2 + "Number of Days Left: " + days2);
System.out.println("City: " + city2 + "Time: " + time + ":00");
System.out.println("");
System.out.println("Where would you like to go?");
System.out.println("1)" + site1);
System.out.println("Cost) " +cost1 );
System.out.println("");
System.out.println("2)" + site2);
System.out.println("Cost) " +cost2 );
System.out.println("");
System.out.println("3)" + site3);
System.out.println("Cost) " +cost3 );
System.out.println("");
System.out.println("4)Eat");
System.out.println("Cost) 25 ");
System.out.println("");
System.out.println("5)Rest");
System.out.println("Cost) 0 ");
System.out.println("");
int answer = scan.nextInt();
ArrayList<String> array = new ArrayList<String>();
Eat food = new Eat(answer,city2,rate2,array);
Descriptions sum = new Descriptions(site1,site2,site3,answer);
switch(answer)
{
case 1: time = time + 2;
scost = scost + tcost1;
sum.Description();
array.add(site1);
System.out.println("");
break;
case 2: time = time + 2;
scost = scost + tcost2;
sum.Description();
array.add(site2);
System.out.println("");
break;
case 3: time = time + 2;
scost = scost + tcost3;
sum.Description();
array.add(site3);
System.out.println("");
break;
case 4: time = time + 2;
scost = scost + (rate2*25);
food.Eat();
break;
case 5: time = time + 2;
System.out.println("You rested for a couple of hours.");
break;
}
if(time == 22)
{
days2 = days2 - 1;
time = 10;
}
if(days2 == 0)
{
count++;
for (int i = 0; i < array.size(); i++)
{
System.out.println("Thank you for using the tourist simulator. You spent " + scost + " and visited " +array.get(i));
}
}
}
}
}
}
catch (IOException e)
{
System.out.println(e);
}
}
}
}
答案 0 :(得分:2)
您将在循环的每次迭代中重新声明ArrayList。
尝试在您的Scanner声明上方声明您的ArrayList。
<强> e.g。强>
移动
ArrayList<String> array = new ArrayList<String>();
就在
之上Scanner scan = new Scanner(System.in);