本文介绍了输出显示为零...没有错误..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! / * 评估指数* / #include < stdio.h > #include < conio.h > int main() { clrscr(); int i,exp; float tot = 1 ,num; printf( 输入应该引用的数字和指数:) ; scanf( %f,%f,& num,& exp) ; for (i = 1 ; i< = exp; i ++){ TOT = TOT * NUM; } printf( 所需的指数值为%f ,TOT); getch(); return 0 ; } 解决方案 scanf 函数的第二个参数类型为 int 但是你使用 float %f格式说明符。 您必须使用%d int 格式说明符: scanf(%f,%d,& num,& exp); 现在来吧! 这是你今天的第二个问题,它们都是相当简单的事情,可以通过调试器进行快速检查。 SO,让我们尝试调试吧? 使用调试器:在线上放一个断点: scanf( %f,%f,& num,& exp) ; 并运行你的程序。 当它停止时,单步调试器并输入你的两个数字:2,3将做。 Whe你按ENTER键,你将回到调试器。 所以 - 看看你输入的两个数字。 的价值是多少? NUM ?对我来说,它是2.0000000 exp 的价值是多少?对我而言,它是1077936128 现在,这不对! 所以看看你的代码,看看你是否能看出为什么它非常不准确! 两个变量 num 和 exp 之间有什么区别以及你如何使用他们? 因为 exp 是一个 int 但是你把它读作一个 float 更改 scanf( %f,%f,& num,& exp); to scanf( %F,%d,试验#,&安培; EXP); /*to evaluate exponent*/#include<stdio.h>#include<conio.h>int main(){ clrscr(); int i,exp; float tot=1,num; printf("enter the number and the exponent to which it should be raised:"); scanf("%f,%f",&num,&exp); for(i=1;i<=exp;i++){ tot=tot*num; } printf("the required value of the exponent is %f",tot); getch(); return 0;}[Edit: Fixed code formatting, added indention, added some line feeds] 解决方案 The second argument to your scanf function is of type int but you use the float "%f" format specifier.You must use the "%d" int format specifier:scanf("%f,%d",&num,&exp);Come on now!This is your second question today, and they are both fairly simple things that a quick check with a debugger would have found.SO, lets try and debug it shall we?Use the debugger: put a breakpoint on the line:scanf("%f,%f",&num,&exp);And run your program.When it stops, single step the debugger and enter your two numbers: "2,3" will do.When you press ENTER, you will be back to the debugger.So - look at the two numbers you just entered.What's the value of num? For me, it's 2.0000000What's the value of exp? For me it's 1077936128Now, that's not right!So look at your code, and see if you can see why it's wildly inaccurate!What is the difference between the two variables num and exp and how you use them?Because exp is an int but you read it as a floatChangescanf("%f,%f",&num,&exp);toscanf("%f,%d",&num,&exp); 这篇关于输出显示为零...没有错误..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-01 16:31