gpt4 book ai didi

c - 找出整数

转载 作者:太空宇宙 更新时间:2023-11-04 06:29:24 25 4
gpt4 key购买 nike

我试图让这段代码在有人输入负数甚至整数时给出错误。我有一个负整数,但我似乎找不到让系统在输入偶数时给出错误消息的方法。

#include <stdio.h>

int main(void){

int size, i, j, spaces, stars, true, false;


printf("How many rows do you want?\n"); //ask the user how many rows they want
scanf("%d", &size);

//this check to make sure a positive, odd integer is entered
if(size <= 0)
printf("sorry, but you need to input a positive, odd integer.\n");


int mid = size/2 +1; //this is to find the midpoint of the diamond where you will start decrementing

//menu options


//this is the for loop that will create your rows
for(i = 1; i <= size; i++){
if(i < mid){
spaces = mid - i;
stars = size - (spaces*2);
}
if(i > mid){
spaces = i - mid;
stars = size - (spaces*2);
}
if (i == mid){
spaces = 0;
stars = size;
}

for(j = 0; j < spaces; j++)
printf(" ");
for(j = 0; j < stars; j++)
printf("*");

printf("\n");

}
return 0;
}

最佳答案

if(size % 2 == 0)
printf("sorry, even integer not allowed.\n");

关于c - 找出整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22361052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com