0

私は本当にJavaを学んでいて、DeitelとDeitelによる本「Javahow to Program」のチュートリアルを読んでいるので、私が犯している基本的な間違いは許してください。私の方法論は最善ではないかもしれないことを理解していますが、これを改善したいと思っています。

私の問題は、プログラムを間違って作成したと信じていることです。プログラムを実行すると、IFオプションとELSEオプションの両方が出力されます。

両方のオプションが実行されている理由を誰かに教えてもらえれば、非常にありがたいです

一番深い場所

package controlStatments;
import java.util.Scanner;

public class Review_4_20 
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        //Declare Variables
        double employeeOneRate;
        double employeeOneHours;
        double employeeTwoRate;
        double employeeTwoHours;
        double employeeThreeRate;
        double employeeThreeHours;
        int calculator;
        double employeeOneTotalPay;
        double employeeOneNormalPay;
        double employeeOneTotalPayOverTime;
        double overTimeRate;

        //Initiate Variables
        employeeOneRate = 0;
        employeeOneHours = 0;
        employeeTwoRate = 0;
        employeeTwoHours = 0;
        employeeThreeRate = 0;
        employeeThreeHours = 0;
        calculator = 0;
        employeeOneTotalPay = 0;
        employeeOneTotalPayOverTime = 0;
        overTimeRate = 1.5; 
        employeeOneNormalPay = 0;

        //Create While Loop
        while (calculator != -1)
        {   
            //Prompt user to input details
            System.out.print("\n\nPlease input the first employees rate");
            employeeOneRate =input.nextDouble();

            System.out.print("Please input the first employees Hours");
            employeeOneHours =input.nextDouble();

            if (employeeOneHours <= 40)
            {
                employeeOneTotalPay = employeeOneHours * employeeOneRate;
                System.out.printf("\nNormal time pay is: %.2f", employeeOneTotalPay);
            }
            else 
                employeeOneNormalPay = employeeOneRate * 40;
            employeeOneTotalPayOverTime = (employeeOneHours - 40) * (employeeOneRate * overTimeRate) + employeeOneNormalPay;                            
            System.out.printf("\n\nTotal Pay including Overtime is: %.2f", employeeOneTotalPayOverTime);
        }
    }
}
4

3 に答える 3

6

elseステートメントの括弧を忘れました。

私はそれがこのようになるべきだと思います:

    if (employeeOneHours <= 40)
    {
        employeeOneTotalPay = employeeOneHours * employeeOneRate;
        System.out.printf("\nNormal time pay is: %.2f", employeeOneTotalPay);
    }
    else {
        employeeOneNormalPay = employeeOneRate * 40;
        employeeOneTotalPayOverTime = (employeeOneHours - 40) * 
                (employeeOneRate * overTimeRate) + employeeOneNormalPay;                                
        System.out.printf("\n\nTotal Pay including Overtime is: %.2f", employeeOneTotalPayOverTime);
    }

角かっこを設定しない場合、最初の行だけがIF/ELSEステートメントに含まれます。

于 2012-04-21T16:29:27.923 に答える
1

、、、、およびループなどの括弧で囲まれたステートメントは、if直後に括弧がない場合にのみ、その直後の行を実行します。else ifelse

だから、あなたの声明:

if (employeeOneHours <= 40)
{
    employeeOneTotalPay = employeeOneHours * employeeOneRate;
    System.out.printf("\nNormal time pay is: %.2f", employeeOneTotalPay);
}
else 
    employeeOneNormalPay = employeeOneRate * 40;
    employeeOneTotalPayOverTime = (employeeOneHours - 40) * (employeeOneRate * overTimeRate) + employeeOneNormalPay;                            
    System.out.printf("\n\nTotal Pay including Overtime is: %.2f", employeeOneTotalPayOverTime);

常に乗算しemployeeOneNormalPayます。

次のように変更します。

if (employeeOneHours <= 40) {
    employeeOneTotalPay = employeeOneHours * employeeOneRate;
    System.out.printf("\nNormal time pay is: %.2f", employeeOneTotalPay);
} else {
    employeeOneNormalPay = employeeOneRate * 40;
    employeeOneTotalPayOverTime = (employeeOneHours - 40) * (employeeOneRate * overTimeRate) + employeeOneNormalPay;                            
    System.out.printf("\n\nTotal Pay including Overtime is: %.2f", employeeOneTotalPayOverTime);
}
于 2012-04-21T16:31:08.170 に答える
1

変化する

if (employeeOneHours <= 40)
        {
            employeeOneTotalPay = employeeOneHours * employeeOneRate;
            System.out.printf("\nNormal time pay is: %.2f", employeeOneTotalPay);
        }
        else 
            employeeOneNormalPay = employeeOneRate * 40;
        employeeOneTotalPayOverTime = (employeeOneHours - 40) * (employeeOneRate * overTimeRate) + employeeOneNormalPay;                            
        System.out.printf("\n\nTotal Pay including Overtime is: %.2f", employeeOneTotalPayOverTime);

と:

if (employeeOneHours <= 40)
        {
            employeeOneTotalPay = employeeOneHours * employeeOneRate;
            System.out.printf("\nNormal time pay is: %.2f", employeeOneTotalPay);
        }
        else 
       {
            employeeOneNormalPay = employeeOneRate * 40;
        employeeOneTotalPayOverTime = (employeeOneHours - 40) * (employeeOneRate * overTimeRate) + employeeOneNormalPay;                            
        System.out.printf("\n\nTotal Pay including Overtime is: %.2f", employeeOneTotalPayOverTime);
}
于 2012-04-21T16:34:22.727 に答える