Java tutorial for beginners, tutorial 9 : If and Else. This program calculates the retail profit, if the value of retail price is more than 250 than the profit is 30 %, else the profit is 40 %.
Step 1 : Read the retail price
Step 2 : If else condition, calculate the profit rate. If it is more than 250, then profit is 30 % of the retail price. Else the profit is 40 % of the retail price.
Step 3 : Print out the profit .
Code below :
// Tutorial on if condition
// For example, if your retail price is more than 250, the profit is 30 %
// Else, the profit rate is fix at 40 %
//use scanner class to input retail price
import java.util.Scanner ; // forgot to add this line
public class TestIf {
public static void main(String[] args){
//declare and initialize the variables;
int retailPrice = 0;
int profit = 0;
Scanner myscan = new Scanner(System.in);
System.out.print("Enter your retail price : ");
retailPrice = myscan.nextInt();
//Your condition at here
if (retailPrice > 250) {
profit = retailPrice * 30 / 100 ; // more than 250 the profit rate is only 30 %
}
else {
profit = retailPrice * 40 / 100 ; // more than 250 the profit rate is only 30 %
}
//Print out the output
System.out.println("Your profit is : " + profit);
}
}
No comments:
Post a Comment