这个是读取和java文件同目录下的数据,输入错误就直接退出,正确就进入,但是输入命令控制时就无限循环了不能传附件,这个是数据,保存为1.dat就行,运行时输入名字也是这个,取其他名字也行
Mnementh
39
Benden
bronze
1000
2000
200
import java.util.*;
import java.io.*;
public class Dragon2
{
    public static void main(String[] args) throws IOException
{
System.out.print("Enter file name >> ");
String name;
int wingspan;
String weyr;
String colour;
int y2;
int x2;
int famle;
String input;
int y1;
int x1;
Scanner sc = new Scanner(System.in);
File file = new File(sc.nextLine());

if(file.exists())
{
Scanner file1 = new Scanner(file);
name = file1.nextLine();
wingspan = file1.nextInt();
file1.nextLine();
weyr = file1.nextLine();
colour = file1.nextLine();
y2 = file1.nextInt();
file1.nextLine();
x2 = file1.nextInt();
file1.nextLine();
int flame = file1.nextInt();
file1.nextLine();
file1.close();


Random random1 = new Random();
y1 = (int)random1.nextInt(2001)+1000;
Random random2 = new Random();
x1 = (int)random2.nextInt(4001);
System.out.println("Thread position");
System.out.println("x = " + x1 + " y = " + y1);
System.out.println("Dragon position");
System.out.println("x = " + x2 + " y = " + y2);
System.out.println(" "); //space
System.out.println("    Menu    ");
    System.out.println("left - Go left");
    System.out.println("right - Go right");
    System.out.println("up - Go up");
    System.out.println("down - Go down");
    System.out.println("flame on - breathe fire");
    System.out.println("between - go between");
    System.out.println("quit");
    System.out.print("Enter choice >> ");
input = sc.nextLine().toUpperCase();
Random random3 = new Random();
int number = (int)random3.nextInt(17);

do
{
    if(input == "LEFT")
{
x2 = x2 - ((100/wingspan)*50);

if(number == 0 || number == 3 || number == 11)
{
x1 += 100;
}
else if(number == 5 || number == 9 || number == 14)
{
x1 = x1 - 100;
}
else
{
x1 = x1;
y1 = y1 - 100;
}
            } 

else if(input == "RIGHT")
{
x2 = x2 +((100/wingspan)*50);

if(number == 0 || number == 3 || number == 11)
{
x1 += 100;
}
else if(number == 5 || number == 9 || number == 14)
{
x1 = x1 - 100;
}
else
{
x1 = x1;
y1 = y1 - 100;
}
}

else if(input == "UP")
{
y2 = y2 + (wingspan * 5);

if(number == 0 || number == 3 || number == 11)
{
x1 += 100;
}
else if(number == 5 || number == 9 || number == 14)
{
x1 = x1 - 100;
}
else
{
x1 = x1;
y1 = y1 - 100;
}
}

else if(input == "DOWN")
{
y2 = y2 - (wingspan * 5);
if(y2 < 0)
{
y2 =0;
}

if(number == 0 || number == 3 || number == 11)
{
x1 += 100;
}
else if(number == 5 || number == 9 || number == 14)
{
x1 = x1 - 100;
}
else
{
x1 = x1;
y1 = y1 - 100;
}
}

else if(input == "FLAMEON ON")
{
if((int)Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2)) <= flame)
{
                System.out.println("Congratulations Rider!!!");
                System.out.println("you and your "+colour+" dragon "+"\""+name+"\" have successfully destroyed Thread "+weyr+" weyr is proud of you!");
                System.exit(0);
            }
            else
            {
                 System.out.println("Have to get closer Rider");
 break;
            }
}

else if(input == "BETWEEN")
{
if(number == 0 || number == 3 || number == 11)
{
x1 += 100;
}
else if(number == 5 || number == 9 || number == 14)
{
x1 = x1 - 100;
}
else
{
x1 = x1;
y1 = y1 - 100;
}
}

else if(input == "QUIT")
{

            
            System.out.println("Not a valid choice");
            

if(y1 > 0)
{
System.out.println("Thread position");
        System.out.println("x = " + x1 + " y = " + y1);
System.out.println("Dragon position");
System.out.println("x = " + x2 + " y = " + y2);
System.out.println(" "); //space
System.out.println("    Menu    ");
System.out.println("left - Go left");
System.out.println("right - Go right");
System.out.println("up - Go up");
System.out.println("down - Go down");
            System.out.println("flame on - breathe fire");
            System.out.println("between - go between");
            System.out.println("quit");
            System.out.print("Enter choice >> ");
        input = sc.nextLine().toUpperCase();
}
else
{
    System.out.println("Try harder next time Rider!!");
                System.out.println("you and your "+colour+" dragon "+"\""+name+"\" allowed Thread to reach the ground "+weyr+" weyr and the people it protects is depending on you");
                break;
            }
    
    }
}while(input != "QUIT");
}
else
{
System.out.println("File is wrong");
}
}
}