首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >到java的伪代码

到java的伪代码
EN

Stack Overflow用户
提问于 2012-09-10 02:57:28
回答 1查看 439关注 0票数 0

我在过去的一个小时左右环顾四周,但在这个问题上我找不到任何帮助。我正在尝试将这个伪代码转换成java,但是我不知道我做错了什么(它曾经打印过任何东西)。

代码语言:javascript
复制
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

我的转换是:

代码语言:javascript
复制
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;
    }

有人能帮帮忙吗?

EN

回答 1

Stack Overflow用户

发布于 2012-09-10 02:59:17

不能在int中使用这样的swap()方法。因为它们是基元,所以它们是通过值传递的,并且更改方法中的局部变量不会对用作参数的变量产生任何影响。

直接在line()方法中进行交换。

第二件事是你的for循环看起来是错误的。根据您的缩进,您可能希望在整个块周围使用大括号。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12341991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档