import java.util.*;
import java.util.*;
public class Dragon2
{
    public static void main(String[] args) throws IOException
{
File fileToOpen = new File();
Scanner sc = new Scanner(System.in);
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;
File file = new File(kb.nextLine());
if(file.exists())
{
Scanner file1 = new Scanner(file);
String name = file1.nextLine();
int wingspan = file1.nextInt();
file1.nextLine();
String weyr = file1.nextLine();
String colour = file1.nextLine();
int y2 = file1.nextInt();
file1.nextLine();
int x2 = file1.nextInt();
file1.nextLine();
int flame = file1.nextInt();
file1.nextLine();
file1.close();


Random random1 = new Random();
int y1 = (int)random1.nextInt(2001)+1000;
Random random2 = new Random();
int 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 >> ");
char input = sc.nextLine().toUpperCase();

do
{

    switch(input)
{
    case 'String input = LEFT':
x2 = x2 - ((100/wingspan)*50);
Random random3 = new Random();
number = (int)random3.nextInt(17);
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;
}
break;

case 'String input = RIGHT':
x2 = x2 +((100/wingspan)*50);
Random random3 = new Random();
number = (int)random3.nextInt(17);
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;
}
break;

case 'String input = UP':
y2 = y2 + (wingspan * 5);
Random random3 = new Random();
number = (int)random3.nextInt(17);
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;
}
break;

case 'String input = DOWN':
y2 = y2 - (wingspan * 5);
if(y2 < 0)
{
y2 =0;
}
Random random3 = new Random();
number = (int)random3.nextInt(17);
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;
}
break;
case 'String input = FLAME 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;
            }

case 'String input = BETWEEN':
Random random3 = new Random();
number = (int)random3.nextInt(17);
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;
}
break;

case 'String input = QUIT':
continue;
            default:
            System.out.println("Not a valid choice");
            break;

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 >> ");
        char 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");
}
}
}

谢谢各位了!!!

解决方案 »

  1.   

    如果刚学java不建议看那么长的代码,这容易丧失信心。尤其是这种长长的,一大堆for和if的另外,你好歹说一下这段代码打算要干嘛
      

  2.   

    编译都没通过吧,
    第一点:import java.io.IOException;这个包没有导入;
    第二点:case 里面只能写常量表达式【包括字符常量,数字】,而'String input = QUIT'这个不是字符常量【也就是单引号里面加一个字符的形式如:‘a’】
    修正方法:1、导入import java.io.IOException;
               2、把代码中CASE后面的形如'String input = QUIT'改成字符常量  
      

  3.   

    昨晚太晚了,就没详细说明,其实我就想问下,switch中,case只能写常量的问题,我必须在里面写LIFT怎么办?我最近刚学了enum,用这个可以吗?
      

  4.   

    1 你的最基本语法就是错的,字符串不用单引号
    2 非得用字也可以,换成jdk7,它支持case 字符型
      

  5.   

    实现功能就行了,何必这么纠结,不行就别用switch case直接用if else算了
      

  6.   


    这也叫长??我见过一个case里面 500+行啊
      

  7.   

    package a.test;
    import java.io.File;
    import java.io.IOException;
    import java.util.Random;
    import java.util.Scanner;
    public class Dragon2
    {
        public static void main(String[] args) throws IOException
    {
    File fileToOpen = new File("filename");  //这里需要你file的路径
    Scanner sc = new Scanner(System.in);
    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;
    File file = new File("");   //kb 没有定义
    if(file.exists())
    {
    Scanner file1 = new Scanner(file);
    /*String*/ name = file1.nextLine();  //name 已经定义过了,这里不能再定义
    /*int*/ wingspan = file1.nextInt();  //同上,已经定义过了
    file1.nextLine();
     weyr = file1.nextLine();  //ts
     colour = file1.nextLine();  //ts
     y2 = file1.nextInt(); //ts
    file1.nextLine();
     x2 = file1.nextInt(); //ts
    file1.nextLine();
    int flame = file1.nextInt();
    file1.nextLine();
    file1.close();


    Random random1 = new Random();
     y1 = (int)random1.nextInt(2001)+1000; //ts
    Random random2 = new Random();
     x1 = (int)random2.nextInt(4001);  //ts
    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();  //ts

        
        char firshChar=input.charAt(0);  //java 中String不能用做case
    do
    {

        switch(firshChar)
    {
        case 'A':
    x2 = x2 - ((100/wingspan)*50);
    Random random3 = new Random();
    int number = (int)random3.nextInt(17);   //没有定义变量
    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;
    }
    break;

    case 'B':
    x2 = x2 +((100/wingspan)*50);
     random3 = new Random();  //定义过了,又重新定义了
    number = (int)random3.nextInt(17);
    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;
    }
    break;

    case 'C':
    y2 = y2 + (wingspan * 5);
     random3 = new Random();   //ts
    number = (int)random3.nextInt(17);
    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;
    }
    break;

    case 'D':
    y2 = y2 - (wingspan * 5);
    if(y2 < 0)
    {
    y2 =0;
    }
     random3 = new Random();  //ts
    number = (int)random3.nextInt(17);
    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;
    }
    break;
    case 'F':

    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;
                }

    case 'G':
     random3 = new Random();  //ts
    number = (int)random3.nextInt(17);
    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;
    }
    break;

    case 'H':
    continue;
                default:
                System.out.println("Not a valid choice");
                break;
        }
    }while(firshChar != 'Q');
    }
    else
    {
    System.out.println("File is wrong");
    }
    }
    }

    终于该完了,有语法错误的地方大部分给你改正并注释了,但整个程序能不能编译通过我没有试,感觉哥们以前是写脚本的吧,程序的逻辑思维还是有的,但代码写的太随心所欲啦
      

  8.   

    package a.test;
    import java.io.File;
    import java.io.IOException;
    import java.util.Random;
    import java.util.Scanner;
    public class Dragon2
    {
        public static void main(String[] args) throws IOException
    {
    File fileToOpen = new File("filename");  //这里需要你file的路径
    Scanner sc = new Scanner(System.in);
    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;
    File file = new File("");   //kb 没有定义
    if(file.exists())
    {
    Scanner file1 = new Scanner(file);
    /*String*/ name = file1.nextLine();  //name 已经定义过了,这里不能再定义
    /*int*/ wingspan = file1.nextInt();  //同上,已经定义过了
    file1.nextLine();
     weyr = file1.nextLine();  //ts
     colour = file1.nextLine();  //ts
     y2 = file1.nextInt(); //ts
    file1.nextLine();
     x2 = file1.nextInt(); //ts
    file1.nextLine();
    int flame = file1.nextInt();
    file1.nextLine();
    file1.close();


    Random random1 = new Random();
     y1 = (int)random1.nextInt(2001)+1000; //ts
    Random random2 = new Random();
     x1 = (int)random2.nextInt(4001);  //ts
    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();  //ts

        
        char firshChar=input.charAt(0);  //java 中String不能用做case
    do
    {

        switch(firshChar)
    {
        case 'A':
    x2 = x2 - ((100/wingspan)*50);
    Random random3 = new Random();
    int number = (int)random3.nextInt(17);   //没有定义变量
    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;
    }
    break;

    case 'B':
    x2 = x2 +((100/wingspan)*50);
     random3 = new Random();  //定义过了,又重新定义了
    number = (int)random3.nextInt(17);
    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;
    }
    break;

    case 'C':
    y2 = y2 + (wingspan * 5);
     random3 = new Random();   //ts
    number = (int)random3.nextInt(17);
    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;
    }
    break;

    case 'D':
    y2 = y2 - (wingspan * 5);
    if(y2 < 0)
    {
    y2 =0;
    }
     random3 = new Random();  //ts
    number = (int)random3.nextInt(17);
    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;
    }
    break;
    case 'F':

    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;
                }

    case 'G':
     random3 = new Random();  //ts
    number = (int)random3.nextInt(17);
    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;
    }
    break;

    case 'H':
    continue;
                default:
                System.out.println("Not a valid choice");
                break;
        }
    }while(firshChar != 'Q');
    }
    else
    {
    System.out.println("File is wrong");
    }
    }
    }

    终于该完了,有语法错误的地方大部分给你改正并注释了,但整个程序能不能编译通过我没有试,感觉哥们以前是写脚本的吧,程序的逻辑思维还是有的,但代码写的太随心所欲啦
    什么呀额,我是新手,第一年学java,it专业的,在国外谢谢了