Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
using System;
using System.Text.RegularExpressions;

namespace ConsoleApp18
{
    class Program
    {
        static void Main(string[] args)
        {
            int value1;

            Console.WriteLine("Please enter humidity(0-100)");

            value1 = Convert.ToInt32(Console.ReadLine());
            switch (value1)
            {
                case value1 <= 20 && value1 > 0:
                    Console.WriteLine("too dry!");
                    break;
            }
        }
    }
}


What I have tried:

I HAVE TO USE SWİTCH-CASE;
QUESTİON: PROGRAM FOR CONVERTS HUMIDITY VALUES TO A HUMAN READABLE FORMAT.

PROGRAM WILL WRITE TO OUTPUT THE FOLLOWING RESULTS DEPENDING INPUT RANGES BETWEEN 0 AND 100
%20 AND LOWER-> TOO DRY
%21-%40-> DRY
%41-&60-> LITTLE DRY
%61-%80-> LITTLE MOIST
%81 AND HIGHER -> MOİST

PLEASE HELP ME TO WRITE THIS CODE I CANT DO IT.
Posted
Updated 27-Oct-20 16:52pm
v2
Comments
j snooze 27-Oct-20 17:50pm    
A simple google search would do the trick. Just look for "c# switch statement greater than".
Google first, ask questions second on here. If we can find it within seconds on an internet search, you can too. Its a wonderful tool for figuring out how to do things.
PIEBALDconsult 27-Oct-20 17:50pm    
That doesn't sound like a suitable situation for switch/case
Member 14976913 27-Oct-20 18:49pm    
Such a upper-level brain person... (j snooze)I did all searching and I couldn't reach result succesfully so as I said I need some help.
BillWoodruff 28-Oct-20 4:39am    
There are new ways in both C# 7 and 8, to do this, but why would I waste my time helping a lazy whiner like you who insults people trying to help ? Grow up !

Simples.

C#
public enum Humidity
{
  [System.ComponentModel.DescriptionAttribute("OUT OF RANGE")]
  Invalid  = 0
,
  [System.ComponentModel.DescriptionAttribute("TOO DRY")]
  VeryLow  = 1
,
  [System.ComponentModel.DescriptionAttribute("DRY")]
  Low      = 2
,
  [System.ComponentModel.DescriptionAttribute("LITTLE DRY")]
  Medium   = 3
,
  [System.ComponentModel.DescriptionAttribute("LITTLE MOIST")]
  High     = 4
,
  [System.ComponentModel.DescriptionAttribute("MOİST")]
  VeryHigh = 5
}

private static Humidity
GetHumidityLevel
(
  int Percent
)
{
  Humidity result = Humidity.Invalid ;

  if ( ( Percent >= 0 ) && ( Percent <= 100 ) )
  {
    result = (Humidity) ( ( Percent - 1 ) / 20 + 1 ) ;
  }

  return ( result ) ;
}

private static System.Collections.Generic.Dictionary<Humidity,string> hum =
  new System.Collections.Generic.Dictionary<Humidity,string>() ;

private static string
GetHumidityDescription
(
  Humidity Level
)
{
  string result ;

  if ( !hum.TryGetValue ( Level , out result ) )
  {
    System.Reflection.FieldInfo f = typeof(Humidity).GetField
    (
      Level.ToString ( "G" )
    ,
      System.Reflection.BindingFlags.Public
      |
      System.Reflection.BindingFlags.Static
    ) ;

    System.ComponentModel.DescriptionAttribute[] a = (System.ComponentModel.DescriptionAttribute[])
      f.GetCustomAttributes ( typeof(System.ComponentModel.DescriptionAttribute) , false ) ;

    hum [ Level ] = result = a [ 0 ].Description ;
  }

  return ( result ) ;
}


Testing:

C#
for ( int h = -5 ; h < 110 ; h += 5 )
{
  Humidity v = GetHumidityLevel ( h ) ;

  switch ( v )
  {
    default :
    {
      System.Console.WriteLine ( "{0} {1} {2}" , h , v , GetHumidityDescription ( v ) ) ;

      break ;
    }
  }
}


Submit it as your own work at your peril of course.
 
Share this answer
 
Comments
BillWoodruff 28-Oct-20 4:42am    
+5 for creative chaos :)
PIEBALDconsult 28-Oct-20 10:21am    
:D Glad to be of service.
The switch statement doesn't allow for multiple conditions, it only allows for case when the switch variable value exactly matches that specified in the case block.

You need to use if ... else if ... else instead:
C#
if (a <= 100 && a >= 80)
   {
   ...
   }
else if (a >= 50)
   {
   ...
   }
else
   {
   ...
   }


If your tutor has told you to use a switch and nothing else, then subtract one from the percentage (to bring all the values in to a range 20 to 39 instead of 21 to 40) then switch on that value divided by ten.
That will reduce it to 9 case statements and a default if you think about it for a minute or two.

Give it a try - it's not complicated!
 
Share this answer
 
Rather than switch, think about if.
C# If ... Else[^]
[Update]
Quote:
As I said I have to use switch. It is just homework but I'm wondering how can I solve this way...

a case can handle only 1 value, so 1 case per value.
C#
switch (value1)
{
    case 1:
    case 2:
    case 3:
        Console.WriteLine("too dry!");
        break;
    ...
}
 
Share this answer
 
v3
Comments
PIEBALDconsult 27-Oct-20 18:13pm    
Ummm... it's a class assignment?
Patrice T 27-Oct-20 18:26pm    
probably :)
Member 14976913 27-Oct-20 18:47pm    
As I said I have to use switch. It is just homework but I'm wondering how can I solve this way...
BillWoodruff 28-Oct-20 4:45am    
Hi, Patrice, both C#7/8 offer ways to switch on ranges.
Patrice T 28-Oct-20 5:22am    
I have not seen this syntax.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900