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");
}
}
}
谢谢各位了!!!
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");
}
}
}
谢谢各位了!!!
解决方案 »
- Eclipse和MyEclipse是啥关系?
- java串口编程,急,急,很急!
- 怎样控制一个图形界面关闭之后再进行后续的操作?
- 如何在jlist中加入面板?
- 请懂得java和c#两门语言的朋友进来看一下.
- java.net.SocketException: Connection reset 帮忙看下什么原因
- 紧急求助,有分,在线等,过了今天就没分了哈
- 正则表达式大比拼
- 一个简单的问题!!!
- 关于JNI的问题....看到此贴者必须回贴(水贴可矣),违者斩立决...
- 为方便程序员编程,推出“java常用代码一(粘贴即用)"系列,不断更新高质量常用代码段
- apache mina发送数据后session立即就关闭了
第一点:import java.io.IOException;这个包没有导入;
第二点:case 里面只能写常量表达式【包括字符常量,数字】,而'String input = QUIT'这个不是字符常量【也就是单引号里面加一个字符的形式如:‘a’】
修正方法:1、导入import java.io.IOException;
2、把代码中CASE后面的形如'String input = QUIT'改成字符常量
2 非得用字也可以,换成jdk7,它支持case 字符型
这也叫长??我见过一个case里面 500+行啊
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");
}
}
}
终于该完了,有语法错误的地方大部分给你改正并注释了,但整个程序能不能编译通过我没有试,感觉哥们以前是写脚本的吧,程序的逻辑思维还是有的,但代码写的太随心所欲啦
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专业的,在国外谢谢了