我正在尝试创建一个代码来确定电话帐单的费用。
到目前为止,这是我的代码
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);
}
}
我觉得我很亲密,但它一直给我这个错误
PhoneBill.java:41: error: 'else' without 'if'
else if (service == 'p')
PhoneBill.java:78: error: 'else' without 'if'
else if (service == 'p')
我有上述陈述的陈述,但它仍然告诉我,我没有。
我没看到什么?
答案 0 :(得分:0)
如果内部代码块超过一行,则需要在If Else语句中使用括号。我总是使用括号来提高代码的可读性。
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);
}
答案 1 :(得分:0)
如果您要在if/else if/else
语句下有多行,则需要使用大括号{}
:
if (service == 'r') {
// your code
} else if (service == 'p') {
// your code
}
我个人总是使用它们,但是有些人喜欢将它们排除在单行之外。
答案 2 :(得分:0)
您的代码中的错误是您没有为if,else和else if语句打开花括号。如果不包括指示if语句要覆盖的大括号,则if语句将仅执行其后的第一行(假设if语句为true)。尝试添加花括号以涵盖if else语句的范围,并检查代码是否有效。
答案 3 :(得分:0)
你只是挂了基本语法
if(case)语句如果
下面只有一个条件,则不带括号if(case)
dothis();
如果有多行需要if语句执行,则需要用大括号括起来。
if(case){
dothis();
dothat();
}
这告诉程序在满足大小写时调用dothis()和dothat()。
我建议关闭括号中的所有if / else语句,因为它可以帮助您完全避免这个问题。