gpt4 book ai didi

JAVA 客户端服务器读取带有新行的字符串

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:51:59 25 4
gpt4 key购买 nike

我正在编写一个带有客户端和服务器的小程序。服务器程序中包含计算器程序。当服务器和客户端运行时,客户端可以输入和等式,例如。 2 + 2 和服务器将计算它并将答案发送给客户端。该程序还应该包含一个帮助选项,因此当命令帮助从客户端发送到服务器时,服务器应该返回一个帮助菜单。我的问题是它正在返回帮助菜单,但所有内容都在一行中,而不是在单独的行中将其打印到控制台。感谢您的帮助。

服务器代码:

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();

}

}

最佳答案

您必须在客户端的 readline 方法周围添加一个 while 循环:

String answer = null;
while((answer=br.readLine()) != null)
{
//print answer
}

最好使用常量而不是硬编码:System.getProperty("line.separator");

关于JAVA 客户端服务器读取带有新行的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13365659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com