+ 2

is there any way to change the if & else if statements in this code?

System.out.println("Please select the unit\n 1-kilogram(kg)\n 2-gram(g)\n 3-milligram(mg)\n 4-ounce\n 5-pounce(oz)"); int convin = tony.nextInt(); System.out.println("Input value:"); int val = tony.nextInt(); System.out.println(""); System.out.println("Convert to?\n 1-kilogram(kg)\n 2-gram(g)\n 3-milligram(mg)\n 4-ounce\n 5-pounce(oz"); int convout = tony.nextInt(); if (convin == 1 && convout == 1){ int ans = val*1; }

27th Oct 2017, 10:20 PM
Toni Sedjoah
Toni Sedjoah - avatar
6 Answers
+ 2
https://code.sololearn.com/cWt45uECfApD/#java Sorry about delay. That's rough generic example. Just change your variables to whatever suits what you're doing and input your calculations in the right spots. I commented it some for ya.
27th Oct 2017, 11:10 PM
AgentSmith
+ 4
I would create a class and create methods for each of the 5 conversions. Then you can just feed the source unit type/value to the methods and have the converted number returned to you. Doing this also allows you to reuse your code later without having to rewrite it.
27th Oct 2017, 10:32 PM
AgentSmith
+ 3
@Netkos Ent Tha KS very much for your reply.. but how am I going to create the methods?
27th Oct 2017, 10:34 PM
Toni Sedjoah
Toni Sedjoah - avatar
+ 3
Thanks very much...will be waiting
27th Oct 2017, 10:40 PM
Toni Sedjoah
Toni Sedjoah - avatar
+ 3
thanks Netkos, I'll go through and get to back if I'm having any challenges.... I really appreciate your help
28th Oct 2017, 9:09 AM
Toni Sedjoah
Toni Sedjoah - avatar
+ 2
Give me a few mins and I'll type up some code in the playground for you to use as an example.
27th Oct 2017, 10:38 PM
AgentSmith