繁体   English   中英

Java Server基本计算器

[英]Java Server Basic Calculator

在此程序中,我的服务器从客户端接收一个命令,后跟1或2个操作数,并返回操作结果。

如果有人可以指出正确的方向,我将无法扫描客户端输入的行并在switch语句中执行实际操作,我将不胜感激。

这是代码:

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

// Takes in a mathematical operation and the operands from a client and returns the result
// Valid operations are add, sub, multiply, power, divide, remainder, square 
public class MathServer 
{ 
    public static void main(String [] args) throws IOException 
    { 
        ServerSocket yourSock = new ServerSocket(50000); //put server online
        while(true)  
        { 
            System.out.println("Waiting to accept connection");
            Socket clientSock = yourSock.accept();  //open server to connections
            System.out.println("Connection accepted");
            process(clientSock);                    //process accepted connection
            System.out.println("Connection closed");
        } 
    }  

    //BufferedReader(Reader r)
    static void process(Socket sock) throws IOException 
    {  
        InputStream in = sock.getInputStream(); 
        BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
        OutputStream out = sock.getOutputStream(); 
        PrintWriter pw = new PrintWriter(out, true);       

        String input = br.readLine(); //get user input from client         

        while(input != null && !input.equals("bye")) //check for input, if bye exit connection
        {              
            int answer = operate(input); //perform desired operation on user input
            pw.println(answer);          //print out result
            input = br.readLine();       //get next line of input 
        } 
        sock.close(); 
    }  

    //Talk to the client          
    static int operate(String s) 
    { 
        System.out.println(s); //check if same as client input

        Scanner myScanner = new Scanner(s); 
        String opType = myScanner.next();   //gets desired operation

        System.out.println(opType); //checks for correct operation 

        switch (opType) { 
            case "add": 
                return (myScanner.nextInt() + myScanner.nextInt());
            case "sub":
                return (myScanner.nextInt() - myScanner.nextInt());
            case "multiply":
                return (myScanner.nextInt() * myScanner.nextInt());
            case "power":
                return (int) Math.pow(myScanner.nextInt(), myScanner.nextInt());
            case "divide":
                return myScanner.nextInt() / myScanner.nextInt();
            case "remainder":
                return myScanner.nextInt() % myScanner.nextInt();
            case "square":
                return (int) Math.pow(myScanner.nextInt(), 2);
            default:
                return (int) Math.pow(myScanner.nextInt(), 3);
        }        
    }
} 

在服务器中使用BufferedReade.readLine()进行读取时,请确保从客户端发送换行符(常见错误)。 另外,您可能需要从客户端刷新OutputStream 由于您的Scanner读取变量的方式,您需要在一行中从客户端发送值,例如

add 100 200

switch(opType)不适用于字符串。

用类似的东西检查

if(opType.equals("add")){ //do Add }
else if(opType.equals("sub")){ //do subtraction } 

等等

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM