短手/三元操作员如果陈述不起作用,逻辑对我来说是正确的,所以我需要另一个眼睛来检查一下

发布于 2025-02-09 15:35:50 字数 2512 浏览 2 评论 0原文

因此,我为我的课程创建的程序是Pascals Triangle,该方法采用三角形的顶点并显示为IT。评论rn的旧代码确实有效并且存在零问题,但是由于我想尝试并始终提高我的编码技巧,因此我尝试使其更清洁,用短手(如果陈述),如果您看到我遇到了什么问题id感谢您让我知道,获取正确顶点的示例数据是 4,1,7,9,2 = Apex(7),但使用新代码,它给了我6

using System;

namespace Lab4
{
    class Program
    {
        // apex will add each pairs of cards, if over 9 wtvr remaining is thats the new sum
        static int Pyramid(int x, int y)
        {
            x = (x + y > 9) ? (x + y) - 9 : x + y;
            return x;
        }
        static int Apex(int Card1,int Card2,int Card3,int Card4,int Card5)
        {
            int x = 0;
            int cycle = 0;
            while (cycle < 5 ) // tells it how many cycles it needs to get to the apex
            {
                
                //having a cycle counter allows the if statment to know when to cancel the last card out ,from 5-2
       
                Card1 = (cycle == 5) ? Card1     : Pyramid(Card1, Card2);
                Card2 = (cycle == 4) ? 0 : Pyramid(Card2, Card3);
                Card3 = (cycle == 3) ? 0 : Pyramid(Card3, Card4);
                Card4 = (cycle == 2) ? 0 : Pyramid(Card4, Card5);
                Card5 = 0;
               
                cycle++;
                
            }
            x = Card1;
            return x;
        }
        static int PickCard() //method is used to let user input card and check to see if card is a correct input 1-9
        {
           int x=int.Parse(Console.ReadLine()); //allows user to input their card choice
            while (x < 1 || x > 9)
            {
                Console.WriteLine("Please Only Choose Cards From 1-9");
                x = int.Parse(Console.ReadLine());
            }
            Console.WriteLine("Please Choose Next Card"); // tells user to choose next card, works since its after the while loop verifies their og input
            return x;
        }
        static void Main(string[] args)
        {
            int ApexCard;
            int Card1,Card2,Card3, Card4, Card5;
            Console.WriteLine("Please Choose 5 Cards Going Left To Right From 1-9");
            Card1 = PickCard();
            Card2 = PickCard();
            Card3 = PickCard();
            Card4 = PickCard();
            Card5 = PickCard();
            ApexCard = Apex(Card1, Card2, Card3, Card4, Card5);
            Console.WriteLine("");
            Console.WriteLine($"Your Apex Card is {ApexCard}");
        //Author Jonaven Gray 06/21
        }
    }
}

So what the program I'm creating for my course is pascals triangle and the method takes the apex of the triangle and display's it. The old code that is commented out rn does work and had zero issues, but since i wanna try and always advance my coding skills, i tried to make it cleaner and smaller with short hand if statements, if you see what the issue i am having is id appreciate you letting me know, the sample data for getting the correct apex is
4,1,7,9,2 = Apex(7) but with the new code its giving me 6

using System;

namespace Lab4
{
    class Program
    {
        // apex will add each pairs of cards, if over 9 wtvr remaining is thats the new sum
        static int Pyramid(int x, int y)
        {
            x = (x + y > 9) ? (x + y) - 9 : x + y;
            return x;
        }
        static int Apex(int Card1,int Card2,int Card3,int Card4,int Card5)
        {
            int x = 0;
            int cycle = 0;
            while (cycle < 5 ) // tells it how many cycles it needs to get to the apex
            {
                
                //having a cycle counter allows the if statment to know when to cancel the last card out ,from 5-2
       
                Card1 = (cycle == 5) ? Card1     : Pyramid(Card1, Card2);
                Card2 = (cycle == 4) ? 0 : Pyramid(Card2, Card3);
                Card3 = (cycle == 3) ? 0 : Pyramid(Card3, Card4);
                Card4 = (cycle == 2) ? 0 : Pyramid(Card4, Card5);
                Card5 = 0;
               
                cycle++;
                
            }
            x = Card1;
            return x;
        }
        static int PickCard() //method is used to let user input card and check to see if card is a correct input 1-9
        {
           int x=int.Parse(Console.ReadLine()); //allows user to input their card choice
            while (x < 1 || x > 9)
            {
                Console.WriteLine("Please Only Choose Cards From 1-9");
                x = int.Parse(Console.ReadLine());
            }
            Console.WriteLine("Please Choose Next Card"); // tells user to choose next card, works since its after the while loop verifies their og input
            return x;
        }
        static void Main(string[] args)
        {
            int ApexCard;
            int Card1,Card2,Card3, Card4, Card5;
            Console.WriteLine("Please Choose 5 Cards Going Left To Right From 1-9");
            Card1 = PickCard();
            Card2 = PickCard();
            Card3 = PickCard();
            Card4 = PickCard();
            Card5 = PickCard();
            ApexCard = Apex(Card1, Card2, Card3, Card4, Card5);
            Console.WriteLine("");
            Console.WriteLine(
quot;Your Apex Card is {ApexCard}");
        //Author Jonaven Gray 06/21
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手心的温暖 2025-02-16 15:35:50

使用当前逻辑,您的APEX计算错误。

您的旧逻辑是(注释和工作):

static int Apex(int Card1,int Card2,int Card3,int Card4,int Card5)
{
    int x = 0;
    int cycle = 0;
    while (cycle < 5 )
    {
        Card1 = Pyramid(Card1, Card2);
        Card2 = Pyramid(Card2, Card3);
        Card3 = Pyramid(Card3, Card4);
        Card4 = Pyramid(Card4, Card5);
        Card5 = (cycle == 1) ? Card5 = 0 : Card5=0;
        
        cycle++;
        
        if (cycle==1) 
        {            
            Card5 = 0;
        }
        else if (cycle == 2)
        {
            Card4 = 0;
        }
        else if (cycle == 3)
        {
            Card3 = 0;
        }
        else if (cycle == 4)
        {
            Card2 = 0;
        }
        else if (cycle == 5)
        {
            x=Card1;
        }
    }
    x = Card1;
    return x;
}

因此,您调用pyramid卡片的方法,然后“将”卡“重置”到0

您当前的代码做不同的事情 - 您首先检查cycle,然后调用金字塔如果满足条件(请注意,在以前的版本中,您调用Pyramid始终)。因此,您APEX方法应该是这样的:

static int Apex(int Card1,int Card2,int Card3,int Card4,int Card5)
{
    int x = 0;
    int cycle = 0;
    while (cycle < 5 )
    {
        Card1 = Pyramid(Card1, Card2);
        Card2 = Pyramid(Card2, Card3);
        Card3 = Pyramid(Card3, Card4);
        Card4 = Pyramid(Card4, Card5);          
        
        cycle++;
        
        Card2 = (cycle == 4) ? 0 : Card2;
        Card3 = (cycle == 3) ? 0 : Card3;
        Card4 = (cycle == 2) ? 0 : Card4;
        Card5 = (cycle == 1) ? 0 : Card5;
    }
    x = Card1;
    return x;
}

Pyramid方法是正确的(ternary and and if-else wife 版本)。

You have incorrect apex calculations using current logic.

Your old logic is (commented and working):

static int Apex(int Card1,int Card2,int Card3,int Card4,int Card5)
{
    int x = 0;
    int cycle = 0;
    while (cycle < 5 )
    {
        Card1 = Pyramid(Card1, Card2);
        Card2 = Pyramid(Card2, Card3);
        Card3 = Pyramid(Card3, Card4);
        Card4 = Pyramid(Card4, Card5);
        Card5 = (cycle == 1) ? Card5 = 0 : Card5=0;
        
        cycle++;
        
        if (cycle==1) 
        {            
            Card5 = 0;
        }
        else if (cycle == 2)
        {
            Card4 = 0;
        }
        else if (cycle == 3)
        {
            Card3 = 0;
        }
        else if (cycle == 4)
        {
            Card2 = 0;
        }
        else if (cycle == 5)
        {
            x=Card1;
        }
    }
    x = Card1;
    return x;
}

So you call Pyramid method for cards and then "reset" cards to 0.

Your current code does different thing - you firstly check cycle and then call Pyramid if condition is met (note that in previous version you call Pyramid always). So you Apex method should be like this:

static int Apex(int Card1,int Card2,int Card3,int Card4,int Card5)
{
    int x = 0;
    int cycle = 0;
    while (cycle < 5 )
    {
        Card1 = Pyramid(Card1, Card2);
        Card2 = Pyramid(Card2, Card3);
        Card3 = Pyramid(Card3, Card4);
        Card4 = Pyramid(Card4, Card5);          
        
        cycle++;
        
        Card2 = (cycle == 4) ? 0 : Card2;
        Card3 = (cycle == 3) ? 0 : Card3;
        Card4 = (cycle == 2) ? 0 : Card4;
        Card5 = (cycle == 1) ? 0 : Card5;
    }
    x = Card1;
    return x;
}

The Pyramid method is correct (both ternary and if-else versions).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文