I am trying to create a code that will determine the cost of a phone bill.
Here is my code so far
import java.util.Scanner;
public class PhoneBill
{
public static void main(String[] args)
{
// Create Scanner
Scanner input = new Scanner (System.in);
//Declare variables
double minutes, nightm, daym, totalminutes, totalcost, daycost, nightcost;
int account;
//Obtain account number
System.out.println("Enter your account number: ");
account = input.nextInt();
//Determine service
System.out.println("Enter r for regular service or p for premium service: ");
char service = input.next(".").charAt(0);
//Logic for regular service
if (service == 'r')
System.out.println("Enter number of minutes talked: ");
minutes = input.nextDouble();
if (minutes >= 50)
totalcost = (10.00 + ((minutes - 50) * 0.20));
else
totalcost = (10.00);
//Logic for premium service
else if (service == 'p')
System.out.println("Enter number of minutes used from 6:00AM to 6:00PM");
daym = input.nextDouble();
System.out.println("Enter number of minutes used from 6:00PM to 6:00AM");
nightm = input.nextDouble();
//Obtain cost for daytime minutes
if (daym < 75)
daycost = 0;
else
daycost = ((daym - 75) * .10);
//Obtain cost for night minutes
if (nightm < 100)
nightcost = 0;
else
nightcost = ((nightm - 100) * .05);
//Obtain total cost of premium service
totalcost = daycost + nightcost;
//Display account
System.out.println ("The account number is: " + account);
//Display service type
if (service == 'r')
System.out.println ("You have selected regular service");
else if (service == 'p')
System.out.println ("You have selected premium service");
//Display results
if (service == 'r')
System.out.println ("Total minutes used is " + minutes);
System.out.println ("Total ammount due is $" + totalcost);
else if (service == 'p')
System.out.println ("Minutes used during the day is: " + daym);
System.out.println ("Minutes used during the night is: " + nightm);
System.out.println ("Total minutes used is: " + (daym + nightm));
System.out.println ("The amount due is: $" + totalcost);
}
}
I feel like I am very close but it keeps giving me this error
PhoneBill.java:41: error: 'else' without 'if'
else if (service == 'p')
PhoneBill.java:78: error: 'else' without 'if'
else if (service == 'p')
I have if statements above these statements, but it is still telling me that I do not.
What am I not seeing?
You need to use brackets on an If Else statement if the inner block of code is more than one line. I always use brackets to increase readability of the code.
Scanner input = new Scanner (System.in);
//Declare variables
double minutes=0, nightm=0, daym=0, totalminutes=0, totalcost=0, daycost=0, nightcost=0;
int account;
//Obtain account number
System.out.println("Enter your account number: ");
account = input.nextInt();
//Determine service
System.out.println("Enter r for regular service or p for premium service: ");
char service = input.next(".").charAt(0);
//Logic for regular service
if (service == 'r'){
System.out.println("Enter number of minutes talked: ");
minutes = input.nextDouble();
if (minutes >= 50)
totalcost = (10.00 + ((minutes - 50) * 0.20));
else
totalcost = (10.00);
//Logic for premium service
}else if (service == 'p'){
System.out.println("Enter number of minutes used from 6:00AM to 6:00PM");
daym = input.nextDouble();
System.out.println("Enter number of minutes used from 6:00PM to 6:00AM");
nightm = input.nextDouble();
//Obtain cost for daytime minutes
if (daym < 75)
daycost = 0;
else
daycost = ((daym - 75) * .10);
//Obtain cost for night minutes
if (nightm < 100)
nightcost = 0;
else
nightcost = ((nightm - 100) * .05);
}
//Obtain total cost of premium service
totalcost = daycost + nightcost;
//Display account
System.out.println ("The account number is: " + account);
//Display service type
if (service == 'r'){
System.out.println ("You have selected regular service");
}else if (service == 'p'){
System.out.println ("You have selected premium service");
}
//Display results
if (service == 'r'){
System.out.println ("Total minutes used is " + minutes);
System.out.println ("Total ammount due is $" + totalcost);
}else if (service == 'p'){
System.out.println ("Minutes used during the day is: " + daym);
System.out.println ("Minutes used during the night is: " + nightm);
System.out.println ("Total minutes used is: " + (daym + nightm));
System.out.println ("The amount due is: $" + totalcost);
}
If you are going to have more than one line under an if/else if/else
statement, you need to use curly brackets {}
:
if (service == 'r') {
// your code
} else if (service == 'p') {
// your code
}
I personally always use them, but some people like to exclude them for one-liners.
The mistake in your code is that you are not opening curly braces for your if, else, and else if statements. If you do not include curly braces that indicate what your if statement wants to cover, the if statement will execute only the first line after it (assuming the if statement is true). Try adding curly braces to cover the scope of your if else statements and check if your code works.
You're just hung up on basic syntax
if(case) statements work without brackets if there's only one condition underneath
if(case)
dothis();
If you have multiple lines you need executed by the if statement you need to surround it in curly braces.
if(case){
dothis();
dothat();
}
This tells the program to call dothis(), and dothat() when the case is met.
I reccommend closing all of your if/else statements in braces as it helps you avoid this problem entirely.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.