伪代码 java_伪代码到java

我在过去的一个小时左右,但我找不到任何帮助解决这个问题。我试图将这个伪代码转换为java,并不知道我做错了什么(它曾经打印过任何东西)。

function line(x0, x1, y0, y1)

boolean steep := abs(y1 - y0) > abs(x1 - x0)

if steep then

swap(x0, y0)

swap(x1, y1)

if x0 > x1 then

swap(x0, x1)

swap(y0, y1)

int deltax := x1 - x0

int deltay := abs(y1 - y0)

real error := 0

real deltaerr := deltay / deltax

int ystep

int y := y0

if y0 < y1 then ystep := 1 else ystep := -1

for x from x0 to x1

if steep then plot(y,x) else plot(x,y)

error := error + deltaerr

if error ≥ 0.5 then

y := y + ystep

error := error - 1.0我的转换是:

public static void line(int x0,int x1,int y0,int y1) {

boolean steep = Math.abs(y1 - y0) > Math.abs(x1 - x0);

if(steep) {

swap(x0, y0);

swap(x1, y1);

}

if (x0 > x1) {

swap(x0, x1);

swap(y0, y1);

}

int deltax = x1 - x0;

int deltay = Math.abs(y1 - y0);

float error = 0;

float deltaerr = deltay / (float)deltax;

int ystep;

int y = y0;

if(y0 < y1) ystep = 1;

else ystep = -1;

//for x from x0 to x1

for(int x = x0; x < x1;x++)

if (steep) plot(y,x);

else plot(x,y);

error = error + deltaerr;

if (error >= 0.5f) {

y = y + ystep;

error = error - 1.0f;

}

}

//method plot

private static void plot(int x, int y) {

System.out.println(x+":"+y);

}

//method swap

private static void swap(int x0, int x1) {

int copy = x0;

x0 = x1;

x1 = copy;

}有人可以帮忙吗?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值