0
该程序编译正确,但不会运行时,当我尝试输入宽度和高度的值,而不是给我的错误消息“线程中的异常”主要“java .lang.IllegalArgumentException:宽度和高度必须为正值“。如何正确地声明我在主方法之外使用Scanner定义的静态变量? (初学编程的,对不起,如果这是显而易见的)非法参数异常 - 如何声明方法中定义的静态变量
public class Animation {
static int width;
static int height;
static double x0;
static double y0;
public static void main (String[] args) {
getInputs();
initStdDraw(width, height);
drawFace(x0, y0);
}
public static void initStdDraw(int width, int height) {
StdDraw.setCanvasSize(width, height);
StdDraw.setXscale(0, width);
StdDraw.setYscale(0, height);
StdDraw.rectangle(width/2, height/2, width/2, height/2);
}
public static void getInputs() {
Scanner console = new Scanner(System.in);
System.out.println("Please provide a canvas width and height: ");
int width = console.nextInt();
int height = console.nextInt();
System.out.println("Please provide a starting position: ");
double x0 = console.nextDouble();
double y0 = console.nextDouble();
请告知使用精确的线,你得到这个错误。也请打印宽度/高度值。 – john16384
你的意思是'x0 = console.nextDouble();'而不是'double x0 = console.nextDouble();'? (对于其他3个变量也是如此)如果你打算分配静态变量或定义一个新的,无关的局部变量? –
是的!这解决了我的问题,非常感谢。你介意解释为什么我以前的版本给我一个错误? – kellabd