Java增量问题

Java增量问题,java,sum,syntax-error,Java,Sum,Syntax Error,在下面的while循环中,我在“+=”下得到一个语法错误。我去了,但答案对我没有帮助。 我只是想打印从服务器传输的每个累计金额 public static void main(String[] args) { try { //Create client socket, connect to server Socket clientSocket = new Socket("localhost",9999); //create

在下面的while循环中,我在“+=”下得到一个语法错误。我去了,但答案对我没有帮助。

我只是想打印从服务器传输的每个累计金额

  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的声明初始化移动到循环之前。在循环中增加它。并使用
System.out.println
显示,而不是两次调用
print
。像

double totalByt = 0; // <-- declare and set to 0.
while((display = inFromServer.readLine())!=null){
    int displayByt = Integer.valueOf(display);
    totalByt += displayByt;
    System.out.println(totalByt);
}

double totalByt=0;// 您应该在循环之前定义和初始化
totalByt
,并且只在循环中添加到它,而不尝试重新定义它:

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

我认为这里唯一的问题是
totalByt
变量需要一个初始值。基本上,按照编写代码的方式,您正在尝试将整数添加到零

试试这个:

String display=null;
double totalByt = 0;
while((display = inFromServer.readLine())!=null){

    int displayByt = Integer.valueOf(display);
    totalByt += displayByt;

    System.out.print(totalByt);
    System.out.print("\n");
}

初始化totalByt。这应该可以解决问题。

double totalByt+=displayByt毫无意义,因为
double totalByt=double totalByt+displayByt毫无意义。
totalByt
需要初始值吗?@justin谢谢you@n00bie1221当我输入完整的回答时,我看到有几个人回答了。但你现在有了一些例子:-)谢谢,我知道这很简单,我总是忘记