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($"Your Apex Card is {ApexCard}");
//Author Jonaven Gray 06/21
}
}
}
You have incorrect apex calculations using current logic.
Your old logic is (commented and working):
So you call
Pyramidmethod for cards and then "reset" cards to0.Your current code does different thing - you firstly check
cycleand then callPyramidif condition is met (note that in previous version you callPyramidalways). So youApexmethod should be like this:The
Pyramidmethod is correct (bothternaryandif-elseversions).