This is what i get该代码需要打印出2个最大数字,输入-999时,该代码需要停止。 我尝试了所有事情,但大多数时候我都获得了最大数量,但没有获得第二个最大数量。
#include <stdio.h>
int main ()
{
int x = 0;
int max = 0;
int max2 = 0;
int y = 0;
int flag = 0;
do
{
printf("Enter the number -999 to stop: ");
scanf("%d", &x);
if (x != -999)
{
if (x > max)
{
max = x;
max2 = y;
}
printf("Enter the number -999 to stop: ");
scanf("%d", &y);
if (y != -999)
{
if (y > max)
{
max = y;
max2 = x;
}
}
else
{
flag = 1;
}
}
else
{
flag = 1;
}
}
while (flag == 0);
printf("The max number is: %d\n", max);
printf("The second max number is: %d\n", max2);
return 0;
}
答案 0 :(得分:1)
Intent intent = new Intent("message_subject_intent");
intent.putExtra("name" , String.valueOf(messageSubject.getname()));
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
答案 1 :(得分:0)
您的代码中还有更多问题。
您有不必要的两个输入(x和y),然后错误地假设,当x > max
时y包含第二个最大值,而当y > max
时x包含第二个最大值。
正确的代码应如下所示:
int main()
{
int x = 0;
int max = 0;
int max2 = 0;
while (true)
{
printf("Enter the number (-999 to stop): ");
scanf("%d", &x);
if (x == -999)
{
break;
}
if (x > max)
{
max2 = max;
max = x;
}
else if (x > max2)
{
max2 = x;
}
}
printf("\n\nThe max number is: %d\n", max);
printf("The second max number is: %d\n", max2);
return 0;
}