4
Answers

If - Else

Maha

Maha

13y
1.2k
1
In the following Program when zone1 or zone2 and quantity less or equal to 10 delivery is free. But Program is giving following wrong result. Please fix the error.

Zone 1
Quantity 9
Not free


using System;
namespace DemoORAndAND
{
class Program
{
static void Main(string[] args)
{
const int ZONE1 = 1, ZONE2 = 2;
const int LOWQUANTITY = 10;
int quantity;
int diliveryZone;

Console.WriteLine("Delivery is free for zone {0} or {1}", ZONE1, ZONE2);

Console.WriteLine("...when the number of boxes is less than {0}", LOWQUANTITY);

quantity = method("\nZone ");
diliveryZone = method("Quantity ");

if ((diliveryZone == ZONE1 || diliveryZone == ZONE2) && quantity <= LOWQUANTITY)
Console.WriteLine("Free");
else
Console.WriteLine("Not free");

Console.ReadKey();
}
public static int method(string s)
{
int num;

Console.Write(s);
num = Convert.ToInt32(Console.ReadLine());
return num;
}
}
}

Answers (4)
0
Maha

Maha

NA 0 172.7k 13y
Okey, I didn't notice the inside change. Thank you very much for your explanation.
0
Vulpes

Vulpes

NA 98.3k 1.5m 13y
This is the code I'm using:

using System;
namespace DemoORAndAND
{
  class Program
  {
    static void Main(string[] args)
    {
      const int ZONE1 = 1, ZONE2 = 2;
      const int LOWQUANTITY = 10;
      int quantity;
      int diliveryZone;

      Console.WriteLine("Delivery is free for zone {0} or {1}", ZONE1, ZONE2);

      Console.WriteLine("...when the number of boxes is less than {0}", LOWQUANTITY);

      quantity = method("\nQuantity ");
      diliveryZone = method("Zone ");

      if ((diliveryZone == ZONE1 || diliveryZone == ZONE2) && quantity <= LOWQUANTITY)
         Console.WriteLine("Free");
      else
         Console.WriteLine("Not free");

      Console.ReadKey();
    }

    public static int method(string s)
    {
      int num;

      Console.Write(s);
      num = Convert.ToInt32(Console.ReadLine());
      return num;
    }
  }
}
0
Vulpes

Vulpes

NA 98.3k 1.5m 13y
It worked OK when I tried it but you have to input the quantity before the zone:

Quantity 9
Zone 1
Free

0
Vulpes

Vulpes

NA 98.3k 1.5m 13y
It's because these two lines are the wrong way around:

   quantity = method("\nZone ");
   diliveryZone = method("Quantity ");

They should be:

  quantity = method("\nQuantity ");
  diliveryZone = method("Zone ");