Java增量问题

标签 java sum syntax-error

在下面的 while 循环中,我在“+=”下出现语法错误。我去了这里,但答案对我没有帮助。
Cumulative sum of an Array

我只是想打印从服务器流式传输的每个累积总和。

  public static void main(String[] args) {

    try
    {
        //Create client socket, connect to server
        Socket clientSocket = new Socket("localhost",9999);
        //create output stream attached to socket
        PrintStream outToServer = new PrintStream(clientSocket.getOutputStream());

        System.out.print("Command : ");
        //create input stream
        InputStreamReader inFromUser = new InputStreamReader(System.in);
        BufferedReader ed = new BufferedReader(inFromUser);

        String temp = ed.readLine();

        outToServer.println(temp);
        //create input stream attached to socket
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        String display=null;
        while((display = inFromServer.readLine())!=null){
        int displayByt = Integer.valueOf(display);
        double totalByt += displayByt;//SYNTAX ERROR  "+="
          //totalByt = totalByt + displayByt; Does not Work either
        System.out.print(totalByt);
        System.out.print("\n");
        }
        clientSocket.close();
    }

最佳答案

您应该在循环之前定义并初始化totalByt,并且仅在循环中添加它,而不是尝试重新定义它:

double totalByt = 0.0; // Defined and initialized here
while ((display = inFromServer.readLine()) != null) {
    int displayByt = Integer.valueOf(display);
    totalByt += displayByt; // Used here
}

关于Java增量问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39926394/

相关文章:

javascript - WEB 上的 Apple Pay 集成 - 设置沙盒环境

java - Java 逐行加载图像

java - 用于 Java 开发的 Linux 发行版

java - 添加第二列与第一列中的重复元素

sql - 同一个表的不同列的计数总和

Java-错误 : Could not find or load main class

iphone - 为什么这个 NSNumber 总和返回一个奇怪的结果?

java错误代码。请检查(java)

syntax-error - 汽车 : + is not a list, 口齿不清错误?

javascript - Aptana Studio 3 语法错误意外标记