1
我正在写一个程序,通过6的10的if..else的if..else循环未正确执行
模量例如,如果要求用户输入然后一个三位数将每个数我输入619,我将收到的输出是275.
我唯一的问题是当我输入100,我收到1360,而不是像我应该的766。
这是我到目前为止有:
int main()
{
//declaring variables
int numbers, newNumbers, input;
int newInput = 0;
//User input
printf("Enter a three-digit number: ");
//reads input (%d) and stores it as "input" (&input)
scanf("%d", &input);
//check if input is 3 digits. If not, print message
if(input < 100 || input > 999)
{
printf("Invalid input, the number must be between 100 and 999 \n");
return 0;
}
//loops program until input is 0
while(input > 0)
{
//modulus 10
numbers = input % 10;
//adds 6 to input. Modulus 10
newNumbers = (numbers + 6) % 10;
//if input > 10, calculates new input
if(input > 100)
newInput = newNumbers;
else if(input > 10)
newInput = (newNumbers * 10) + newInput;
else
newInput = (newNumbers * 100) + newInput;
input /= 10;
}
//prints result
printf("Output: %d \n", newInput);
return 0;
}
尝试打印newNumbers ,newInput,并在循环内输入结果以查看每次值。 – Bek
而不是检查输入是否为3位数字,也可以使用scanf(“%3d”,&input);这样做用户也可以编写123456,但只有前三个数字将被存储 –
其实你有一个拒绝小于100的数字的if语句,那么你如何输入100作为输入。 – phantom