need help with c# conversion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • EmNewbie
    New Member
    • Oct 2011
    • 3

    need help with c# conversion

    I'm new to c# and I need help converting this-any help would be greatly appreciated.-I was previously in a java class and now I'm in c# class with the same assignment.


    Code:
    public class Furniture
    {
    public static void main(String[] args)
    {
    int tree = 0;
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter \n 1 for pine \n 2 for oak \n 3 for mahogany");
    try
    {
    tree = scan.nextInt();
    }
    catch(Exception e)
    {
    System.out.println("Invalid input. Only numbers are accepted");
    }
    int cost = 0;
    if(tree == 1)
    cost = 100;
    else if(tree == 2)
    cost = 225;
    else if(tree == 3)
    cost = 310;
    else
    cost = 0;
    String tab = "";
    if(tree == 1)
    tab = "Pine";
    else if(tree == 2)
    tab = "Oak";
    else if(tree == 3)
    tab = "Mahogany";
    System.out.println("This " + tab + " table will cost you $" + cost);
    }
    Last edited by Meetee; Oct 31 '11, 05:20 AM. Reason: code tags added
  • Paul Johnson
    New Member
    • Oct 2010
    • 97

    #2
    Hi,

    This isn't too hard...

    Code:
        public class Furniture
        {
        public static void main(String[] args)
    main is slightly difference in C#, but not greatly. As you're not bothering to pass anything in...

    Code:
    static void Main()
    Quick changes here....

    Code:
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter \n 1 for pine \n 2 for oak \n 3 for mahogany");
        try
        {
        tree = scan.nextInt();
    becomes
    Code:
    int tree = 0;
    while (tree < 1 || tree > 3)
    Console.Write("Enter \n 1 for pine \n 2 for oak \n 3 for mahogany");
    string number = Console.Read();
    tree = Convert.ToInt32(string);
    I'd now change these to a switch....
    Code:
        int cost = 0;
        if(tree == 1)
        cost = 100;
        else if(tree == 2)
        cost = 225;
        else if(tree == 3)
        cost = 310;
    Now becomes
    Code:
    switch(tree)
    {
       case 1 : cost = 100; tab = "Pine"; break;
       case 2 : cost = 225; tab = "Oak"; break;
       case 3 : cost = 310; tab = "Mahogany"; break;
    }
    And finally...
    Code:
        System.out.println("This " + tab + " table will cost you $" + cost);
    becomes

    Code:
    Console.WriteLine("This {0} table will cost you ${1}", tab, cost);
    HTH
    }
    Last edited by Paul Johnson; Oct 31 '11, 02:32 PM. Reason: Incorrect variable name

    Comment

    • EmNewbie
      New Member
      • Oct 2011
      • 3

      #3
      Thank you for the help. It's greatly appreciated

      Comment

      • surya varma
        New Member
        • Nov 2011
        • 2

        #4
        public class Furniture
        {
        public static void main(String[] args)
        {
        int tree = 0;
        Scanner scan = new Scanner(System. in);
        Console.WriteLi ne("Enter \n 1 for pine \n 2 for oak \n 3 for mahogany");
        try
        {
        tree =Convert.ToInt3 2(scan);
        }
        catch(Exception e)
        {
        Console.WriteLi ne("Invalid input. Only numbers are accepted");
        }
        int cost = 0;
        if(tree == 1)
        cost = 100;
        else if(tree == 2)
        cost = 225;
        else if(tree == 3)
        cost = 310;
        else
        cost = 0;
        String tab = "";
        if(tree == 1)
        tab = "Pine";
        else if(tree == 2)
        tab = "Oak";
        else if(tree == 3)
        tab = "Mahogany";
        Console.WriteLi ne("This " + tab + " table will cost you $" + cost);
        }

        Comment

        • Paul Johnson
          New Member
          • Oct 2010
          • 97

          #5
          While I'm nowhere near an expert, I'm at a loss over what System.in is - it's certainly not listed on MSDN. Would it not be simpler to have

          Code:
          string foo = ""
          int tree = 0;
          foo=Console.ReadLine();
          tree = Convert.ToInt32(foo);
          With something around the Convert to check for the value?

          Comment

          • EmNewbie
            New Member
            • Oct 2011
            • 3

            #6
            Thank you for the help

            Comment

            Working...