这是嵌套的吗?和循环?不确定我是否正确使用它们。但我收到了错误。 “否则没有”

时间:2016-02-07 03:39:58

标签: java if-statement while-loop nested

//决定用户是否会获得佣金。

import java.util.Scanner;
public class commiPay{
    public static void main( String[] args ){
        Scanner input = new Scanner(System.in);
        int pay = 200;
        double com;
        double sales;
        double x;
        while ( sales = -1 )// I want the user to enter positive numbers
        {
            System.out.printf( "Sales cannot be negative");
            System.out.printf( "Enter sales sold this week: ");
        }
        System.out.println("Enter sales made this week: ");
        sales = input.nextInt(); 
        if ( sales >= 7500 )/*got this format from the traditional letter grade java example**/
        {
            System.out.println( "Commission is multiplied by 5%. " );
            com = sales*.5;
         else if ( sales >= 5001 )
             System.out.println( "Commission is multiplied by 4%. " );
             com = sales*.4;
         else if ( sales >= 2501 )
             System.out.println( "Commission is multiplied by 3%. " );
            com = sales*.3;   
         else if ( sales >= 1001 )
             System.out.println( "Commission is multiplied by 2%. " );
             com = sales*.2;    
         else ( sales < 1000 )
             System.out.println( "No commission. " );
             total = pay;
         }
         System.out.println( "You will get paid" + total + "this week."  );
     }
}

销售人员每周支付 $ 200.00 以及佣金。佣金取决于她(他)本周的销售额。

编写一个程序,提示用户一周的销售额(使用销售额的整数)。

如果输入的金额小于0,则显示一条错误消息,告知用户“销售金额不能为负数!”

使用下表中的信息,确定销售人员的佣金应该是多少。佣金应基于本周的总销售额。

委员会表:

        up to 1,000                              no commission
        1,001 – 2,500                            2% of sales
        2,501 – 5,000                            3% of sales
        5,001 – 7,500                            4% of sales
        over 7,500                               5% of sales

5 个答案:

答案 0 :(得分:0)

确保正确格式化if / elseif / else语句:

if() {

} else if() {

} else if() {

} else {

}

答案 1 :(得分:0)

您的代码中缺少很多括号。您还需要初始化变量。键入小于1000的值,您将看到代码现在可以正常工作。还有一些我修复过的代码尚未实现。在每个if或else if块中,您需要将public void method2() { method1(); System.out.println("harry 2"); } com变量添加到pay变量中。我还注意到你的佣金公式中你的小数点已经关闭了。

例如:

您的代码total中有这个,这将产生20%的佣金,而不是代码评论中的2%。正确的公式是:

com = sales * .2;

我在您的代码中应用了此修复程序。

com = sales * 0.02;

答案 2 :(得分:0)

如果您在其他人之后拥有多行,则需要将其放在大括号内,以确保您的程序不会混淆。据我所知,你只能使用

else if(something)
     indented something;
else if(something else)
etc.

如果你只有一行。

else if ( sales >= 5001 )
             System.out.println( "Commission is multiplied by 4%. " );
             com = sales*.4;

需要

else if ( sales >= 5001 ){
             System.out.println( "Commission is multiplied by 4%. " );
             com = sales*.4;
}

等等。

答案 3 :(得分:0)

您的代码缺少括号,而while condition中的代码也是错误的。

import java.util.Scanner;
public class commiPay{
public static void main( String[] args ){
Scanner input = new Scanner(System.in);
    int pay = 200;
    double com;
    double sales = 0;
    double x;
    double total; // added this variable

    //First you need to get the user input. So I added these two lines to here.
    System.out.println("Enter sales made this week: ");
    sales = input.nextInt();

    //Checking whether enterd number is a positive or not
    while ( sales < 0 )// I want the user to enter positive numbers
    {
        System.out.printf( "Sales cannot be negative ");
        System.out.printf( "Enter sales sold this week: ");
        sales = input.nextInt(); // if the entered number is an negative number ask the user to re enter the number 
    }

    if ( sales >= 7500 )/*got this format from the traditional letter grade java example**/
    {
        System.out.println( "Commission is multiplied by 5%. " );
        com = sales*0.05;
    }
     else if ( sales >= 5001 ){
         System.out.println( "Commission is multiplied by 4%. " );
         com = sales*0.04;
     }
     else if ( sales >= 2501 ){
         System.out.println( "Commission is multiplied by 3%. " );
        com = sales*0.03;
     }
     else if ( sales >= 1001 ){
         System.out.println( "Commission is multiplied by 2%. " );
         com = sales*0.02;
     }
     else if( sales < 1000 ){
         System.out.println( "No commission. " );
         total = pay;
     }
}
}

答案 4 :(得分:0)

您有以下内容:

    while ( sales = -1)

但如果他们没有具体输入-1怎么办?它应该是:

    while ( sales < 0 )

这将涵盖任何负数。您还应修改括号,因为您似乎缺少一些。在 while 循环后,您也可以不使用 printf ,并使用标准 println

    while ( sales = -1 )// I want the user to enter positive numbers
    {
        System.out.printf( "Sales cannot be negative");
        System.out.printf( "Enter sales sold this week: ");

但我不认为这会导致问题。

至于你的其他代码,你在你的印刷行中记录销售额将乘以5%,但乘以0.5(如下):

    System.out.println( "Commission is multiplied by 5%. " );
        com = sales*.5;

乘以0.5会使其达到50%。尝试将其交换为0.05(5/100)。