简体   繁体   English

方法中的增量计数器不适用于Java

[英]The increment counter in method not working for java

I am developing two separate program which involve method call from each side.Program A is MyNoteCenter.java and program B is SocketServer.java. 我正在开发两个单独的程序,每个程序都涉及方法调用。程序A是MyNoteCenter.java,程序B是SocketServer.java。 There consist of a method call in MyNoteCenter to trigger the method in the SocketServer for download the resource, so the counter in SocketServer will increment by 1. when I click the button download inside the MyNoteCenter, it will contact the SocketServer for download request and increment the counter by one if SocketServer receive valid argument but why my counter only rise one time? MyNoteCenter中包含一个方法调用,以触发SocketServer中的方法来下载资源,因此SocketServer中的计数器将增加1。当我单击MyNoteCenter中的下载按钮时,它将与SocketServer联系以请求下载并增加如果SocketServer接收到有效的参数,计数器将加一,但是为什么我的计数器只会加一? it will only function well for the first time I click the download button but when I click the second time, the counter still showing 1 它只会在我第一次单击下载按钮时才能正常运行,但是在我第二次单击时,计数器仍显示1

This is some portion of my SocketServer program 这是我的SocketServer程序的一部分

 public String getDownload()
{
    int c = 0;
    c = c + 1;
    switch(software)
    {

        case "1" :
            message = "ITune";
        //    counter++;
            break;

        case "2" :
            message = "ZoneAlarm";
        //  counter++;
            break;

        case "3" :
            message = "Winrar";
        //  counter++;
            break;

        case "4" :
            message = "Audacity";
        //  counter++;
            break;

    }

    JOptionPane.showMessageDialog(null,"Your download is\n" +message+ "\n the number of download is\n"+c);
    return message;

}

This is the method in MyNoteCenter, the method will be trigger after the btn2 is click which is download button, the runCC method will contact the SocketServer method for download 这是MyNoteCenter中的方法,单击btn2即下载按钮后将触发该方法,runCC方法将与SocketServer方法联系以进行下载。

 public static void runCC(String software,String id,String name,String job,String country)
       {
         SocketServer dc = new SocketServer(software,id,name,job,country);
         String ServerReplyMessage = dc.getDownload();
         JOptionPane.showMessageDialog(null,"Downloading :\n" +ServerReplyMessage);
         int answer = JOptionPane.showConfirmDialog(null, "Do you want to continue?", "Confirm",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (answer == JOptionPane.NO_OPTION)
        {
            JOptionPane.showMessageDialog(null,"Please click close button");
        }
       else
       {
           JOptionPane.showMessageDialog(null,"Please proceed");
       }

http://codepad.org/ >>my full SocketServer program http://codepad.org/ >>我完整的SocketServer程序

You are re initializing c every time you run the method. 您每次运行该方法时都要重新初始化c

c should be a field defined within the class that will maintain value every time you need to increment it OR evaluate it's current value. c应该是在类中定义的字段,该字段将在每次需要增加值或评估其当前值时都将保持值。

 public class MyClass {

    private int c = 0;


    public String getDownload() {
       c++;

       switch case...
    }

 }

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

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