为什么我的代码跳过if语句?

时间:2014-11-09 00:48:35

标签: java if-statement java.util.scanner user-input

我发布了一段我遇到问题的大段代码。它应该自己运行。出于测试目的,只需在第一个提示输入一个。一旦它运行print语句,程序就会终止,而不是要求变量。我不明白为什么。有人能帮助我吗?

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);
      }

    }
  }
}

谢谢大家的帮助!

2 个答案:

答案 0 :(得分:3)

如果我理解正确,请尝试更改

variableCaseOne = input.nextLine();

variableCaseOne = input.next();

它对我有用

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();
}

答案 1 :(得分:0)

当您输入1?

时,它是否也终止了程序

当:

if (switchNumber == 1)   //the input is not a 1 the program will terminate

使用开关尝试:

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回答是您问题的主要解决方案