【发布时间】:2022-11-15 12:01:14
【问题描述】:
//This my code
#include <stdio.h>
#include <conio.h>
int processChoice()
{
int choice = -1; //I need to execute this code without using any variable?
printf("\nMake a Choice (1, 2, 3 or 0): ");
scanf("%d",&choice);
printf("%d",choice);
switch(choice)
{
case 0:
printf("\nExiting...\n");
break;
case 1:
printf("\nDrawing rectangle...\n");
break;
case 2:
printf("\nDrawing Right triangle...\n");
break;
case 3:
printf("\nDrawing isosceles triangle...\n");
break;
default:
printf("\n** Invalid Choice! **\n");
choice = -1;
}
return choice;
}
void showMenu()
{
printf("\nMenu:");
printf("\n1. Draw Rectangle");
printf("\n2. Draw Right triangle");
printf("\n3. Draw isosceles triangle");
printf("\n0. Exit program\n");
}
int main()
{
int x = -1;
do
{
showMenu();
}while(processChoice() != 0);
return 0;
}
/* That's my code here I used a variable "int Choice = -1;" I'm supposed to execute the same code without using any variable as per guidelines of my mentor. Please help me with this */
我期望在不使用任何变量的情况下执行相同的代码。
【问题讨论】:
-
一个代码没有任何variables 根本不是一个非常有趣的程序,它只是吐出一些信息。他们可能一直试图引导您在
switch声明中使用getch(),但总的来说我不同意这个建议。 -
但为什么?如果你想读取输入,那么你实际上需要将该输入存储在某个地方,比如在一个变量中。你的实际任务或练习是什么?为什么你的“导师”说你必须在没有变量的情况下使用它?应该解决的问题是什么?你能请你的“导师”澄清一下吗?
-
对于这段代码,你必须使用“变量”。也许你的意思是没有一个初始值设定项.也就是把
int Choice = -1;改成int Choice;? -
作为一个可能猜测,也许导师希望您在其他地方(可能在它自己的函数中)读取输入,然后将其作为参数传递给
processChoice函数?那仍然不会没有变量,因为参数是一个变量。 -
欢迎来到堆栈溢出。请阅读How to Ask。这是不是论坛也不是辅导服务,我们无法读懂您导师的想法。如果你不明白你的导师的想法,问你的导师.如果你认为这项任务没有意义,向你的导师解释原因.
标签: c switch-statement function-definition