i'm working on program give user menu, allow them select option, ask different numbers calculation based on menu choice. i've started basic outline of program, need getting loops implemented , working.
here code far:
import java.util.scanner; public class lb04alexander { public static void main(string[] args) { scanner input = new scanner(system.in); system.out.println("this program perform several types of calculations."); system.out.println(); system.out.println("-menu-"); system.out.println("select following menu options:"); system.out.println("1 - compute integer powers of number"); system.out.println("2 - compute factorial"); system.out.println("3 - compute square root"); system.out.println("4 - compute cube root"); system.out.println("5 - compute exponential"); system.out.println("6 - compute sine of angle"); system.out.println("7 - compute cosine of angle"); system.out.println("8 - convert positive integer binary"); system.out.println("9 - exit"); system.out.println("please enter number corresponding choice:"); int menuchoice = input.nextint(); while (menuchoice >= 1 && menuchoice <= 9); { switch (menuchoice) { case 1: system.out.println("you have chosen compute integer powers of number."); system.out.println("please enter number base:"); double base1 = input.nextdouble(); system.out.println("now, enter non-negative integer exponent:"); int exponent1 = input.nextint(); if (exponent1 < 0) system.out.println("error: exponent negitive."); else system.out.println(base1 + "^" + exponent1 + " = " + ""); break; case 2: system.out.println("you have chosen compute factorial (n!) of non-negative integer."); system.out.println("please enter non-negitive integer n:"); long fact = input.nextlong(); int result = 1; if (fact < 0) system.out.println("error: integer negitive."); else if (fact >= 127) system.out.println("error: answer large calculate"); else (int = 1; <= fact; i++) { result = result * i; } system.out.println(fact + "! = " + result ); break; case 3: system.out.println("you have chosen compute square root."); break; case 4: system.out.println("you have chosen compute cube root."); break; case 5: system.out.println("you have chosen compute exponential."); break; case 6: system.out.println("you have chosen compute sine of angle."); break; case 7: system.out.println("you have chosen compute cosine of angle."); break; case 8: system.out.println("you have chosen convert positive integer binary"); break; case 9: system.out.println("you have chosen exit"); system.out.println("program lb04alexander terminating..."); input.close(); system.out.println("the program has ended. goodbye."); break; default: system.out.println("invalid menu choice, please try again."); break; } } } } the first problem i'm having getting menu loop right way. either doesn't work, or loops forever.
while (menuchoice >= 1 && menuchoice <= 9); get rid of semi-colon. when choice satisfies condition, loops forever. when doesn't, quit (since none of cases match) after printing out invalid menu choice, please try again..
Comments
Post a Comment