我是计算机科学专业的一年级学生,目前正在学习编程。我们正在学习C编程语言,我无法弄清楚如何在代码末尾正确输入循环,以询问用户是否要继续。
这个简单的任务要求开发一个程序来确定员工的总薪酬(包括确定加班的“if”陈述)。正如您在我的代码中看到的那样,我认为我已经正确完成了。该任务继续指出,在我的程序结束时,我应该使用循环来询问用户是否要继续。
在课堂上,我们讨论了For循环和While循环,但我对如何正确实现此功能感到有点迷失。
我最初尝试做类似的事情......
printf("Would you like to continue? (1 = Yes, 2 = No) \n" );
scanf("%i", _____ ); While (_____ == 'y' || ______ == 'Y') {
}
但不确定要为输入(scanf)声明什么或者在while循环中放入什么。请帮忙。我的春假和没有校园辅导就在附近。谢谢!
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int main () {
double totalHours, rate, grossPay, overTime, overTimepay, otHours, grossPaywithOT;
//1. I began by asking user for total hours & getting input
printf("Enter your total hours worked : \n");
scanf("%lf", &totalHours);
//Now I'm using a selection statement to determine pay for overtime hours
if (totalHours > 40) {
//a. Inform user they have overtime hours
printf("You worked over 40 hours this period. \n");
//b. Ask how many hours over 40 they worked
printf("How many hours over 40 did you work? : \n");
scanf("%lf", &otHours);
//c. Ask the user for hourly rate
printf("What is your hourly rate? : \n");
scanf("%lf", &rate);
//d. Overtime Rate Calculation & Gross Pay Calculation
grossPay = totalHours * rate;
overTime = 1.5 * rate;
overTimepay = otHours * overTime;
grossPaywithOT = overTimepay + grossPay;
//e. Display overtime pay and Gross Pay
printf("Your overtime pay is %.02lf \n", overTimepay);
printf("Your total Gross Pay including overtime is %.02lf \n", grossPaywithOT);
} else {
//2. Ask the user for hourly rate
printf("What is your hourly rate? : \n");
//3. User input for hourly rate
scanf("%lf", &rate);
//4. Gross Pay Calculation
grossPay = totalHours * rate;
//5. Display grossPay
printf("Your Gross Pay is %.02lf \n", grossPay);
}
}
答案 0 :(得分:1)
我会:
Integer.compare(p1.getAge(), p2.getAge()) // java 7+
/ A
答案 1 :(得分:0)
do
{
// loop until they decide to stop
// put the code to do your normal stuff here
int i = 0;
do
{
// loop until they input a 1 or 2
printf("\nWould you like to continue? (1 = Yes, 2 = No) \n" );
scanf("%i", &i);
} while ((i != 1) && (i != 2));
} while (i == 1);
答案 2 :(得分:0)
scanf()
不会写入变量,而是写入内存地址。
因此,在_____
代码中,您应该编写一个指向内存地址的指针。几个例子:
int * intPointer;
scanf("%i", intPointer);
或者:
int integer;
scanf("%i", &integer);
请注意,您在格式化字符串中键入了"%i"
。 %i
告诉scanf()
用户输入的字符串应该被解析为整数。你想把它作为一个角色来阅读。 C格式化函数(scanf()
中的最终 f 表示对字符使用%c
。