2015-10-01 5 views
-1
/* This program simulates a bouncing ball by computing its height 
* in feet each second as time passes on a simulated clock. 
* Stop at the fifth bounce. 
* 
* 
* Compile: javac GabeVergen_Ball.java 
* Run GabeVergen_Ball 
*/ 

// Import scanner 

    import java.util.Scanner; 

// Identify main class 

    public class GabeVergenBall 
    { 

// Identify method 

    public static void main (String args[]) 
    { 
// Create the scanner 

    Scanner keyboard = new Scanner(System.in); 

// User prompt 

    System.out.println("Enter the velocity of the ball: "); 

//Identify velocity 

    double velocity; 

//Input is read 

    double velocity = keyboard.nextDouble(); 

// Identify variables 

    int time = 0; 
    int bounce = 0; 
    double height = 0; 

//If bounce is less than 5, execute 

    while(bounce < 5) 
    { 
//Time and height are displayed 

    System.out.println("Time: " + time); 
    System.out.println("Height:" + height); 

//Update variable time 

    time++; 

//Uptade variables velocity and height 

    height += velocity; 
    velocity -= 32; 

//If height is less than 0, execute 

     if (height < 0) 
     { 
//In order to simulate the bounce, multiply height and velocity by -0.5 

      height *= -0.5; 
      velocity *= -0.5; 

     //Display bounce 

      System.out.println("Bounce!"); 

     //Bounce count 
      bounce++; 
    } //end of if statement 
    }//end of while statement 
    //Print end statement when ball stops 

     System.out.println("Stop"); 

    }//End of method 
} 
+0

В в какой строке возникает исключение? – dxdy

+0

Файл: (нет связанного файла) [строка: (нет источника)] Ошибка: java.lang.ArrayIndexOutOfBoundsException: 51966 –

+0

Опубликовать трассировку стека и определить самый высокий стек в трассе, который указывает на класс, который вы написали, и что номер строки. – Kon

ответ

0

Исходный код, который вы предоставили, не имеет ошибок, кроме двойного объявления скорости. (для исправления просто удалить первое объявление).

Вот результат, который я получаю при заданной скорости = 0.0 (я не хотел вводить ввод с клавиатуры).

C:\temp>java bounce Enter the velocity of the ball: Time: 0 Height:0.0 Time: 1 Height:0.0 Bounce! Time: 2 Height:16.0 Time: 3 Height:48.0 Time: 4 Height:48.0 Time: 5 Height:16.0 Bounce! Time: 6 Height:24.0 Time: 7 Height:72.0 Time: 8 Height:88.0 Time: 9 Height:72.0 Time: 10 Height:24.0 Bounce! Time: 11 Height:28.0 Time: 12 Height:84.0 Time: 13 Height:108.0 Time: 14 Height:100.0 Time: 15 Height:60.0 Bounce! Time: 16 Height:6.0 Time: 17 Height:58.0 Time: 18 Height:78.0 Time: 19 Height:66.0 Time: 20 Height:22.0 Bounce!

Я задаюсь вопросом, может ли проблема быть чем-то иным, чем исходный код, который вы предоставляете. Можете ли вы получить простую программу «Hello World» для компиляции и выполнения?

0

Проблема:

Проблема в том, папка, в которой вы хранения файла

Решение

Сохранить файл в папке с тем же именем, что класс

Смежные вопросы