Java -if else显示多个输出

时间:2016-09-16 04:01:52

标签: java if-statement

如果不期望答案,我会将这段代码返回到程序的开头。

...
else // returns to start for unsatisfactory
{   
    System.out.println();
    System.out.println();
    System.out.println("Check your spelling and try again");
    main (args);                                       
}   
...   

然而,当我输入不同的单词然后再次通过并输入预期的单词时,程序会输出两个不同的结果

<小时/> 目前的薪水:100.00美元

筹集金额:$ 4.00

你的新工资:104.00美元


目前的薪水:100.00美元

加注金额:$ 0.00

您的新薪水:$ 100.00

我尝试使用else if语句来消除它作为一个原因,但它导致了同样的事情。


import java.util.Scanner;
import java.text.NumberFormat;

 public class Salary {
     public static void main (String[] args) {
         double currentSalary; // employee's current salary
         double raise = 0.0;         // amount of the raise
         double newSalary;     // new salary for the employee
         String rating;         // performance rating

         Scanner scan = new Scanner(System.in);
         System.out.print ("Enter the current salary: ");
         currentSalary = scan.nextDouble();

        System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");

        rating = scan.next();

        // Computes raise with if-else
        if ((rating.equals("Excellent")) || (rating.equals("excellent"))) {
            // calculates raise for excellent
            raise = .06 * currentSalary;
        }
        else if ((rating.equals("Good")) || (rating.equals("good"))) {
            // calculates raise for good
            raise = .04 * currentSalary;
        }
        else if ((rating.equals("Poor")) || (rating.equals("poor"))) {
            // calculates raise for poor
            raise = .015 * currentSalary;
        }   
        else {
            // returns to start for unsatisfactory
            System.out.println();
            System.out.println();
            System.out.println("Check your spelling and try again");
            main (args);
        }      

        newSalary = currentSalary + raise;

        // Print the results

        NumberFormat money = NumberFormat.getCurrencyInstance();
        System.out.println();
        System.out.println("Current Salary:       " + money.format(currentSalary));
        System.out.println("Amount of your raise: " + money.format(raise));
        System.out.println("Your new salary:     " + money.format(newSalary));
        System.out.println();
    }
}

5 个答案:

答案 0 :(得分:2)

这是因为当你没有获得预期的输入时,你递归地调用main(这不被认为是好的练习BTW)。输入(第二次)预期输入后,必须仍然执行初始main的剩余部分,然后使用0.0的raise,因为输入无效。

针对您的问题的实用解决方案可能是避免递归调用main并将其换行,例如像这样的循环中的输入验证

...
System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");
while (true) {
   rating = scan.next();   
   if ((rating.equals("Excellent")) || (rating.equals("excellent")))
   { 
       raise = .06 * currentSalary; break;
   }
   else if ((rating.equals("Good")) || (rating.equals("good")))
   {
       raise = .04 * currentSalary; break;
   }
   else if ((rating.equals("Poor")) || (rating.equals("poor")))
   {
       raise = .015 * currentSalary; break;
   }   
   else 
   {   
      System.out.println();
      System.out.println();
      System.out.println("Check your spelling and try again");     
   } 
}
...

答案 1 :(得分:1)

你对main()&#34发出的(第二次)电话;结束&#34;然后回到第一个&#34;通过启动程序调用的那个。

因此,第一批结果来自您对main()的显式调用。第二个地段是从那个电话结束时开始,然后你回到你打电话的地方。

不建议递归调用main()。你应该在main()中使用while循环。即在您知道输入有效之前继续询问输入,然后实际使用它。

答案 2 :(得分:1)

您致电main (args);后仍未返回,因此您的计划的每次迭代都会继续。

您应该在return;

之后添加main (args);
{   
       System.out.println();
       System.out.println();
       System.out.println("Check your spelling and try again");
       main (args);
       return;

}   

编辑:正如John3136所指出的那样,你也不应该递归地调用main (args)

答案 3 :(得分:0)

你不应该递归地调用main。你应该使用do while循环来更新代码并且它工作正常。

  import java.util.Scanner;

  import java.text.NumberFormat;



  public class Salary {

  public static void main (String[] args) {

     double currentSalary; // employee's current salary

     double raise = 0.0;         // amount of the raise

     double newSalary;     // new salary for the employee

     String rating;         // performance rating

     boolean flag=false; // to check input 


     Scanner scan = new Scanner(System.in);


 do{

       System.out.print ("Enter the current salary: ");

       currentSalary = scan.nextDouble();

       System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");

       rating = scan.next();


       // Computes raise with if-else
         if ((rating.equals("Excellent")) || (rating.equals("excellent"))) // calculates raise for excellent
            { 
                   raise = .06 * currentSalary;
                   flag=true;

            }
            else if ((rating.equals("Good")) || (rating.equals("good")))   // calculates raise for good
               {
                   raise = .04 * currentSalary;
                     flag=true;

               }
            else if ((rating.equals("Poor")) || (rating.equals("poor"))) // calculates raise for poor
               {
                   raise = .015 * currentSalary;
                     flag=true;

               }   
         else                                                     // returns to start for unsatisfactory
            {   
                   System.out.println();
                   System.out.println();
                   System.out.println("Check your spelling and try again");
                   flag=false;

            }      

 }while(!flag);


     newSalary = currentSalary + raise;



     // Print the results

    NumberFormat money = NumberFormat.getCurrencyInstance();

    System.out.println();

    System.out.println("Current Salary:       " + money.format(currentSalary));

    System.out.println("Amount of your raise: " + money.format(raise));

    System.out.println("Your new salary:     " + money.format(newSalary));

    System.out.println();






       }

  }

答案 4 :(得分:0)

您可能想要考虑另一种方法。如果输入无效或尝试修复它,请终止问题。以下示例适用于“修复”方法,

public class Salary {
    public static void main(String[] args) {
        IPM ipm;
        if (verify(args)) {
            ipm = new IPM(args);
        } else {
            Scanner scan = new Scanner(System.in);
            String[] update;
            do {
                update = repair(scan);
            } while (!verify(update)); // You may want a loop count as well...
            ipm = new IPM(update);
        }
        ipm.print();
    }

    public static boolean verify(String[] s) {
        return IPM.verify(s);
    }

    public static String[] repair (Scanner s) {
        // Request new input and store it in an array of strings.
        // This method does not validate the input.
    }
}

public class IPM {
    double currentSalary;
    double raise = 0.0;
    double newSalary;
    String rating;

    IPM(String[] input) {
        // Set attributes of IPM.
    }

    public static boolean verify(String[] s) {
        //Determine if the input is valid.
    }

    public void print() {
        // Print IPM object.
    }
}

请注意从工资中调用IPM.verify()。这应该是IPM责任的一部分,因为薪水不需要了解主要内容。此外,课程IPM可能会更改,对IPM.verify()的调用也不会要求所有验证IPM的课程也会发生变化。