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