如何在我的调查java项目中避免无限循环?

时间:2014-06-14 13:41:54

标签: infinite-loop infinite survey

我刚跳到编程领域。请帮我找一个合理的解决方案来解决我的教育项目。如果输入不正确(例如'z'代替图形),我的程序将启动无限循环。我正在寻找无限循环的替代方案并捕捉使用。至少,也许有人可以建议如何清空托盘(此站点上提供的方法不适用于我的Win7,NetBeans IDE 7.4)。

目标 制定一个程序,允许确定调查中获得的特定数量的指标。

数据 公司赞助您为目标潜在客户实现调查系统。根据白天收集的个人数据,我们可以计算出有助于公司更好地定位客户的指标。

用于调查的问题是:

问题1:您是否喜欢使用我们的产品?

问题2:如果对问题1的回答是肯定的,您将继续使用我们的产品吗?

问题3:您打算使用我们竞争对手的产品吗?

问题4:在1到3的范围内(1:差,2中立,3优),您如何看待我们公司的声誉?

具体任务是确定每个问题的百分比。对于问题1至3,需要YES和NO的百分比。对于问题4,需要每种可能选择的百分比。

在程序过程中,数据存储在单个存储器结构中。到下午晚些时候,代理在输出数据后终止程序。因此,我们暂时不会坚持使用文件或数据库。

数据或指标将在当天晚些时候以清晰的格式发布。

因此,目的是实现数据的捕获和存储以及计算指标。

这是您的Java代码:

package surveyproject;

import java.util.Scanner;

import java.util.ArrayList;

public class SurveyProject {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);


        ArrayList TotalSurvey = new ArrayList();

        do {


               try
               {

                System.out.println("Welcome to survey !");

                 System.out.println("1 - New survey");
                 System.out.println("2 - Publish survey results");
                 System.out.println("3 - Leave the program");

                 int new_or_printing_results_or_exit = sc.nextInt(); 

                 if (new_or_printing_results_or_exit == 1)
                 {

                          // ============== Questions 1 and 2 ================ //

                            System.out.println("Do you use our product? 1-yes 0-no");

                             int Q1_using_product = sc.nextInt(); 

                             int Q2_using_product_in_future = -1; 

                             if (Q1_using_product == 1)
                             {
                                 System.out.println("Thanks to use our product. Will you "
                                         + "continue to use it? 1-yes 0-no");

                                 Q2_using_product_in_future = sc.nextInt();


                             }
                             else 
                             {
                                 System.out.println("That's a pity you do not use your product.");
                             }

                         // =====///====== Question 1 and 2 ================ //

                         // ============== Question 3 ================ //
                              System.out.println("Do you use the products of our competitors? 1-yes 0-no");

                                int Q3_using_competitor_product = sc.nextInt();
                         // =====///====== Question 3 ================ //    

                         // ============== Question 4 ================ //
                              System.out.println("Evaluate our company.");
                              System.out.println("1 - Bad");
                              System.out.println("2 - Neutral");
                              System.out.println("3 - Good");

                                int Q4_company_reputation_rate = sc.nextInt();
                         // =====///====== Question 4 ================ //

                         System.out.println("Thank you for the participation!");

                         // =========== Save current Survey =========== //


                         Survey objSurvey = new Survey(Q1_using_product, 
                                                          Q2_using_product_in_future,
                                                          Q3_using_competitor_product,
                                                          Q4_company_reputation_rate);

                         TotalSurvey.add(objSurvey);


                         // =====///=== Save current Survey =========== //

                 }
                 else if (new_or_printing_results_or_exit == 2)
                 {


                     PrintSurveyResult(TotalSurvey);


                 }
                 else if (new_or_printing_results_or_exit == 3)
                 {
                      System.out.println("Thank you to use our system!");

                      System.exit(0);
                 }

               }
               catch(Exception except)
               {
                   System.out.println("You entered incorrect answer. Please,"
                           + "try again the survey." );


                     System.out.println("Total answers: " + total_answers);

                     System.out.println("Question 1: ");
                     System.out.println("Yes answered: " + Q1_answer_yes + "(" + (double)Q1_answer_yes/total_answers * 100.0 + "%)");
                     System.out.println("No answered: " + Q1_answer_no + "(" + (double)Q1_answer_no/total_answers * 100.0 + "%)");


                     System.out.println("Question 2: ");
                     System.out.println("Yes answered: " + Q2_answer_yes + "(" + (double)Q2_answer_yes/total_answers * 100.0 + "%)");
                     System.out.println("No answered: " + Q2_answer_no + "(" + (double)Q2_answer_no/total_answers * 100.0 + "%)");


                     System.out.println("Question 3: ");
                     System.out.println("Yes answered: " + Q3_answer_yes + "(" + (double)Q3_answer_yes/total_answers * 100.0 + "%)");
                     System.out.println("No answered: " + Q3_answer_no + "(" + (double)Q3_answer_no/total_answers * 100.0 + "%)");


                     System.out.println("Question 4: ");
                     System.out.println("Bad answered: " + Q4_answer_bad + "(" + (double)Q4_answer_bad/total_answers * 100.0 + "%)");
                     System.out.println("Neutral answered: " + Q4_answer_neutral + "(" + (double)Q4_answer_neutral/total_answers * 100.0 + "%)");
                     System.out.println("Good answered: " + Q4_answer_good + "(" + (double)Q4_answer_good/total_answers * 100.0 + "%)");       
    }   
}


package surveyproject;

public class Survey {

    int Q1_using_product;
    int Q2_using_product_in_future;
    int Q3_using_competitor_product;
    int Q4_company_reputation_rate;


    public Survey (int Q1, int Q2, int Q3, int Q4)
    {
        Q1_using_product = Q1;
        Q2_using_product_in_future = Q2;
        Q3_using_competitor_product = Q3;
        Q4_company_reputation_rate = Q4;

    }


    public int getQ1_using_product()
    {
       return Q1_using_product;
    }

    public int getQ2_using_product_in_future()
    {
       return Q2_using_product_in_future;
    }

    public int getQ3_using_competitor_product()
    {
       return Q3_using_competitor_product;
    }

    public int getQ4_company_reputation_rate()
    {
       return Q4_company_reputation_rate;
    }
}

0 个答案:

没有答案