2014-11-09 136 views
0

我發佈了一段較大的代碼段,我遇到了麻煩。它應該自行運行。爲了進行測試,只需在第一個提示處輸入一個即可。一旦它運行print語句,程序就會終止而不是詢問變量。我不明白爲什麼。有人能幫我嗎?爲什麼我的代碼跳過if語句?

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

    } 
    } 
} 

謝謝大家的幫助!

+4

可能重複http://stackoverflow.com/questions/7056749/scanner-issue-when- using-nextline-after-nextxxx) – PakkuDon 2014-11-09 00:49:36

回答

3

主要的解決方案,如果我理解正確的嘗試:

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

與交換機試試吧改變

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(); 
} 
[nextXXX後使用nextLine當掃描儀問題(指
+0

如果這是你的解決方案,請投票支持=) – 2014-11-09 01:36:14

+0

用戶需要15點聲望才能投票。 – mattias 2014-11-09 08:00:24

+0

uppps耶對不起; P – 2014-11-09 16:31:54

0

當你在輸入1中輸入時,它是否也終止了程序?

當:

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

安德烈Bori的回答您的問題