Write a program to input a person’s age and salary. If the age is over 32, then add $1000, otherwise subtract $500 from salary. Finally output the age and new salary. here is the problem

Scanner scan = new Scanner (System.in);
        int age,  sum, difference, old_salary, new_salary, salary;


        System.out.println("Please enter the person age");
        age = scan.nextInt();

        System.out.println("Please enter the person old salary");
        old_salary = scan.nextInt();

        System.out.println("Please enter the person new salary");
        new_salary = scan.nextInt();


        if(age > 32)
        {

           sum = old_salary + 1000;


           System.out.println("your salary is " + salary ); getting a red line underneath 'salary' (the variable salary has nnot be initialized)

        }

        else if (age<=32)
        {

            old_salary = new_salary - 500;
            difference = old_salary - 500;

            System.out.println("your new salary is "  + salary); getting a red line underneath 'salary' (the variable salary has nnot be initialized)
            System.out.println("your age is " + age);

why do i have to initialized salary, at least when I run the program, I dont think it run properly. when i entered the old salary it say one thing but I think it increased when i entered the new salary, at least i think it should have increased to show the new salary and decreased if the condition in the first statement is false. dont know where I have gone wrong again.

Recommended Answers

All 2 Replies

well, the problem is that salary is not initialized.
you've initialized (set a value) for old_salary and for new_salary, but not for the variable salary.

so, just change this line:

int age, sum, difference, old_salary, new_salary, salary;

to

int age, sum, difference, old_salary, new_salary, salary = 0;

but since you don't use this variable (except for the print) why declare/print it at all ?

thanks stultuske

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.