【问题标题】:Can anyone help me to reprogram this in such a way that, that I don't need to use any sort of variable?任何人都可以帮助我以不需要使用任何变量的方式重新编程吗?
【发布时间】: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


【解决方案1】:

也许你的导师意味着这个声明主要是

int x = -1;

未使用,应删除。

至于函数processChoice,那么在任何情况下您都需要输入用户的值。我看到使用函数 getchar 的以下方式在不使用变量的情况下编写函数的唯一可能性

int processChoice( void )
{
    printf("
Make a Choice (1, 2, 3 or 0): ");
    
    switch( getchar() )
    {
        case '0':
        printf("
Exiting...
");
        while ( getchar() != '
' );
        return 0;

        case '1':
        printf("
Drawing rectangle...
");
        while ( getchar() != '
' );
        return 1;

        case '2':
        printf("
Drawing Right triangle...
");
        while ( getchar() != '
' );
        return 2;

        case '3':
        printf("
Drawing isosceles triangle...
");
        while ( getchar() != '
' );
        return 3;
        
        default:
        printf("
** Invalid Choice! **
");
        while ( getchar() != '
' );
        return -1;
    }
}

【讨论】:

  • 我相信 Vlad 已经这样做了,因为他们看到/评论了我在看时所做的同样的事情。
  • @Rogue 我没有看链接。
  • 那么,将其视为您对int x 的怀疑是正确的确认。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-07
  • 1970-01-01
  • 2015-03-31
  • 1970-01-01
  • 2019-10-12
  • 2021-12-13
相关资源
最近更新 更多