java - Writing an infinite loop -
i writing code should create infinite loop, terminating unexpectedly. when either type in 'y' yes or 'n' no, code should enter non-terminating loop.
import java.util.scanner; import java.text.numberformat; import java.text.decimalformat; public class math_island_xtended_final { static scanner sc = new scanner(system.in); static scanner keyboard = new scanner(system.in); public static void main(string[] args) { //declarations int bignumber; int mednumber; int smallnumber; double addition; double subtraction; double multiplcation; double division; double sphere_radius1; double sphere_radius2; double sphere_radius3; double rectangle_measurements; char repeat; string input; system.out.println("welcome math island :d ! "); system.out.println("we use numbers our formula ! "); system.out.println("1 rule, no decimals !! "); system.out.println("please pick # 1 100 now!! "); bignumber = sc.nextint(); system.out.print("please pick # 1 20 now!! "); mednumber = sc.nextint(); system.out.print("please pick # 1 5 now!! "); smallnumber = sc.nextint(); //results addition = bignumber + mednumber + smallnumber; subtraction = bignumber - mednumber - smallnumber; multiplcation = bignumber * mednumber * smallnumber; division = bignumber / mednumber / smallnumber; sphere_radius1 = bignumber * 3.14 * 3.14; sphere_radius2 = mednumber * 3.14 * 3.14; sphere_radius3 = smallnumber * 3.14 * 3.14; rectangle_measurements = bignumber * mednumber * smallnumber; system.out.println(); system.out.println(); //results 2 system.out.println(" addition answer " + addition); system.out.println(" subtraction answer " + subtraction); system.out.println(" multiplcation answer " + multiplcation); system.out.println(" division answer " + division); system.out.println(" first sphere answer " + sphere_radius1); system.out.println(" second sphere answer " + sphere_radius2); system.out.println(" third sphere answer " + sphere_radius3); system.out.println(" recangle size " + rectangle_measurements+ " in cubic feet"); system.out.println(); system.out.println(); system.out.println("would play again ? "); system.out.println("y yes, & n no " ); input = keyboard.nextline(); repeat = input.charat(0); while (repeat == 'y'); system.out.println(); while (repeat == 'n'); system.out.println(); system.out.println("goodbye!"); } }
encase entire code in while
loop
define repeat
boolean before while loop- set true
then replace
while (repeat == 'y') { system.out.println(); } while (repeat == 'n') { system.out.println(); system.out.println("goodbye!"); }
with if
statement checks if user input "n", in case change repeat
false. otherwise keep repeat
true.
also- lots of syntax errors in code- clean , should fine
Comments
Post a Comment