好吧,我已经编辑了一些代码,所以现在的问题是它如何计算正确的最终总数?我正在处理常规服务,我希望它先减去50分钟的免费分钟,然后添加用户过度使用的额外分钟数,然后将额外的分钟数乘以$ 0.20并将其计入最终总数。 如果我有任何更多的错误,请告诉我。我知道我犯了很多错误,对不起! 我有一个项目要做,这是请求的功能列表:
提示用户输入帐号,服务代码以及使用服务的分钟数。
常规:值为“R”和“r”:
高级:值为“P”和“p”:
$ 25.00 /月加:
如果使用任何其他字符,则显示错误消息
这是我的代码:
#include <iostream>
using namespace std;
int main()
{
// VARIABLES //
int accnum, minnum, minnumm;
double total, finaltotal, grandtotal, finaltotall;
char scode;
cout << "Hello, thank you for paying your phone bill." << ends;
cout << endl;
cout << endl;
cout << "Please enter your account number: ";
cin >> accnum ; //Enter some number
cout << "Please enter your service code (r as regular service or p for premium service): ";
cin >> scode ; //Enter R or r for regular service and P or p for premium service
////////////// THIS IS FOR REGULAR SERVICE //////////////
if (scode == 'R' || scode == 'r') { // Values for Regular service
cout << "This service provides 50 minutes for phone calls for 50 minutes for free for $10.00 a month, and charge $0.20 every minute over 50 minutes." << endl;
cout << "How many minutes have you used up?: ";
cin >> minnum;
if (minnum > 50) {
total = minnum * .20;
finaltotal = total + 10;
cout << "You have made phone calls over 50 minutes, your total will be " << finaltotal << "." << endl;
cout << "Your account number is " << accnum << " , with a Regular service. You have used " << minnum << "/50 minutes. Your total is $" << finaltotal << " . Thank you for your time." << endl; //Displays the acc. #, type of service, # of min the phone service used, and amount due from user
cout << endl;
} else {
cout << "You have not made phone calls over 50 minutes, your total will be $" << finaltotal << "." << endl;
cout << "Your account number is " << accnum << " , with a Regular service. You have used " << minnum << "/50 minutes. Your total is $" << finaltotal << " . Thank you for your time." << endl; //Displays the acc. #, type of service, # of min the phone service used, and amount due from user
}
////////////// THIS IS FOR PREMIUM SERVICE //////////////
} else if (scode == 'P' || scode == 'p') { //Values for Premium service
cout << "This service provides your first 75 minutes phone calls from 6:00 - 18:00, and charge $0.10 for every minute over. Your first 100 minutes for phone calls from 18:00 - 6:00 are free, and charge $0.05 for every minute over." << endl;
cout << "How many minutes have you used up between 6:00 - 18:00? "; //Time between 6am - 6pm
cin >> minnumm;
if (minnumm > 75) { //If # of minutes is over 75
total = minnumm * .10; //Then it multiplies total * $.10
finaltotall = total + 25; //Then adds the $10/month
cout << "You have made phone calls over 75 minutes, your total will be $" << finaltotall << "." << endl;
} else {
cout << "How many minutes have you used up between 18:00 - 6:00? "; } //Time between 6pm - 6am
cin >> minnum;
if (minnum > 100) { //If # of minutes is over 100
total = minnum * .05; //Then it multiplies total * $.05
finaltotal = total + 25;
//Then adds the $25/month
cout << "You have made phone calls over 100 minutes, your total will be $" << finaltotal << "." << endl;
grandtotal = finaltotall + finaltotal; //Calculates both the 6am-6pm and 6pm-6am totals together
cout << "Your account number is " << accnum << " , with a Premium service. You have used " << minnumm << "/75 minutes from 6:00-18:00. You have used" << minnum << "/100 from 18:00-6:00. Your total is $" << grandtotal << " . Thank you for your time." << endl; //Displays the acc. #, type of service, # of min the phone service used, and amount due from user
} else {
cout << "You have not made phone calls over 100 minutes, your total will be $" << finaltotal << "." << endl;
cout << "Your account number is " << accnum << " , with a Premium service. You have used " << minnumm << "/75 minutes from 6:00-18:00. You have used " << minnum << "/100 from 18:00-6:00. Your total is " << grandtotal << " . Thank you for your time." << endl;
cout << endl;
cout << endl; } //Displays the acc. #, type of service, # of min the phone service used, and amount due from user
////////////// THIS IS IF THEY TYPED IN ANY LETTER OTHER THAN R, r, P, p //////////////
} else { //If user doesn't type P or R, then this is error message
cout << "Sorry, that service does not exist. Please try again." << endl;
cout << endl; }
system("pause");
return 0;
}
答案 0 :(得分:1)
据我所知(后续答案中的人可能会发现更多错误),明显的问题是您使用了 AND 运算符而不是 OR 运营商。
本声明:
class Test
{
Test()
{
super();
}
public static void main(String[] args)
{
Test t = new Test();
}
}
应该是:
if (scode == 'R' && scode == 'r') { // Values for Regular service
同样,这句话:
if (scode == 'R' || scode == 'r') { // Values for Regular service
应该是:
if (scode == 'P' && scode == 'p') { //Values for Premium service
变量不能同时匹配两个不同的值,因此在使用if (scode == 'P' || scode == 'p') { //Values for Premium service
而不是if
时,永远不会输入&&
块。