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

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -