Reputation: 17
Here is my code:
import java.util.Scanner;
import java.io.Console;
public class Activity2
{
public static void main(String[] args) {
String name;
int choice;
int time;
Console cons = System.console();
if (cons==null)
{
System.out.println("");
return;
}
name = cons.readLine("Enter your Name: ");
Scanner scn = new Scanner(System.in);
System.out.println("\nMenu");
System.out.println("\n[1] Network Engineer\n[2] Software Engineer\n[3] Full Stack Developer\n[4] Technical Support");
System.out.print("\nEnter your job(Choose a number from the menu): ");
choice = scn.nextInt();
System.out.print("Enter no. of hours present at work: " );
time = scn.nextInt();
if (choice == 1)
{
int gsalary = 1200*time;
int nsalary = gsalary - (gsalary * 12 / 100);
cons.printf("\nHello %s!\n", name);
System.out.println("Your job salary as a Network Engineer is 1200 per hour.");
System.out.println("Your gross salary for this week is "+gsalary+".");
System.out.println("Your net salary for this weeks is "+nsalary+".");
}
if (choice == 2)
{
int gsalary = 800*time;
int nsalary = gsalary - (gsalary * 12 / 100);
cons.printf("\nHello %s!\n", name);
System.out.println("Your job salary as a Network Engineer is 800 per hour.");
System.out.println("Your gross salary for this week is "+gsalary+".");
System.out.println("Your net salary for this weeks is "+nsalary+".");
}
if (choice == 3)
{
int gsalary = 600*time;
int nsalary = gsalary - (gsalary * 12 / 100);
cons.printf("\nHello %s!\n", name);
System.out.println("Your job salary as a Network Engineer is 600 per hour.");
System.out.println("Your gross salary for this week is "+gsalary+".");
System.out.println("Your net salary for this weeks is "+nsalary+".");
}
if (choice == 4)
{
int gsalary = 500*time;
int nsalary = gsalary - (gsalary * 12 / 100);
cons.printf("\nHello %s!\n", name);
System.out.println("Your job salary as a Network Engineer is 500 per hour.");
System.out.println("Your gross salary for this week is "+gsalary+".");
System.out.println("Your net salary for this weeks is "+nsalary+".");
}
}
}
Here is the sample output:
Enter your name: Matsuno Chifuyu
Menu
[1]Network Engineer
[2]Software Engineer
[3]full Stack Developer
[4]Technical Support
Enter your job(Choose from the menu): 3 Enter no. of hours present in work: 36
Hello Matsuno Chifuyu! Your job salary as a Full Stack Developer is 600 per hour. Your gross salary for this week is 21600. Your net salary for this week is 19008.
Do you want to input another employee? (Yes or No)
How can I manage to do a loop that If answered yes the program will restart and if no the program will exit
Upvotes: 0
Views: 56
Reputation: 856
Easiest way to accomplish this is to throw your code in a loop and then break if the user inputs no. See the example below:
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while (true) {
// put your app logic here
System.out.println("Do you want to input another employee? [Y]es/[N]o");
if (!in.next().equalsIgnoreCase("Y") {
break;
}
}
}
Upvotes: 2