Simple Calculator Java Coding Errors -
this question has answer here:
- simple java calculator 10 answers
trying make simple calculator in java keep getting errors when try compile it. not sure if it's coding or doing when compile it. appreciated!
import java.util.scanner; class simple calculator { // simple calculator public static void main(string args[]){ system.out.println("my simple calculator\n"); scanner bucky= new scanner(system.in); double fnum,snum,ans; system.out.print("enter first , second " + "number : "); a=bucky.nextfloat(); //assign numbers b=bucky.nextdouble(); // respective variable system.out.println("what operation u want " + "perform"); string operation; operation=bucky.next(); switch(operation) { case "+": ans=a + b; system.out.print("sum of 2 inputs = "); system.out.print(ans); break; case "-": ans=a - b; system.out.print("subtraction of 2 inputs = "); system.out.print(ans); break; case "*": ans=a * b; system.out.print("multiplication of 2 inputs = "); system.out.print(ans); break; case "/": ans=a / b; system.out.print("division of 2 inputs = "); system.out.print(ans); break; default : system.out.println("give proper operation " + "symbol "); break; // not required } } }
two compilation errors:
- your class name has whitespace in it:
class simple calculator
isn't valid, because "calculator" isn't recognized token. try:class simplecalculator
. make public , change name of file match. (simplecalculator.java). - you declare variables
a
,b
don't give them types. usefloat = ...
,double b = ...
. 1 float , other double strange, pressing on...
other new-to-java issues note:
- you never close
bucky
, scanner. resource leak, , in bigger application major bug. addbucky.close()
after read last line it, afteroperation = bucky.next()
- the name
bucky
tells me absolutely nothing or use is. try use descriptive variable names. (the main exception being single letter names loop indices, etc). fnum
,snum
local variables declare never use. perhaps rid of them?
here's code edits made:
import java.util.scanner; public class simplecalculator { // simple calculator public static void main(string args[]){ system.out.println("my simple calculator\n"); scanner bucky= new scanner(system.in); double ans; system.out.print("enter first , second " + "number : "); float a=bucky.nextfloat(); //assign numbers double b=bucky.nextdouble(); // respective variable system.out.println("what operation u want " + "perform"); string operation; operation=bucky.next(); bucky.close(); switch(operation) { case "+": ans=a + b; system.out.print("sum of 2 inputs = "); system.out.print(ans); break; case "-": ans=a - b; system.out.print("subtraction of 2 inputs = "); system.out.print(ans); break; case "*": ans=a * b; system.out.print("multiplication of 2 inputs = "); system.out.print(ans); break; case "/": ans=a / b; system.out.print("division of 2 inputs = "); system.out.print(ans); break; default : system.out.println("give proper operation " + "symbol "); break; // not required } } }
i able compile in terminal no trouble.
Comments
Post a Comment