#include <stdio.h>
int main (void) //starting entry of integer indicate not associated with any data types argument
{
int value1, value2, calculate = 0; //integer values
printf ("\nProgram should perform the following:");
printf("\n");
printf ("\nHave the user enter two different integer numbers to be divided by the program ");
{
printf ("\nIf the division of both numbers");
printf("\n");
printf ("\nresult with the remainder of zero,");
printf("\n");
printf ("\nby the second number.");
printf("\n");
printf ("Example: thirty-five divided by seven will have a remainder of zero");
printf ("\n");
}
printf ("Please enter the first integer number>>");
scanf ("%i", &value1);
printf ("Please enter the second integer>> ");
scanf ("%i", &value2);
if (value1 % value2 == 0 ) {
printf ("\n%i is evenly divisible by %i\n", value1, value2);
calculate = value1 / value2;
printf ("%i / %i = %i\n", value1, value2, calculate);
else
printf ("\n%i is not evenly divisible by %i\n", value1, value2);
return 0;
}
我撰寫的這段代碼作業正常。但我似乎無法找到使用哪個 else 或 else if 陳述句使代碼顯示錯誤:如果用戶輸入的第二個數字為零,則程式中允許零。
uj5u.com熱心網友回復:
如果你想為用戶引入這樣的訊息,你必須在用戶提供輸入后實作檢查數字的條件。可以這樣做:
#include <stdio.h>
int main(void) //starting entry of integer indicate not associated with any data types argument
{
int value1, value2, calculate = 0; //integer values
printf("\nProgram should perform the following:");
printf("\n");
printf("\nHave the user enter two different integer numbers to be divided by the program ");
printf("\nIf the division of both numbers");
printf("\n");
printf("\nresult with the remainder of zero,");
printf("\n");
printf("\nby the second number.");
printf("\n");
printf("Example: thirty-five divided by seven will have a remainder of zero");
printf("\n");
printf("Please enter the first integer number>>");
scanf("%i", &value1);
printf("Please enter the second integer>> ");
scanf("%i", &value2);
if (value1 == 0 && value2 != 0)
{
printf("\nA zero is allowed in the program if the second number that the user enters is zero");
return 0;
}
else
{
if (value1 % value2 == 0)
{
printf("\n%i is evenly divisible by %i\n", value1, value2);
calculate = value1 / value2;
printf("%i / %i = %i\n", value1, value2, calculate);
}
else
{
printf("\n%i is not evenly divisible by %i\n", value1, value2);
}
}
return 0;
}
uj5u.com熱心網友回復:
不要在 之后使用括號printf,它不會改變任何內容并%d在內部scanf和printf整數中使用。并if在else.之前關閉括號。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/351557.html
下一篇:為什么我在嘗試使用fin2.seekg(-2*sizeof(int),ios::cur)從末尾到中間讀取檔案時出現奇怪的錯誤;?沒有字串
