十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Server.java
创新互联公司主营天峨网站建设的网络公司,主营网站建设方案,app开发定制,天峨h5微信平台小程序开发搭建,天峨网站营销推广欢迎天峨等地区企业咨询
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Server {
private ListServerThread serverThreads;
private ServerSocket serverSocket;
public Server() throws IOException {
serverSocket = new ServerSocket(8025);
serverThreads = new ArrayListServerThread();
}
public ListServerThread getServerThreads() {
return serverThreads;
}
public void setServerThreads(ListServerThread serverThreads) {
this.serverThreads = serverThreads;
}
public ServerSocket getServerSocket() {
return serverSocket;
}
public void setServerSocket(ServerSocket serverSocket) {
this.serverSocket = serverSocket;
}
public static void main(String[] args) {
Server server = null;
try {
server = new Server();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
try {
Socket socket;
Thread t;
while (true) {
socket = server.getServerSocket().accept();
ServerThread serverThread = new ServerThread(server, socket);
server.getServerThreads().add(serverThread);
t = new Thread(serverThread);
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendMessage(String message, Socket socket) {
for (ServerThread serverThread : serverThreads) {
try {
serverThread.getDos().writeUTF(message);
} catch (IOException e) {
// e.printStackTrace();
System.out.println("推送消息给" + serverThread.getSocket().getRemoteSocketAddress() + "过程中发生了错误.");
}
}
}
}
class ServerThread implements Runnable {
private Server server;
private Socket socket;
private DataInputStream dis;
private DataOutputStream dos;
public ServerThread(Server server, Socket socket) throws IOException {
this.server = server;
this.socket = socket;
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
}
public Server getServer() {
return server;
}
public void setServer(Server server) {
this.server = server;
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
public DataInputStream getDis() {
return dis;
}
public void setDis(DataInputStream dis) {
this.dis = dis;
}
public DataOutputStream getDos() {
return dos;
}
public void setDos(DataOutputStream dos) {
this.dos = dos;
}
public void run() {
String message = "";
while (!message.equals("logout")) {
try {
message = socket.getRemoteSocketAddress() + " say: " + dis.readUTF();
// System.out.println(message);
server.sendMessage(message, socket);
} catch (IOException e) {
//e.printStackTrace();
break;
}
}
server.getServerThreads().remove(this);
try {
dis.close();
dos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
private Socket socket;
private DataOutputStream dos;
private DataInputStream dis;
public Client() throws UnknownHostException, IOException {
socket = new Socket("127.0.0.1", 8025);
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
new Thread(new ClientThread(this)).start();
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
public DataOutputStream getDos() {
return dos;
}
public void setDos(DataOutputStream dos) {
this.dos = dos;
}
public DataInputStream getDis() {
return dis;
}
public void setDis(DataInputStream dis) {
this.dis = dis;
}
public void close() {
try {
dis.close();
dos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void sendMessage(String message) {
try {
dos.writeUTF(message);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Client client = null;
try {
client = new Client();
System.out.println(client.getSocket().getLocalPort());
} catch (IOException e) {
System.out.println("BUG");
e.printStackTrace();
System.exit(-1);
}
String message = "";
Scanner scanner = new Scanner(System.in);
while (!message.equals("logout")) {
message = scanner.nextLine();
// System.out.println(message);
client.sendMessage(message);
}
scanner.close();
client.close();
}
}
class ClientThread implements Runnable {
private Client client;
public ClientThread(Client client) {
super();
this.client = client;
}
@Override
public void run() {
try {
while (true) {
System.out.println(client.getDis().readUTF());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class WalkDistance {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for (int i = 0; i n; i++) {
String order = in.next();
System.out.println(new DecimalFormat("0.00").format(calDistance(order)));
}
in.close();
}
private static float calDistance(String order) {
char[] orderChar = order.toCharArray();
ListString orderList = new ArrayListString();
int num = 0;
for (int i = 0, len = orderChar.length; i len; i++) {
if (orderChar[i] = '0' orderChar[i] = '9') {
num++;
} else {
if (num == 0) {
orderList.add(String.valueOf(orderChar[i]));
} else {
StringBuffer sb = new StringBuffer();
for (int j = num; j 0; j--) {
sb.append(String.valueOf(orderChar[i - j]));
}
orderList.add(sb.toString());
orderList.add(String.valueOf(orderChar[i]));
num = 0;
}
}
if (i == len - 1 num != 0) {
StringBuffer sb = new StringBuffer();
for (int j = num - 1; j = 0; j--) {
sb.append(String.valueOf(orderChar[i - j]));
}
orderList.add(sb.toString());
}
}
Point curPoint = new Point(0, 0, 90);
for (int i = 0, len = orderList.size(); i len; i++) {
if ("R".equals(orderList.get(i))) {
curPoint.angle = (curPoint.angle - 90 + 360) % 360;
} else if ("L".equals(orderList.get(i))) {
curPoint.angle = (curPoint.angle + 90) % 360;
} else {
curPoint.setLocation(curPoint, orderList.get(i));
}
}
return curPoint.getDis();
}
static class Point {
private int x;
private int y;
private int angle;
private float dis;
public Point(int x, int y, int angle) {
this.x = x;
this.y = y;
this.angle = angle;
this.dis = (float) 0;
}
public void setLocation(Point point, String distance) {
if (point.angle == 0) {
point.x += Integer.valueOf(distance);
} else if (point.angle == 90) {
point.y += Integer.valueOf(distance);
} else if (point.angle == 180) {
point.x -= Integer.valueOf(distance);
} else {
point.y -= Integer.valueOf(distance);
}
}
public float getDis() {
return (float) Math.sqrt(this.x * this.x + this.y * this.y);
}
}
}
java机器人游戏最经典的是Robocode项目,里面有详细的文档以及源码,可以自己学习修改。(不要说自己参加这个项目,自己不认真学些东西,那可以面壁了)。下面是它的项目主页
是这样的错误么?
Frist.java:5: cannot resolve symbol
symbol : class Scanner
location: class Frist
Scanner input=new Scanner(System.in);
Scanner是JDK1.5开始才有的
这样的错误是因为JDK版本不够,不支持。。。
我用的就是1.4.2 所以是这样的错误