IF条件被跳过

时间:2015-03-22 19:54:44

标签: java if-statement

该代码基本上实现了Bully Election算法。代码运行正常,但只在第一次运行。第二步,它会跳过if函数中的notice语句。我知道代码很长,但如果我只发布if条件,我就不会理解你会理解。

import java.util.Scanner;

public class ElectionBully { `

static int nop, iap, cood;
//static Scanner pri = new Scanner(System.in);
public static void main(String pribhat[]) throws InterruptedException{
    Scanner priMain = new Scanner(System.in);
    System.out.println("Enter number of processes: ");
    nop = priMain.nextInt();
    cood = nop;
    System.out.println("Process "+cood+" is the co-ordinator");
    menu();
}

public static void menu() throws InterruptedException{
    Scanner priMenu = new Scanner(System.in);

    System.out.println("Menu:");
    System.out.println("1. Inactivate a Process. ");
    System.out.println("2. Activate a Process. ");
    System.out.println("3. Exit. ");
    int choice, noti;
    choice = priMenu.nextInt();

    switch(choice){

        case 1: System.out.println("Enter the process to be deactivated: ");
                choice = priMenu.nextInt();
                System.out.println("Process "+choice+" has been successfully deactivated!");
                iap = choice;
                if(choice == cood){
                    System.out.println("Enter the process which detects co-ordinator failure: ");
                    noti = priMenu.nextInt();
                    runBullyRun(noti, 1);
                }
                else
                System.out.println("No change in co-ordinator!");
                break;

        case 2: System.out.println("Enter the process to be activated: ");
                choice = priMenu.nextInt();
                System.out.println("Process "+choice+" has been successfully activated!");

                if(iap == choice)
                    iap=0;

                if(choice > cood) {
                System.out.println("Enter the process which detects co-ordinator failure: ");
                noti = priMenu.nextInt();
                runBullyRun(noti, 2);
                }

                break;

        case 3: System.out.println("Buh-Bye! Have a Good day!");
                break;

       default: System.out.println("WRONG CHOiCE! Let's try this again...");
                menu();
                break;
    }

}

static void runBullyRun(int node, int mode) throws InterruptedException{
    Scanner priRBR = new Scanner(System.in);

    if(mode == 1){
        System.out.println("Process "+node+" detected failure of Co-ordinator"+cood);
        System.out.println("Process "+node+" starts sending election notice...");
    }


    if(mode == 2 && node>cood){
        System.out.println("Since Process "+node+" has greate priority than current co-ordinator...");
        System.out.println("Process "+node+" starts sending election notice...");
    }


    notice(node);
    System.out.println("Do you want to continue? (y/n) ");
        String lul;
        lul = priRBR.nextLine();
        if(lul.equals("y"))
            menu();
        else
            System.out.println("Buh-Bye! Have a good day!");
}


public static void notice(int sender) throws InterruptedException{

    int i, j=0;
    if(nop == iap){
        for(i = sender; i <= nop; i++){

            if(isActive(i)){
                for(j = i; j <= nop; j++){

                        System.out.println("Process "+i+" sends election notice to Process "+j+"...");

                    if(isActive(j)){
                        //Thread.sleep(1000);
                        System.out.println("Response from Process "+j);

                    }

                }

                if((i+1) != iap){
                    //Thread.sleep(1000);
                    System.out.println("Process "+i+" quits since higher priority processes exist");  
                }

            }

            else{
                //Thread.sleep(5000);
                System.out.println("No response from Process "+(j-1));
                cood = (i-1);
                System.out.println("Process "+(i-1)+" is the new co-ordinator");
            }

        }


    }

}

public static void response(int receiver){

    System.out.println("Response from "+receiver);


}
static boolean isActive(int process){
    boolean result = false;

    if(process != iap)
        result = true;

    return result;
 }
}

代码如何运作?

  1. 从询问流程数量开始
  2. 您选择要停用/激活的流程
  3. 根据用户的选择发送/接收选举通知和响应。
  4. 最高效的流程是协调员。
  5. 如果用户选择继续,他可以选择激活先前不活动的进程。这将触发第3步,依此类推。
  6. 我面临的问题是步骤1-4工作正常。但是在步骤5,当用户选择继续时,步骤1-2正在运行,但是跳过了函数通知中的if条件。

    到目前为止我已尝试过:

    • if函数
    • 中执行runBullyRun语句
    • 个人扫描仪
    • 谷歌搜索
    • 的StackOverflow

    P.S。我在Java上不是那么好,如果这是重复的,那么请将我直接指向原文,以便我可以解决这个问题。我会删除这个。谢谢。

0 个答案:

没有答案