We help IT Professionals succeed at work.

C# Change the instruction to user.

Ms L
Ms L asked
on
107 Views
Last Modified: 2017-05-11
Currently I have coding that ask user to input the numbers like this .
Here my outputI want to change a little bit the instruction to be like this

    Please enter the numbers (separated by enter) :
    Lot     Qty
    A        10
    B         30
    C         50
    D        70
    E         80
    F         100
    G         110
    H         120


How can I do like that? The alphabet will automatically add until the user click " space". After user click space button, The other instruction will come out.  Below is the coding:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static void Main(string[] args)
    {
        string input;
        int NoDisplay;
        decimal goal;
        decimal element;
        
        do
        {
            Console.WriteLine("Please enter the target:");
            input = Console.ReadLine();
        }
        while (!decimal.TryParse(input, out goal));


        

        Console.WriteLine("Please enter the numbers (separated by spaces)");

        
        input = Console.ReadLine();
        string[] elementsText = input.Split(' ');
        List<decimal> elementsList = new List<decimal>();
        foreach (string elementText in elementsText)
        {
            if (decimal.TryParse(elementText, out element))
            {
                elementsList.Add(element);
            }
        }

        int i;
        int j;
        decimal tmp;
        int MaxCnt = 0;
        Boolean recordexist = false;

        for (i = 0; i < elementsList.Count; i++)
        {
            for (j = i + 1; j < elementsList.Count; j++)
            {
                if (elementsList[i] < elementsList[j])
                {
                    tmp = elementsList[i];
                    elementsList[i] = elementsList[j];
                    elementsList[j] = tmp;
                }
            }
        }

        Console.WriteLine("Please enter the maximum combination :");
        NoDisplay = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("The results :"); //the first result

        Solver solver = new Solver();
        List<List<decimal>> results = solver.Solve(goal, elementsList.ToArray());

        List<resultList> ResultList = new List<resultList>();

        recordexist = false;
        foreach (List<decimal> result in results)
        {
            if (result.Count == NoDisplay)
            {
                recordexist = true;
                decimal sum = 0;
                foreach (decimal value in result)
                {
                   
                    sum = sum + value;
                }
                ResultList.Add(new resultList(result, sum));
            }
        }
        List<resultList> SortedList = ResultList.OrderBy(o => o.sum).ToList();

        if (recordexist == false)
        {
            Console.WriteLine("No record exist");
        }
        else
        {
            MaxCnt = SortedList.Count > 50 ? 50 : SortedList.Count;
            for (int k = 0; k < MaxCnt; k++)
            {
                foreach (decimal value in (List<decimal>)SortedList[k].results)
                    Console.Write("{0}\t", value);

                Console.WriteLine(" = {0}", SortedList[k].sum);   //the minimum result line
            }
        }

        Console.WriteLine("The final results :"); //the last result

        List<resultList> minResultList = new List<resultList>();

        decimal minSub = 100000;     //to holding minimun total  
        List<decimal> minResult = new List<decimal>(); //to holding minimum Result

        recordexist = false;
        foreach (List<decimal> result in results)
        {
            if (result.Count == NoDisplay)
            {
                decimal subtract = 0;
                decimal sum = 0;
                recordexist = true;
                foreach (decimal value in result)
                {
                    sum = sum + value;
                    subtract = sum - goal;
                }

                if (subtract < minSub)
                {
                    minSub = subtract;
                    minResult = null;
                    minResult = new List<decimal>();
                    foreach (decimal value in result)
                    {
                        minResult.Add(value);
                    }
                    minResultList.Add(new resultList(minResult, minSub));
                }
            }
        }
        SortedList = minResultList.OrderBy(o => o.sum).ToList();

        if (recordexist == false)
        {
            Console.WriteLine("No record exist");
        }
        else
        {
            MaxCnt = SortedList.Count > 5 ? 5 : SortedList.Count;
            for (int k = 0; k < MaxCnt; k++)
            {
                foreach (decimal value in (List<decimal>)SortedList[k].results)
                    Console.Write("{0}\t", value);

                Console.WriteLine(" = {0}", SortedList[k].sum);   //the minimum result line                    
            }
        }

        Console.WriteLine("The minimum results is :"); //the minimum result line
        MaxCnt = minResultList.Count > 1 ? minResultList.Count : 0;

        if (MaxCnt > 0)
        {
            foreach (decimal value in (List<decimal>)minResultList[MaxCnt - 1].results)
                Console.Write("{0}\t", value);

            Console.WriteLine(" = {0}", minResultList[MaxCnt - 1].sum);   //the minimum result line
        }
        else
        {
            Console.WriteLine("No record exist");
        }
        Console.ReadLine();
    }
    }



    public class resultList
    {
    public List<decimal> results;
    public decimal sum = 0;

    public resultList(List<decimal> rst, decimal sub)
    {
        results = rst;
        sum = sub;
    }
    }

Open in new window


I tried to add this in the coding  but not successful.
  int a;
        int b;
        int c;
        int d;
        int e;
        int f;
        int g;
        int h;
        

        do
        {
            Console.WriteLine("Please enter the target:");
            input = Console.ReadLine();
        }
        while (!decimal.TryParse(input, out goal));


        

        Console.WriteLine("Please enter the numbers (separated by spaces)");

        a = Convert.ToInt32(Console.ReadLine());
        b = Convert.ToInt32(Console.ReadLine());
        c = Convert.ToInt32(Console.ReadLine());
        d = Convert.ToInt32(Console.ReadLine());
        e = Convert.ToInt32(Console.ReadLine());
        f = Convert.ToInt32(Console.ReadLine());
        g = Convert.ToInt32(Console.ReadLine());
        h = Convert.ToInt32(Console.ReadLine());

Open in new window

Comment
Watch Question

Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer / Applications Consultant
CERTIFIED EXPERT

Commented:
you can try something like this to capture the input:

List<decimal> yourInputList = new List<decimal>();
            do
            {
                input = Console.ReadLine();
                //store the input
                if (decimal.TryParse(input, out goal))
                {
                    yourInputList.Add(goal);
                }
            }
            while (input != " ");

Open in new window

Ms LStudent

Author

Commented:
Ryan Chong, do u mean like this?
        Console.WriteLine("Please enter the numbers (separated by spaces)");

        input = Console.ReadLine();
        string[] elementsText = input.Split(' ');
        List<decimal> elementsList = new List<decimal>();

        do
        {
            input = Console.ReadLine();
            //store the input
            if (decimal.TryParse(input, out goal))
            {
                elementsList.Add(goal);
            }
        }
        while (input != " ");

Open in new window

Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer / Applications Consultant
CERTIFIED EXPERT

Commented:
i got something like tihs (amended from your previous question)

see if this is what you need?

string input;
            int NoDisplay;
            decimal goal;
            decimal element;

            do
            {
                Console.WriteLine("Please enter the target:");
                input = Console.ReadLine();
            }
            while (!decimal.TryParse(input, out goal));

            Console.WriteLine("Please enter the numbers (separated by entered)");

            List<decimal> yourInputList = new List<decimal>();
            do
            {
                input = Console.ReadLine();
                //store the input
                if (decimal.TryParse(input, out goal))
                {
                    yourInputList.Add(goal);
                }
            }
            while (input != " ");

//.... continue with the rest of your codes

Open in new window

Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer / Applications Consultant
CERTIFIED EXPERT

Commented:
yup, i think you can adapt it to use elementsList  instead of yourInputList
Ms LStudent

Author

Commented:
I already tried and the output be like this
thisI think that is not I mean. The alphabet like A,B,C,D will be display automatically and user just need to enter the qty that user want. It will go to other Alphabet if user click enter button. About the alphabet , I read this http://stackoverflow.com/questions/29004792/logic-to-generate-an-alphabetical-sequence-in-c-sharp?rq=1 but I dont think that is the way.
Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer / Applications Consultant
CERTIFIED EXPERT

Commented:
The alphabet like A,B,C,D will be display automatically and user just need to enter the qty that user wan
what if the user entered a non-numerical value?
Ms LStudent

Author

Commented:
The non-numerical value will not do the combination. Do u mean like this?
3.jpg
Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer / Applications Consultant
CERTIFIED EXPERT

Commented:
The non-numerical value will not do the combintation. Do u mean like this?
nope...

and based on this question, thought you wish to let the user to numbers by using "enter" key ? my another question is if you list the entries with alphabets, what if the values entered are non-numeric.
Ms LStudent

Author

Commented:
thought you wish to let the user to numbers by using "enter" key ?

What I mean is, the first  instruction will be like this
  Please enter the numbers (separated by enter) :
    Lot     Qty
    A        

( at the qty , user will keyin the number ) then after user click enter it will be like this
   B        
after B appear , user will key-in others number at qty column. It will continue tell user to keyin the number until user click the space button. Then it will show like this

Please enter the maximum combination :

another question is if you list the entries with alphabets, what if the values entered are non-numeric.
sorry, not understand what u mean.
Software Tead Lead / Business Analyst / System Analyst / Data Engineer / Applications Consultant
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Ms LStudent

Author

Commented:
Okay sorry my mistake. I will change this console app to win forms. Thanks btw
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.