📄 cartest2.java
字号:
public class CarTest2 {
public static void turnOn( Car c ) {
c.isRunning = true;
}
public static void turnOff( Car c ) {
c.isRunning = false;
}
public static void accelerate( Car c ) {
switch( c.engineType ) {
case Car.V4:
speedUp( c, 2 );
break;
case Car.V6:
speedUp( c, 3 );
break;
case Car.V8:
speedUp( c, 4 );
break;
case Car.V10:
speedUp( c, 5 );
break;
}
}
public static void speedUp( Car c, int amount ) {
if( c.isRunning == false ) {
// Do nothing - car is not running!
return;
}
if( ( c.currentSpeed + amount ) >= c.topSpeed ) {
c.currentSpeed = c.topSpeed;
}
else {
c.currentSpeed += amount;
}
}
public static void decelerate( Car c ) {
if( c.isRunning == false ) {
// Do nothing - car is not running!
return;
}
if( ( c.currentSpeed - 5 ) <= 0 ) {
c.currentSpeed = 0;
}
else {
c.currentSpeed -= 5;
}
}
public static void main( String[] args ) {
// Define the attributes of the car
Car c1 = new Car();
c1.engineType = Car.V10;
c1.bodyType = Car.CONVERTIBLE;
c1.topSpeed = 185;
c1.isRunning = false;
c1.currentSpeed = 0;
// Do some things with the car
turnOn( c1 );
for( int i=0; i<10; i++ ) {
accelerate( c1 );
System.out.println( "Current Speed: " + c1.currentSpeed );
}
for( int i=0; i<5; i++ ) {
decelerate( c1 );
System.out.println( "Current Speed: " + c1.currentSpeed );
}
turnOff( c1 );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -