java程序不執行編譯不報錯
『壹』 java中throw拋出的一些異常,程序不進行處理程序編譯也不會錯誤
不會,拋出異常本意就是在某些不滿足條件的時候終止程序運行,但是也可以選擇捕獲處理,捕獲後就不會使程序終止。但是為什麼有的系統方法必須讓你捕獲呢?那是因為該方法使用throws關鍵字聲明了,作用就是將throw拋出的異常顯示的交給調用者處理,如果調用者不處理,就不try-catch那麼才會編譯不通過。
『貳』 java 中寫出的類為什麼編譯不報錯但是不能運行,代碼沒錯啊
//看樓主問為什麼能編譯但不能運行,說明樓主應該是Java的初學者吧,Java程序其實編譯和運行是兩回事,沒有必然聯系的。剛才你的問題,在下面有注釋的,這樣就可以運行了,記得把類修改為test.java哦。運行下面
import java.util.*;
//這里不能用car ,重新起一個名字,因為和下面的class Car沖突了。
public class test {
private static Scanner input;
public static void main(String args[]){
input = new Scanner(System.in);
System.out.println("請輸入車主姓名,車速,方向盤角度");
Car car=new Car(input.next(),input.nextFloat(),input.nextFloat());
System.out.println("車主姓名為:"+car.getOwnerName());
System.out.println("當前車速為:"+car.getCurSpeed());
System.out.println("當前方向盤角度為:"+car.getCurDirInDegree());
System.out.println("修改車速");
System.out.println("新的車速為:");
car.changeSpeed(input.nextFloat());
System.out.println("在調用changeSpeed(80)後,車速為"+car.getCurSpeed());
car.stop();
System.out.println("在調用stop()後,車速為"+car.getCurSpeed());
}
}
class Car {
private String ownerName; //車主姓名
private float curSpeed; //當前車速
private float curDirInDegree; //當前方向盤轉向角度
public Car (String ownerName){
this.ownerName=ownerName;
}
public Car (String ownerName, float speed, float dirInDegree){
this(ownerName);
curSpeed=speed;
curDirInDegree=dirInDegree;
}
public String getOwnerName() { //提供對車主姓名的訪問
return ownerName;
}
public float getCurDirInDegree() { //提供對當前方向盤轉向角度的訪問
return curDirInDegree;
}
public float getCurSpeed() { //提供對當前車速的訪問
return curSpeed;
}
public void changeSpeed(float curSpeed) { //提供改變當前的車速
this.curSpeed=curSpeed;
}
public void stop(){ //提供停車
curSpeed = 0;
}
}