Visual Studio 有人能告诉我我做错了什么吗?[关闭]

ndh0cuux  于 2023-03-24  发布在  其他
关注(0)|答案(4)|浏览(110)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

昨天关门了。
社区正在审查是否从昨天开始重新讨论这个问题。
Improve this question
enter image description here我是相当新的代码,但我有下面的代码,大部分工作,但我需要它说“你有{0}鸡蛋,等于{1}打和{2}鸡蛋剩余。”当它只有一个鸡蛋剩余和鸡蛋时,超过一个剩余。目前它只说“鸡蛋”剩余,我不知道如何改变这一点?

using System;
using System.Diagnostics.Eventing.Reader;

namespace Q2
{
    public class Q2
    {
        static void Main()
        {
            // Keep the following line intact 
            Console.WriteLine("===========================");

            // Insert your solution here.
            int totaleggs = 0;

            
            Console.Write("Enter the number of chickens: ");
            int Chickens = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < Chickens; i++)
            {
                
                Console.Write("Eggs: ");
                int egg = Convert.ToInt32(Console.ReadLine());

                if (egg > 0)
                {
                    totaleggs += egg;
                    Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} eggs left over.", totaleggs, (int)(totaleggs / 12), totaleggs % 12);
                    Console.ReadLine();
                }
                else if (totaleggs % 12 ==1)
                {
                    Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} egg left over.", totaleggs, (int)(totaleggs / 12),  totaleggs % 12);
                    Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} eggs left over.", totaleggs, (int)(totaleggs / 12), totaleggs % 12);
                    Console.ReadLine();
                }
            }
            
            // Keep the following lines intact
            Console.WriteLine("===========================");
        }
    }
}
w8f9ii69

w8f9ii691#

在你的代码中,你的第一个if子句是这样的:

if (egg > 0)

这意味着,只要egg是一个非零的正整数,那么您就执行iff***的主体并忽略它后面的所有else语句***。
您需要在egg > 0条件之前检查totaleggs % 12 == 1条件。
你的for循环需要看起来像这样:

for (int i = 0; i < Chickens; i++)
{
    Console.Write("Eggs: ");
    int egg = Convert.ToInt32(Console.ReadLine());
    totaleggs += egg;

    if (totaleggs % 12 == 1)
    {
        Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} egg left over.", totaleggs, totaleggs / 12, totaleggs % 12);
        Console.ReadLine();
    }
    else
    {
        Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} eggs left over.", totaleggs, totaleggs / 12, totaleggs % 12);
        Console.ReadLine();
    }
}

旁注:你不需要写(int)(totaleggs / 12),因为totaleggs / 12做的是完全相同的事情。
如果我写这篇文章,我会做一些更像这样的事情:

int totaleggs = 0;

int ReadNumber()
{
    while (true)
    {
        string input = Console.ReadLine();
        if (int.TryParse(input, out int result) && result > 0)
        {
            return result;
        }
        Console.WriteLine("Not a valid number. Please try again.");
    }
}

Console.Write("Enter the number of chickens: ");
int chickens = ReadNumber();

for (int i = 0; i < chickens; i++)
{
    Console.Write("Eggs: ");
    int egg = ReadNumber();
    
    totaleggs += egg;

    int dozens = totaleggs / 12;
    int singles = totaleggs % 12;
    
    string singles_text =
        singles == 1
        ? "1 egg"
        : $"{singles} eggs";

    Console.WriteLine($"You have {totaleggs} eggs which equals {dozens} dozen and {singles_text} left over.");
    Console.ReadLine();
}
kqhtkvqz

kqhtkvqz2#

“* 目前它只写着‘鸡蛋’剩下,我不知道如何改变这一点??*”
改变这种情况的一种方法是计算所有需要写入的内容,然后只写一次:

int egg = Convert.ToInt32(Console.ReadLine());
// proceed only if the chicken has at least 1 egg
if (egg > 0)
{
    totaleggs += egg;
    int dozens = totaleggs/12;
    int eggsLeft = totaleggs%12;
    // will add an "s" only if more than 1 egg left
    string plural = eggsLeft > 1 ? "s" : "";

    Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} egg{3} left over.", totaleggs, dozens, eggsLeft, plural);
    Console.ReadLine();
}

**注意:**这样,如果您需要更改句子,只需更改一次。

“谁能告诉我,我做错了什么?”
您错误地使用了if/else if,因为您只会在鸡的蛋数少于1个时才检查剩余的蛋数(* 即 * 如果第一个条件-egg > 0-是false)。
让代码正常工作的一种变化较小的方法是将else if部分 * 移到 * 第一个if中,* 即 * 更改以下内容:

if (egg > 0)
{
    totaleggs += egg;
    Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} eggs left over.", totaleggs, (int)(totaleggs / 12), totaleggs % 12);
    Console.ReadLine();
}
else if (totaleggs % 12 ==1)
{
    Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} egg left over.", totaleggs, (int)(totaleggs / 12),  totaleggs % 12);
    Console.ReadLine();
}
else
{
    Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} eggs left over.", totaleggs, (int)(totaleggs / 12), totaleggs % 12);
    Console.ReadLine();
}

为此:

if (egg > 0)
{
    totaleggs += egg;
    if (totaleggs % 12 ==1)
    {
        Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} egg left over.", totaleggs, (int)(totaleggs / 12),  totaleggs % 12);
        Console.ReadLine();
    }
    else
    {
        Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} eggs left over.", totaleggs, (int)(totaleggs / 12), totaleggs % 12);
        Console.ReadLine();
    }
}

或者,如果你仍然想在没有鸡蛋的情况下打印一些东西,只需将else if更改为if(仍然从第一个if删除打印):

if (egg > 0)
{
    totaleggs += egg;
    //Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} eggs left over.", totaleggs, (int)(totaleggs / 12), totaleggs % 12);
    //Console.ReadLine();
}
/* else */ if (totaleggs % 12 ==1)
{
    Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} egg left over.", totaleggs, (int)(totaleggs / 12),  totaleggs % 12);
    Console.ReadLine();
}
else
{
    Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} eggs left over.", totaleggs, (int)(totaleggs / 12), totaleggs % 12);
    Console.ReadLine();
}
sauutmhj

sauutmhj3#

如果你想得到这样的东西(据我所知)。

Enter the number of chickens: 2
Eggs: 13
You have 13 eggs which equals 1 dozen and 1 egg left over.

Eggs: 15
You have 28 eggs which equals 2 dozen and 4 eggs left over.

那么有几个选择。

选项1(最佳方式):

这是一句俏皮话,如果,可能看起来很难,但当理解。然后会爱他们。
学习链接:https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator
选项2是相同的,但“凌乱”的变种
代码:

Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} {3} left over.", totaleggs, (int)(totaleggs / 12), totaleggs % 12, totaleggs % 12 == 1 ? "egg" : "eggs");

备选案文2:

代码:

string word;

if (totaleggs % 12 == 1)
{
   word = "egg";
}    
else
{
   word = "eggs";
}
                        
Console.WriteLine("You have {0} eggs which equals {1} dozen and {2} {3} left over.", totaleggs, (int)(totaleggs / 12), totaleggs % 12, word);
Console.ReadLine();
agyaoht7

agyaoht74#

你可能希望for循环的内容是这样的:

for (int i = 0; i < Chickens; i++)
{
    Console.Write("Eggs: ");
    int egg = Convert.ToInt32(Console.ReadLine());

    totaleggs += egg;

    int eggs = totaleggs;
    int dozen = totaleggs / 12;
    int leftover = totaleggs % 12;

    string stotaleggs;
    if (eggs == 1)
        stotaleggs = "egg";
    else
        stotaleggs = "eggs";

    string sleftover;
    if (leftover == 1)
        sleftover = "egg";
    else
        sleftover = "eggs";

    Console.WriteLine("You have {0} {3} which equals {1} dozen and {2} {4} left over.", eggs, dozen, leftover, stotaleggs, sleftover);
    Console.ReadLine();
}

这是相当自我解释。
这可能是写在一个更优雅的方式,但你应该得到的想法。

相关问题