java - Why is my code skipping through the if statement? -
java - Why is my code skipping through the if statement? -
i'm posting segment of larger block of code i'm having problem with. should run itself. purpose of testing, input 1 @ first prompt. 1 time runs print statement, programme terminates instead of asking variable. don't understand why. can help me?
import java.util.scanner; public class physics { public static void main(string[] args) { scanner input = new scanner(system.in); int switchnumber; string variablecaseone; double distance; double initialvelocity; double time; double gravity; system.out.println("this section projectile motion."); system.out.println("which equation use?"); system.out.println("1. horizontal equation: d = vi * t"); system.out.println("2. vertical equation: d = vi * t - (1/2)g * (t^2)"); switchnumber = input.nextint(); if (switchnumber == 1) { system.out.println("tell me variable you'd solve for."); variablecaseone = input.nextline(); if (variablecaseone.equals("d")) { system.out.println("enter initial velocity."); initialvelocity = input.nextdouble(); system.out.println("enter time."); time = input.nextdouble(); system.out.println("distance equals: " + initialvelocity * time); } } } }
thank helping!
if have understood correctly seek change
variablecaseone = input.nextline();
to
variablecaseone = input.next();
it works me
snpt
public static void main(string[] args) { scanner input = new scanner(system.in); int switchnumber; string variablecaseone; double initialvelocity; double time; system.out.println("this section projectile motion."); system.out.println("which equation use?"); system.out.println("1. horizontal equation: d = vi * t"); system.out.println("2. vertical equation: d = vi * t - (1/2)g * (t^2)"); switchnumber = input.nextint(); if (switchnumber == 1) { system.out.println("tell me variable you'd solve for."); variablecaseone = input.next(); if (variablecaseone.equals("d")) { system.out.println("enter initial velocity."); initialvelocity = input.nextdouble(); system.out.println("enter time."); time = input.nextdouble(); system.out.println("distance equals: " + initialvelocity * time); } } input.close(); }
java if-statement java.util.scanner user-input
Comments
Post a Comment