我正在编写显示输入数字最大的代码,但在运行时会返回一个看似随机的数字,我的代码如下:
#include <stdio.h>
int main() {
int largest;
int nvalue;
int no1, no2, no3;
printf("Enter value of n:");
scanf("%d\n", &nvalue);
printf ("Number1:");
scanf("%d\n", &no1);
printf("Number2:");
scanf("%d\n", &no2);
printf("Number3:");
scanf("%d\n", &no3);
largest == nvalue;
if (no1 > nvalue) {
printf ("The largest number is:%d\n", &no1); }
else if (no2 > nvalue) {
printf("The largest number is %d\n", &no2); }
else if (no3 > nvalue) {
printf("The largest number is %d\n", &no3); }
else {
printf("The largest number is %d\n", &largest); }
return 0;
}
运行时返回:
Enter value of n:3
3
Number1:34
Number2:89
Number3:54
你能告诉我我做错了什么吗? 谢谢 最大的数字是-1536115152
答案 0 :(得分:1)
在&
中避免使用printf
,它会返回该变量的地址。
答案 1 :(得分:1)
代码应格式良好,例如花括号({})内的代码应正确缩进(这样可以更容易地阅读代码)。
#include <stdio.h>
int main(int argc, char *argv[])
{
变量应该是描述性的(如果其他人正在阅读您的代码,则会很有帮助。)
int largest = 0;
int num1 = 0, num2 = 0, num3 = 0;
除非您希望scanf将换行符作为输入的一部分,否则不需要换行符('\ n)。
printf("Enter the first number : ");
scanf("%d", &num1);
printf("Enter the second number : ");
scanf("%d", &num2);
printf("Enter the third number : ");
scanf("%d", &num3);
这是一个更清洁,更简单的检查,以查看哪个是最大的数字。 (摘自上面的Rishav Choudhuri's代码)
if (num1 > num2 && num1 > num3)
{
largest = num1;
}
else if (num2 > num1 && num2 > num3)
{
largest = num2;
}
else
{
largest = num3;
}
printf("The largest number is : %d", largest);
return 0;
}
我还建议您阅读本书The C Programming Language 2nd Edition并在线阅读一些教程
还要确保编写大量代码:D
答案 2 :(得分:0)
为什么要写这一行 <form action = "" method = "POST" enctype = "multipart/form-data">
<b>Type/pad</b><br>
<select name="path">
<option value="magieweb/images/publicroom">Publieke Kamer</option>
<option value="magieweb/images/news">Nieuwsafbeelding</option>
<option value="app/tpl/skins/Habbo/swf/c_images/album1584">Badges</option>
</select><div style="margin-bottom:5px;"></div>
<b>Afbeelding</b><input type = "file" name = "image" /> <br>
<input type = "submit"/> of <a href="ase-dash.php">Annuleren</a>
<br><br>
<ul style="border: 1px solid #2087A1; list-style-type: none; margin-right:40px;">
<li><strong style="color:#2087A1; margin-top:3px; margin-bottom:3px;">Uploadbestandinformatie</strong></li>
<li><strong>Bestandnaam:</strong> <?php echo $_FILES['image']['name']; ?>
<li><strong>Bestandgrootte:</strong> <?php echo $_FILES['image']['size']; ?>
<li><strong>Bestandtype:</strong> <?php echo $_FILES['image']['type'] ?>
</ul>
</form>
?
你的if-else if命令是完全错误的。
试试这段代码:
largest = nvalue;
答案 3 :(得分:0)
#include <stdio.h>
#include <limits.h>
int main(void) {
int largest = INT_MIN;
int n, value;
int i;
printf("Enter value of n:");//fflush(stdout);
scanf("%d", &n);
for(i = 1; i <= n; ++i){
printf ("Number%d:", i);//fflush(stdout);
if(1 != scanf("%d", &value)){
printf("invalid input!\n");
while(getchar() != '\n');//clear input
--i;
continue;
}
if(value > largest)
largest = value;
}
printf("The largest number is %d\n", largest);
return 0;
}