Reputation: 3
I'm posting a segment of a larger block of code that I'm having trouble with. It should run by itself. For the purpose of testing, just input one at the first prompt. Once it runs the print statement, the program terminates instead of asking for the variable. I don't understand why. Can someone 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 is for projectile motion.");
System.out.println("Which equation would you like to 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 which variable you'd like to solve for.");
variableCaseOne = input.nextLine();
if (variableCaseOne.equals("d")) {
System.out.println("Enter the Initial velocity.");
initialVelocity = input.nextDouble();
System.out.println("Enter the time.");
time = input.nextDouble();
System.out.println("Distance equals: " + initialVelocity * time);
}
}
}
}
Thank you all for helping!
Upvotes: 0
Views: 107
Reputation: 34
Did it also terminate the program when you put in the input a 1?
When:
if (switchNumber == 1) //the input is not a 1 the program will terminate
Try it with a switch:
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 is for projectile motion.");
System.out.println("Which equation would you like to 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();
switch (switchNumber){
case 1: System.out.println("Tell me which variable you'd like to solve for.");
variableCaseOne = input.next();
if (variableCaseOne.equals("d")) {
System.out.println("Enter the Initial velocity.");
initialVelocity = input.nextDouble();
System.out.println("Enter the time.");
time = input.nextDouble();
System.out.println("Distance equals: " + initialVelocity * time);
}
case 2 : // your next case
}
}
Andrea Bori answer is the main Solution for your Problem
Upvotes: 0
Reputation: 190
if i have understood correctly try to change
variableCaseOne = input.nextLine();
to
variableCaseOne = input.next();
it works for 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 is for projectile motion.");
System.out.println("Which equation would you like to 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 which variable you'd like to solve for.");
variableCaseOne = input.next();
if (variableCaseOne.equals("d"))
{
System.out.println("Enter the Initial velocity.");
initialVelocity = input.nextDouble();
System.out.println("Enter the time.");
time = input.nextDouble();
System.out.println("Distance equals: " + initialVelocity * time);
}
}
input.close();
}
Upvotes: 3