对不正确的输入使用正确的循环

本文关键字:循环 不正确 输入 | 更新日期: 2023-09-27 18:20:35

我正试图进入一个循环,以阻止区域数量低于0或高于5,我曾尝试进入一个do while循环,但即使输入了错误的输入,它仍然会执行下一个代码块。我已经用循环注释了该部分。非常感谢您的帮助,谢谢。

const int MaxItems = 5; // max size of array needed 
double[] itemCosts = new double[MaxItems];
int jobType; // valid values are 1=normal, 2=waterproofing, 3=skim 
int nAreas;
int i;
string customer;
double totalCost = 0.0;
double averageCost;
const double discountPrice = 800; // price at which discount available 
const double discountRate = 0.1; // discount rate 
const double setupCostPerArea = 30.00;
// cost of moving furniture, carpets etc.     
double discount, discountedTotal; // discount amount and discounted total 
double width, height; // width and height of plaster area 
double[] basePrices = { 0, 35.0, 30.0, 20.0 };  // added 0 as index placeholder, so 35 can be selected using 1, and so forth.
// prices per square metre for standard, plasterboard and skim, and skim only 
Console.Write("enter name of customer: ");
customer = Console.ReadLine();
Console.Write("enter number of plaster areas to quote for: ");
nAreas = Convert.ToInt32(Console.ReadLine());
do
{
    Console.WriteLine("Please Enter numbers of rooms between 1 and 5 only!!!!");
} while (nAreas < 1 && nAreas > 5); // loop
for (i = 0; i < nAreas; i++)
{
    Console.WriteLine("Data entry for area {0}", i + 1);
    Console.Write("enter 1 (standard), 2 (plasterboard and skim) or 3 (skim only): ");
    jobType = Convert.ToInt32(Console.ReadLine());
    Console.Write("enter width of area {0} in metres: ", i + 1);
    width = Convert.ToDouble(Console.ReadLine());
    Console.Write("enter height of area {0} in metres: ", i + 1);
    height = Convert.ToDouble(Console.ReadLine());
    // add base area cost to price based on area and type of plaster 
    itemCosts[i] = setupCostPerArea + (basePrices[jobType] * (width * height));
    totalCost += itemCosts[i];
}
averageCost = totalCost / nAreas;
// main report heading 
Console.WriteLine("'n");
Console.WriteLine("Plasterers R US Job Costing Report");
Console.WriteLine("===================================");
Console.WriteLine("'nName of customer: {0}'n", customer);
Console.WriteLine("No. of plaster areas: {0}", nAreas);
Console.WriteLine("Average Cost per area £{0:0.00}", averageCost);
// output subheadings 
Console.WriteLine("Area'tCost'tDifference From Average");
for (i = 0; i < nAreas; i++)
{
    Console.Write("{0}'t", i + 1);
    Console.Write("£{0:0.00}'t", itemCosts[i]);
    Console.WriteLine("£{0:0.00}", itemCosts[i] - averageCost);
}
Console.WriteLine("Total Cost £{0:0.00}", totalCost);
if (totalCost > discountPrice)
{
    discount = totalCost * discountRate;
    discountedTotal = totalCost - discount;
    Console.WriteLine(" - Discount of £{0:0.00}", discount);
    Console.WriteLine(" Discounted Total £{0:0.00}", discountedTotal);
}
Console.WriteLine("press enter to continue");
Console.ReadLine();

对不正确的输入使用正确的循环

do
{
   Console.Write("enter number of plaster areas to quote for: ");
   nAreas = Convert.ToInt32(Console.ReadLine());
   if(nAreas < 1 || nAreas > 5)
     Console.WriteLine("Please Enter numbers of rooms between 1 and 5 only!!!!");
} while (nAreas < 1 || nAreas > 5); // loop

两个问题:

  1. 您没有在循环中请求新的输入
  2. 您的循环存在错误的退出条件-非法的are数将小于1或大于5

调整两者:

do
{
   Console.Write("enter number of plaster areas to quote for: ");
   nAreas = Convert.ToInt32(Console.ReadLine());
   if(nAreas < 1 || nAreas > 5)
      Console.WriteLine("Please Enter numbers of rooms between 1 and 5 only!!!!");
} while (nAreas < 1 || nAreas > 5); // loop