我正在用客戶端和服務器編寫一個小程序。服務器程序包含計算器程序。當服務器和客戶端運行時,客戶端可以輸入並等於例如。 2 + 2,服務器將計算它並將答案發送給客戶端。程序也應該包含一個幫助選項,所以當命令幫助從客戶端發送到服務器時,服務器應該返回一個幫助菜單。 我的問題是,它返回的幫助菜單,但在一行中,它不是打印出來的控制檯在單獨的行。謝謝你的幫助。JAVA客戶端服務器用新行讀取字符串
服務器代碼:
package One;
import java.io.*;
import java.net.*;
public class ServerCalculator {
static double answer;
public ServerCalculator(){
System.out.println("Server...");
theServer();
}
void theServer(){
try{
ServerSocket ss = new ServerSocket(9990);
while(true){
Socket sc = ss.accept();
Help hp = new Help();
BufferedReader br = new BufferedReader(new InputStreamReader(sc.getInputStream()));
String compute = br.readLine();
Maths(compute);
PrintWriter pw = new PrintWriter(sc.getOutputStream());
if(compute.equals("help")){
// pw.println(hp.noOfLines());
pw.println(hp.menu());
}
if(compute.equals("exit")){
ss.close();
}
else{
pw.println(answer);
}
pw.flush();
}
}
catch (Exception ee){
ee.printStackTrace();
}
}
static double Maths(String compute){
String message[] = compute.split(" ");
if(message[0].equalsIgnoreCase("Help"))
{
return -2;
}
if(message[0].equalsIgnoreCase("Exit"))
{
return -1;
}
double rad = 0;
double a = Integer.parseInt(message[0]);
double b = Integer.parseInt(message[2]);
if(message[1].equalsIgnoreCase("Sin")){
rad = Math.toRadians(a);
answer = Math.sin(rad);
}
if(message[1].equalsIgnoreCase("Cos")){
if(a==90 || a==270){
answer = 0;
}
else{
rad = Math.toRadians(a);
answer = Math.cos(rad);
}
}
if(message[1].equalsIgnoreCase("Tan")){
if(a==90 || a==270){
System.out.println("Invalid Calculation");
answer = 0;
}
else if(a==45 || a==135){
rad = Math.toRadians(a);
answer = Math.tan(rad);
}
rad = Math.toRadians(a);
answer = Math.tan(rad);
}
if(message[1].equalsIgnoreCase("Power") || (message[1].equalsIgnoreCase("^")))
{
answer = Math.pow(a, b);
}
if(message[1].equalsIgnoreCase("Multiply") || (message[1].equalsIgnoreCase("*")))
{
answer = a*b;
}
if(message[1].equalsIgnoreCase("Add") || (message[1].equalsIgnoreCase("+")))
{
answer = a+b;
}
if(message[1].equalsIgnoreCase("Subtract") || (message[1].equalsIgnoreCase("-")))
{
answer = a-b;
}
if(message[1].equalsIgnoreCase("Divide") || (message[1].equalsIgnoreCase("/")))
{
answer = a/b;
}
return answer;
}
}
客戶端代碼:
package One;
import java.net.*;
import java.util.Scanner;
import java.io.*;
public class ClientCalculator {
public static void main(String[] args) {
Socket sc;
System.out.println("Client...");
while(true){
try
{
Scanner sin = new Scanner(System.in);
String question = sin.nextLine();
System.out.println("Processing: " + question);
sc = new Socket("localhost",9990);
PrintWriter pw = new PrintWriter(sc.getOutputStream());
pw.println(question);
pw.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(sc.getInputStream()));
String answer = br.readLine();
System.out.println(question + " = " + answer);
// sc.close();
// sin.close();
}
catch(Exception ee){
}
}
}
}
幫助菜單:
package One;
public class Help {
String menu(){
String helpMenu =
"Instructions for the calculator " +
"Input the number followed by space and then by word or operator and by number to get result " +
"e.g. 5 + 5. or 30 Sin 30 - where 30 is the angle. " +
"Options are " +
"Multiply or (*) " +
"Add or (+) " +
"Subtract or (-) " +
"Divide or (/) " +
"Sin, Cos, Tan ";
return helpMenu;
}
// String noOfLines() {
//
// return "9";
// }
}
運行代碼:
package One;
public class TestClass {
public static void main(String[] args) {
new ServerCalculator();
}
}
這適用於在控制檯上打印菜單,但它只會在一次和以後工作,我無法發送任何其他命令到服務器。 – Saint
您沒有關閉服務器上的套接字。 –
如果關閉套接字ss我會關閉服務器。 – Saint