I have an application where I need the ones, tens and hundreds values from numbers between 10 and 120. I tested my theories out in a small program but I cant get tens to work without using conditional statements. This program generates numbers and tells you the ones, tens and hundreds values of the number with if statements. (I only want even numbers)
Is there any way to achieve these results without using an If or a Switch statement? (I couldn't figure out how to use the latter with 2 conditions)
using System;
namespace Digit_tests
{
class Program
{
static int digit; static int tens; static int ones; static int hundrds;
public static void Main()
{
hundrds = 0;
Random rnd = new Random();
digit = rnd.Next(10,121);
if (digit % 2 != 0)
{
random();
}
else {}
if (digit >= 10 && digit < 20)
{
tens = 1;
}
else if (digit >= 20 && digit < 30)
{
tens = 2;
}
else if (digit >= 30 && digit < 40)
{
tens = 3;
}
else if (digit >= 40 && digit < 50)
{
tens = 4;
}
else if (digit >= 50 && digit < 60)
{
tens = 5;
}
else if (digit >= 60 && digit < 70)
{
tens = 6;
}
else if (digit >= 70 && digit < 80)
{
tens = 7;
}
else if (digit >= 80 && digit < 90)
{
tens = 8;
}
else if (digit >= 90 && digit < 100)
{
tens = 9;
}
else if (digit >= 100 && digit < 110)
{
tens = 0;
hundrds = 1;
}
else if (digit >= 110 && digit < 120)
{
tens = 1;
hundrds = 1;
}
else if (digit >= 120)
{
tens = 2;
hundrds = 1;
}
ones = digit - tens * 10;
if (hundrds == 1)
{
ones = ones - 100;
}
Console.WriteLine(digit + "\n" + hundrds + "-" + tens + "-" + ones);
Console.ReadKey();
Main();
}
public static void random()
{
Random rnd = new Random();
digit = rnd.Next(10,121);
Main();
}
}
}
Aucun commentaire:
Enregistrer un commentaire