gpt4 book ai didi

c# - 控制台应用程序 C# 循环

转载 作者:行者123 更新时间:2023-11-30 14:01:44 24 4
gpt4 key购买 nike

大家好,我正在学习成为一名程序员,这是我正在从事的一个项目。控制台基本上接受用户输入(直径)并告诉您将获得多少披萨片以及每片的面积。在告诉他们每个切片和面积之后,它必须提示新的输入。切片是根据范围确定的,例如 12 - 20"的直径 = 8 个切片。用户必须输入 12"- 36"之间的范围,否则会收到错误消息。此外,如果用户输入的直径为 36,它必须以 4 的减量显示所有其他切片,因此 24 、 20 、 16 、 12 、 8 。但是如果直径最终落在 8 个切片以下,它必须只显示那个。因此 12 个切片将显示 8 个切片并且12 片。

最后,我们正在学习循环,我们将在这个项目中使用 do while 循环和 for 循环。

这是我的,但它不起作用。

        //DECLARATIONS
double circleArea; // area of pizza
double diameter; // diameter of the pizza
double areaOfSlice; // area of the slices
double radius; // half the diameter of the pizza
double slices = 0; // number of pizza slices
string message = ""; // a string to hold a message to the user
const int DIAMETER_RANGE_MASSIVE = 36;
const int DIAMETER_RANGE_EXTRA_LARGE = 30;
const int DIAMETER_RANGE_LARGE = 24;
const int DIAMETER_RANGE_MED = 16;
const int DIAMETER_RANGE_LOW = 12; // Low end on range scale for diameter
const int SLICES_LOW_DIAMETER = 8; // number of pizza slices based on diameter
const int SLICES_MID_DIAMETER = 12; // number of pizza slices based on diameter
const int SLICES_HIGH_DIAMETER = 16; // number of pizza slices based on diameter
const int SLICES_GIANT_DIAMETER = 24; // number of pizza slices based on diameter
bool needInput = true;
const int END_PROGRAM = 0;




// INPUT
// Prompt for and get keyboard input

Console.Write("Please enter the diameter of your pizza (0 to end program): "); // get user to input diameter
diameter = Convert.ToDouble(Console.ReadLine()); // read a line of text (string) from the keyboard,
// convert that string to an double,
// assign the resulting value to diameter

// PROCESSING
// determine if diameter meets requirements of 12" to 36"
// if does not meet requirements show error message and have user enter in new diameter
do
{
if (diameter < DIAMETER_RANGE_LOW || diameter > DIAMETER_RANGE_MASSIVE)

{
message = "\nENTRY ERROR";
message += "\nPizza must have a diameter in the range of 12” to 36” inclusive!";
message += "\nPlease try again.";
}



else {
needInput = false;
// determine the number of slices based on diameter


if (diameter >= DIAMETER_RANGE_LOW && diameter < DIAMETER_RANGE_MED)
{
slices = (SLICES_LOW_DIAMETER);
}
else if (diameter < DIAMETER_RANGE_LARGE)
{
slices = (SLICES_MID_DIAMETER);
}
else if (diameter < DIAMETER_RANGE_EXTRA_LARGE)
{
slices = (SLICES_HIGH_DIAMETER);
}
else
{
slices = (SLICES_GIANT_DIAMETER);
}

Console.Clear(); // clears console to show new output lines
//OUTPUT
for (int slicesAddFour = 8; slicesAddFour <=slices; slicesAddFour+=4) // for each slices
{
// determine the area of the slices
radius = diameter / 2; // uses diameter to get radius
circleArea = Math.PI * Math.Pow(radius, 2); // uses math class to calculate circle area
areaOfSlice = Math.Round((circleArea / slices), 2); // divides circle area by slices, takes the result of above calculation and rounds

Console.WriteLine("\nA {0}\" Pizza diameter: {0}\".", diameter);
message +=("\n==============================================");
Console.WriteLine("\nCut in {0} slices results in an area of {1}\" per slice.",areaOfSlice,slices);

}


} //end of else

message = ("\nPlease enter the diameter of your pizza (0 to end program)");
needInput = true;
} while (diameter != END_PROGRAM && needInput);
}
}

最佳答案

你的问题是你读入了一次数据,然后继续循环而没有得到任何新数据。如果你移动

Console.Write("Please enter the diameter of your pizza (0 to end program): ");  // get user to input diameter
diameter = Convert.ToDouble(Console.ReadLine()); // read a line of text (string) from the keyboard,

在循环内部,它应该可以解决您的问题

关于c# - 控制台应用程序 C# 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7825221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com