程序终止而不会出错

时间:2014-08-08 16:56:00

标签: java

每次运行程序时,它都会终止部分程序而不会留下任何错误消息。这发生在每次相同的地方,在第57-82行之间。为什么会发生这种情况,我该如何解决?

public class DutyDecider_console {
static Scanner input = new Scanner(System.in);

public static void main(String[] args) {
    String a, b, c, d, e, f, go, candidateone, candidatetwo, candidatethree, candidatefour, candidatefive, candidatesix;
    double z = Math.random();
    go =("go");

    String optionch;
    System.out.println("Welcome to Duty Decider.");
    System.out.println(" ");
    System.out.println("Please type in your duty you wish to be decided.");
    optionch = input.nextLine();
    System.out.println("You want to decide who will be " +optionch +".");
    System.out.println(" ");
    System.out.println("Please type in the possible candidates for doing the " +optionch +".(Type one name at a time and press enter after each name. ");
    System.out.println("There can be a total of six candidates. Type 'go' after the desired number of candidates have been entered.");
    a = input.nextLine();
    candidateone = a;

    if(candidateone.contains("go")){
        System.out.println(candidateone +" has been selected to " +optionch +".");
    }
    else if(!candidateone.contains("go")){
        System.out.println(candidateone +" has been confirmed.");
        System.out.println(" ");
        b =input.nextLine();
        candidatetwo = b;
        if(candidatetwo.contains("go")){
            System.out.println(candidateone +" has been selected to " +optionch +".");
        }
        else if(!candidatetwo.contains("go")){
            System.out.println(candidatetwo +" has been confirmed." );
            System.out.println(" ");
            c = input.nextLine();
            candidatethree = c;
            if(candidatethree.contains("go")){
                if(z<0.5){
                    System.out.println(candidateone +" has been selected to " +optionch +".");
                    System.out.println(" ");
                    System.out.println("Thank you for using 'Duty Decider. Have a nice day");
                }
                else if(z<1.0&&z>0.5){
                    System.out.println(candidatetwo +" has been selected to " +optionch +".");
                    System.out.println(" ");
                    System.out.println("Thank you for using 'Duty Decider. Have a nice day");
                }
            }
            else if(!candidatethree.contains("go")){
                System.out.println(candidatethree +" has been confirmed.");
                System.out.println(" ");
                //where I believe the problem begins
                d = input.nextLine();
                candidatefour = d;
                if(candidatefour.contains("go")){
                    //z by thirds, point .33
                    if(z<0.33){
                        System.out.println(candidateone +" has been selected to " +optionch +".");
                        System.out.println(" ");
                        System.out.println("Thank you for using 'Duty Decider. Have a nice day");
                    }
                    else if(z<0.66&&z>0.33){
                        System.out.println(candidatetwo +" has been selected to " +optionch +".");
                        System.out.println(" ");
                        System.out.println("Thank you for using 'Duty Decider. Have a nice day");
                    }
                    else if(z<1.00&&z>0.66){
                        System.out.println(candidatethree +" has been selected to " +optionch +".");
                        System.out.println(" ");
                        System.out.println("Thank you for using 'Duty Decider. Have a nice day");

                    }
                    else{
                        System.out.println(candidatefour +" has been confirmed.");
                        System.out.println(" ");
                        e = input.nextLine();
                        candidatefive = e;
                        //where I believe the problem ends.
                        if(candidatefive.contains("go")){
                            //z by fours, .25
                            if(z<0.25){
                                System.out.println(candidateone +" has been selected to " +optionch +".");
                                System.out.println(" ");
                                System.out.println("Thank you for using 'Duty Decider. Have a nice day");
                            }
                            else if(z<0.50&&z>0.25){
                                System.out.println(candidatetwo +" has been selected to " +optionch +".");
                                System.out.println(" ");
                                System.out.println("Thank you for using 'Duty Decider. Have a nice day");
                            }
                            else if(z<0.75&&z>0.50){
                                System.out.println(candidatethree +" has been selected to " +optionch +".");
                                System.out.println(" ");
                                System.out.println("Thank you for using 'Duty Decider. Have a nice day");
                            }
                            else if(z>0.75&&z<1.00){
                                System.out.println(candidatefour +" has been selected to " +optionch +".");
                                System.out.println(" ");
                                System.out.println("Thank you for using 'Duty Decider. Have a nice day!");
                            }
                        }
                        else if(!candidatefive.contains("go")){
                            System.out.println(candidatefive +" has been confirmed.");
                            System.out.println(" ");
                            f = input.nextLine();
                            candidatesix = f;
                            if(candidatesix.contains("go")){
                                //z divisible by 5, point twos
                                if(z<0.20){
                                    System.out.println(candidateone +" has been selected to " +optionch +".");
                                    System.out.println(" ");
                                    System.out.println("Thank you for using 'Duty Decider. Have a nice day");
                                }
                                else if(z<0.40&&z>0.20){
                                    System.out.println(candidatetwo +" has been selected to " +optionch +".");
                                    System.out.println(" ");
                                    System.out.println("Thank you for using 'Duty Decider. Have a nice day");
                                }
                                else if(z<0.60&&z>0.40){
                                    System.out.println(candidatethree +" has been selected to " +optionch +".");
                                    System.out.println(" ");
                                    System.out.println("Thank you for using 'Duty Decider. Have a nice day");
                                }
                                else if(z<0.80&&z>0.60){
                                    System.out.println(candidatefour +" has been selected to " +optionch +".");
                                    System.out.println(" ");
                                    System.out.println("Thank you for using 'Duty Decider. Have a nice day!");
                                }
                                else if(z<1.00&&z>0.80){
                                    System.out.println(candidatefive +" has been selected to " +optionch +".");
                                    System.out.println(" ");
                                    System.out.println("Thank you for using 'Duty Decider. Have a nice day!");
                                }
                            }
                            else if (!candidatesix.contains("go")){
                                System.out.println(candidatesix +"has been confirmed.");
                                System.out.println(" ");
                                //z divisible by 6, point .1666
                                if(z<0.1666){
                                    System.out.println(candidateone +" has been selected to " +optionch +".");
                                    System.out.println(" ");
                                    System.out.println("Thank you for using 'Duty Decider. Have a nice day");
                                }
                                else if(z<0.3332&&z>0.1666){
                                    System.out.println(candidatetwo +" has been selected to " +optionch +".");
                                    System.out.println(" ");
                                    System.out.println("Thank you for using 'Duty Decider. Have a nice day");
                                }
                                else if(z<0.4998&&z>0.3332){
                                    System.out.println(candidatethree +" has been selected to " +optionch +".");
                                    System.out.println(" ");
                                    System.out.println("Thank you for using 'Duty Decider. Have a nice day");
                                }
                                else if(z<0.6664&&z>0.4998){
                                    System.out.println(candidatefour +" has been selected to " +optionch +".");
                                    System.out.println(" ");
                                    System.out.println("Thank you for using 'Duty Decider. Have a nice day!");
                                }
                                else if(z<833&&z>0.6664){
                                    System.out.println(candidatefive +" has been selected to " +optionch +".");
                                    System.out.println(" ");
                                    System.out.println("Thank you for using 'Duty Decider. Have a nice day!");
                                }
                                else if(z<1.00&&z>0.833){
                                    System.out.println(candidatesix +" has been selected to " +optionch +".");
                                    System.out.println(" ");
                                    System.out.println("Thank you for using 'Duty Decider. Have a nice day!");
                                }
                            }
                        }
                    }
                }
            }
        }


    }   
}



}

该计划应该从最多六名候选人中进行选择,他们将完成用户选择的某项职责或家务活。

2 个答案:

答案 0 :(得分:1)

你错过了一个结束括号:

//where I believe the problem begins
                d = input.nextLine();
                candidatefour = d;
                if(candidatefour.contains("go")){
                    //z by thirds, point .33
                    if(z<0.33){
                        System.out.println(candidateone +" has been selected to " +optionch +".");
                        System.out.println(" ");
                        System.out.println("Thank you for using 'Duty Decider. Have a nice day");
                    }
                    else if(z<0.66&&z>0.33){
                        System.out.println(candidatetwo +" has been selected to " +optionch +".");
                        System.out.println(" ");
                        System.out.println("Thank you for using 'Duty Decider. Have a nice day");
                    }
                    else if(z<1.00&&z>0.66){
                        System.out.println(candidatethree +" has been selected to " +optionch +".");
                        System.out.println(" ");
                        System.out.println("Thank you for using 'Duty Decider. Have a nice day");

                    }
                    else{
                        System.out.println(candidatefour +" has been confirmed.");

最后两行应该是这样的:

                    }else{
                        System.out.println(candidatefour +" has been confirmed.");

正如laune指出的那样,支架实际上并没有丢失,而是放错位置,所以你还必须删除程序末尾的许多右括号中的一个。

答案 1 :(得分:0)

你有这个if语句:

if(candidatefour.contains("go")){
                //z by thirds, point .33
           if(z < 0.33){

           }
           else if(z < 0.66 && z > 0.33){

           }
           else if(z < 1.00 && z > 0.66){

           }
           else{
               // lots of code
           }

  // close all open blocks, method, class.

你将最后一个与错误联系起来。