短手/三元操作员如果陈述不起作用,逻辑对我来说是正确的,所以我需要另一个眼睛来检查一下
因此,我为我的课程创建的程序是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 is4,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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用当前逻辑,您的APEX计算错误。
您的旧逻辑是(注释和工作):
因此,您调用
pyramid
卡片的方法,然后“将”卡“重置”到0
。您当前的代码做不同的事情 - 您首先检查
cycle
,然后调用金字塔
如果满足条件(请注意,在以前的版本中,您调用Pyramid
始终)。因此,您APEX
方法应该是这样的:Pyramid
方法是正确的(ternary
and andif-else
wife 版本)。You have incorrect apex calculations using current logic.
Your old logic is (commented and working):
So you call
Pyramid
method for cards and then "reset" cards to0
.Your current code does different thing - you firstly check
cycle
and then callPyramid
if condition is met (note that in previous version you callPyramid
always). So youApex
method should be like this:The
Pyramid
method is correct (bothternary
andif-else
versions).