Saturday 15 March 2014

Simple Java Calculator (Need Decimal Help) -



Simple Java Calculator (Need Decimal Help) -

ok simple java calculator works fine need help allowing decimals. lot ever helps me this, school project need reply pretty fast :)

import java.util.scanner; public class javaproject { public static void main(string[] args) { int x; int y; string whatop; scanner input = new scanner(system.in); system.out.println("please come in first number"); x = input.nextint(); system.out.println("please come in sec number"); y = input.nextint(); scanner op = new scanner(system.in); system.out.println("please come in operation want use"); whatop = op.next(); if (whatop.equals("+")) { system.out.println("the reply " + (x + y)); } else if (whatop.equals("-")) { system.out.println("the reply " + (x - y)); } else if (whatop.equals("*")) { system.out.println("the reply " + (x * y)); } else if (whatop.equals("/")) { system.out.println("the reply " + (x / y)); } else { system.out.println("thats not choice!"); } } }

in programme using integers. hence no decimals can used. in java there multiple types of variables. 1 want utilize decimals called double. integers , doubles both primitive info types in java. if want larn more primitive info types check out link: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

to create calculator able have double type input, need alter x , y doubles instead of integer:

double x; double y;

everything same until want receive user input, instead of using:

x = input.nextint(); y = input.nextint();

you need alter .nextint(); .nextdouble(); because number want inputted double , not integer. this:

x = input.nextdouble(); y = input.nextdouble();

i hope helps.

java

No comments:

Post a Comment