-3

i know this is easy but i am new to java. We are learning if, else and the while loop in class. I i don't know how to write the "if" part in this problem. So i don't if i need to declare a new string name excellent or something like that.

//****************************************************************************************
//
// Computes the amount of raise and the new 
// salary for an employee. The current salary
//  and a performance rating (A string: Excellent", "Good" or "Poor") are input.
//****************************************************************************************

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

public class Salary1
{
  public static void main (String[] args)
  {
    double currentSalary; //employee's current salary
    double raise;         // amount of the raise
    double newSalary;     // new salary of 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();

    // Compute the the raise using if....


    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();
  }
}

1 Answers1

-1

The pseudo-code for if statements looks like this:

if(/*boolean value*/) {
    // code to execute when the part in parenthesis is true
} else {
    // code to execute when the part in parenthesis is false
}

In the above pseudo-code snippet, /*boolean value*/ can be replaced by anything that can be evaluated to true or false. This includes:

  • Boolean variables
  • Integers data types (where 0 is false and non-zero is true)
  • Methods that return Boolean or Integer data types
  • Comparison expression (==, !=, <, >, >=, <=)
  • Any combination of the above, combined using the Boolean operators (||, &&)

And as a side note, you can obtain the inverse boolean value of any of these using an !.

It is also worth mentioning that the else part of the statement is completely optional.

And this will help get you started on comparing strings in Java (with a method that will return a boolean value.

Community
  • 1
  • 1
nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • Can someone explain the downvote? The question is about writing `if` statements in Java. Did I miss something? – nhgrif Dec 10 '13 at 03:26