473,324 Members | 2,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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.


Expand|Select|Wrap|Line Numbers
  1. public class Furniture
  2. {
  3. public static void main(String[] args)
  4. {
  5. int tree = 0;
  6. Scanner scan = new Scanner(System.in);
  7. System.out.println("Enter \n 1 for pine \n 2 for oak \n 3 for mahogany");
  8. try
  9. {
  10. tree = scan.nextInt();
  11. }
  12. catch(Exception e)
  13. {
  14. System.out.println("Invalid input. Only numbers are accepted");
  15. }
  16. int cost = 0;
  17. if(tree == 1)
  18. cost = 100;
  19. else if(tree == 2)
  20. cost = 225;
  21. else if(tree == 3)
  22. cost = 310;
  23. else
  24. cost = 0;
  25. String tab = "";
  26. if(tree == 1)
  27. tab = "Pine";
  28. else if(tree == 2)
  29. tab = "Oak";
  30. else if(tree == 3)
  31. tab = "Mahogany";
  32. System.out.println("This " + tab + " table will cost you $" + cost);
  33. }
Oct 31 '11 #1
5 2004
Hi,

This isn't too hard...

Expand|Select|Wrap|Line Numbers
  1.     public class Furniture
  2.     {
  3.     public static void main(String[] args)
main is slightly difference in C#, but not greatly. As you're not bothering to pass anything in...

Expand|Select|Wrap|Line Numbers
  1. static void Main()
  2.  
Quick changes here....

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

Expand|Select|Wrap|Line Numbers
  1. Console.WriteLine("This {0} table will cost you ${1}", tab, cost);
  2.  
HTH
}
Oct 31 '11 #2
Thank you for the help. It's greatly appreciated
Oct 31 '11 #3
public class Furniture
{
public static void main(String[] args)
{
int tree = 0;
Scanner scan = new Scanner(System.in);
Console.WriteLine("Enter \n 1 for pine \n 2 for oak \n 3 for mahogany");
try
{
tree =Convert.ToInt32(scan);
}
catch(Exception e)
{
Console.WriteLine("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.WriteLine("This " + tab + " table will cost you $" + cost);
}
Nov 1 '11 #4
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

Expand|Select|Wrap|Line Numbers
  1. string foo = ""
  2. int tree = 0;
  3. foo=Console.ReadLine();
  4. tree = Convert.ToInt32(foo);
  5.  
With something around the Convert to check for the value?
Nov 1 '11 #5
Thank you for the help
Nov 3 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Hilary Band | last post by:
I have a feeling I'm missing something obvious here, but being new to SQL Server is my only excuse! I have a table that holds a variety of data. I am writing a user report where the user wants...
14
by: ES Kim | last post by:
Consider: #include <string> #include <iostream> using namespace std; struct S { const char* ps_; operator string();
5
by: John J. Hughes II | last post by:
I need to convert a byte array to a structure with conversion on the byte order. I found the below deserilizer which works for getting the data converted to a structure but it's in the wrong byte...
5
by: Chris | last post by:
Hi, How can I convert positive integers to negative integers. Eg 1 to -1. Thanks
4
by: DeanL | last post by:
Hi All, Having a problem developing an Access 97 database in Access 2003. The database needs to go out to a number of staff who will review it and recommend any changes but when I convert the...
7
by: Rob R. Ainscough | last post by:
I've tried several online C# to VB.NET converters but none have produce valid output. C# ckey.SetValue("Type", (int)ckey.GetValue("Type") | 0x100); the converts show: ckey.SetValue("Type",...
3
by: Rob | last post by:
Any idease on how to fix ? The Code in C# public void Load(string FileName) { LineToDraw l; try { this.Clear(true);
1
by: mitsura | last post by:
Hi, I need to re-write a VB script into Python (because I really don't like VB). The VB script is used to create a Windows COM object. (I am more of Unix guy, so COM objects are a little bit...
2
by: MikeCheese | last post by:
I understand that it is not necessary to make the conversion but Access 2003 loses tables. The problem is I have two databases that share linked tables. I have create an mde and the tables are...
0
by: ponvijaya | last post by:
Hi all, i have date format as dd/mm/yy in my table field. I need to convert it into MySql Format YYYY-MM-DD . For example i need to convert the date 09/05/07 to 2007-05-09. Can a any body...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.