What the problem I want to ask is I cannot make the E part, but the V one is working and I tried all I could to figure it out what is going on in my logic... Please help me out to make the 'E' works.
import java.util.Scanner;
public class Service {
public static void main(String args[]) {
String service;
float monthV, usaV, ausV, rusV, callV;
float monthE, usaE, ausE, rusE, callE;
Scanner input = new Scanner(System.in);
System.out.print("Which service did you use for the calls?<V - Vartec, E - Eircom> : ");
service = input.nextLine();
if (service.charAt(0) != 'E')
if (service.charAt(0) != 'V') {
System.out.println("Thank you for your time...good bye.");
} else {
if (service.charAt(0) == 'V') {
System.out.print("\nPlease enter the total number of calls made in the month: ");
monthV = input.nextFloat();
System.out.print("\nPlease enter the number of minutes spent calling the USA: ");
usaV = input.nextFloat();
System.out.print("\nPlease enter the number of minutes spent calling the Australia: ");
ausV = input.nextFloat();
System.out.print("\nPlease enter the number of minutes spent calling the Russia: ");
rusV = input.nextFloat();
callV = ((usaV * 0.06f) + (ausV * 0.08f) + (rusV * 0.24f));
System.out.println("The total cost of using the Vartec service for the month is"
+ String.format("%.2f", callV));
} else {
if (service.charAt(0) == 'E') {
System.out.print("\nPlease enter the total number of calls made in the month: ");
monthE = input.nextFloat();
System.out.print("\nPlease enter the number of minutes spent calling the USA: ");
usaE = input.nextFloat();
System.out.print("\nPlease enter the number of minutes spent calling the Australia: ");
ausE = input.nextFloat();
System.out.print("\nPlease enter the number of minutes spent calling the Russia: ");
rusE = input.nextFloat();
callE = ((usaE * 0.19f) + (ausE * 0.85f) + (rusE * 0.92f));
System.out.println("The total cost of using the Vartec service for the month is"
+ String.format("%.2f", callE));
}
}
}
}
simple way
a bit more advanced (easier to understand with more options):
Your problem is doing something like
Note: you can also use a switch with strings (unless using very old java version):