0

Can we write this code even better....???

I working on Scientific calculator project User can give as many as inputs for a particular Aurthamatic operation I separately testing this code import java.util.Scanner; public class Program { public static void main(String[] args) { int a,b; Scanner Sc = new Scanner(System.in); a = Sc.nextInt(); b = Sc.nextInt(); a = a + b; System.out.println(a); while(a>0) { try { b = Sc.nextInt(); } catch (Exception e) { System.out.println("Input terminated"); break; } a = a + b; System.out.println(a); } } } I felt it's better to ask you guys "how I turn this code even better , I want ur suggestions... 🤗

3rd May 2021, 7:13 AM
Ashok Kumar
Ashok Kumar - avatar
7 Réponses
+ 3
Ashok Kumar It really depends on what you mean by better. The answer will mean something different for a student learner compared to a professional software engineer. It will depend on your long term and short term goals for this code. I'm assuming the scope of your question is at a beginner level. Without clear context of what you mean by "better", I still believe that, fundamentally, this code could be significantly improved in its structure, logic flow, readability, testability, etc. I wouldn't even know where to start to begin explaining the improvements. Perhaps I'll post a followup with some minor recommendations. 🤷‍♂️
3rd May 2021, 7:40 AM
David Carroll
David Carroll - avatar
+ 2
Ashok Kumar Here's a simplified version of your code with some basic improvements. -------- public static void main(String[] args) { Scanner sc = new Scanner(System.in); int sum = 0; while(sc.hasNext()) { try { sum += sc.nextInt(); System.out.println(sum); } catch (Exception e) { System.out.println("Input terminated"); return; } } } --------
3rd May 2021, 8:18 AM
David Carroll
David Carroll - avatar
+ 2
Ashok Kumar I've improved the above code further: https://code.sololearn.com/cIn6PJOypshu/?ref=app BTW... The output "Input Terminated" was left in the case in case you needed that to print everytime this is run. However, that line could be removed. -------- public static void main(String[] args) { var sc = new Scanner(System.in); var sum = 0; while(sc.hasNextInt()) { sum += sc.nextInt(); out.println(sum); } out.println("Input Terminated"); } --------
4th May 2021, 4:57 AM
David Carroll
David Carroll - avatar
+ 1
Ohh, sry for not conveying my doubt clearly. I want to know the improvements that my code required I expected if anyone already done this type program will help me to write this program in different ways and to improve the structure, etc I am still student learner.... So I thought it's better to ask someone help. I really got no idea about this program improvements. Thank you for your suggestions🤗 Next time I will definitely try to explain the exact problem I facing in code.....
3rd May 2021, 8:21 AM
Ashok Kumar
Ashok Kumar - avatar
+ 1
Ashok Kumar Take a look at my simple revisions and see if you can identify all the improvements.
3rd May 2021, 8:25 AM
David Carroll
David Carroll - avatar
+ 1
David Carroll thank you very much. Yes I found the improvements that my code required... Tq
3rd May 2021, 8:36 AM
Ashok Kumar
Ashok Kumar - avatar
0
David Carroll I really have doubt about the loop condition statement & try, catch. From the 1st code: while (a>0) {} If I give negative value to the a Loop will fail. I didn't get any alternative condition statement for loop and how to excute this code without using try, catch. Ur further improvement in code cleared my 2 doubts Tq.. 😇
4th May 2021, 7:19 AM
Ashok Kumar
Ashok Kumar - avatar