有没有更好的方法 - Java条件语句

时间:2013-09-15 15:22:31

标签: java

我刚刚开始编程。我为我的编程课写了这个类。它从驱动程序中提取参数,老师给了我们,然后将比例形状放入用户选择的jPanel的象限中。它运行正常,但我想知道是否有更高效,更清晰的编写代码的方法。老师没有对早期的项目给出任何反馈,所以我会问这里。谢谢你的帮助。

import java.awt.*;

 public class LearnGraphics {


public static void drawRectangle(Graphics g, int size, String cord_x, String cord_y) {
        int longside = size / 4;
        int shortside = size / 8;
        int x = 0;
        int y = 0;
        if(cord_x.equals("top")&&cord_y.equals("right")) {  
            x = size-(size/3);
            y = size/8;
        }
        if(cord_x.equals("top")&&cord_y.equals("left")) {  
            x = size/12;
            y = size/8;
        }
        if(cord_x.equals("bottom")&&cord_y.equals("right")) {  
            x = size-(size/3);
            y = size-(size/4);
        }
        if(cord_x.equals("bottom")&&cord_y.equals("left")) {  
            x =  size/12;
            y = size-(size/4);
        }
        g.drawRect(x, y, longside, shortside);

    }

public static void drawLine(Graphics g, int size, String cord_x, String cord_y) {

    int x = 0;
    int y = 0;
    int x1 = 0;
    int y1 = 0;
    if(cord_x.equals("top")&&cord_y.equals("right")) {  
        x = size/12;
        y = size/6;
        x1 = size/3;
        y1 = size/6;
    }
    if(cord_x.equals("top")&&cord_y.equals("left")) {  
        x = (size/2)+(size/6);
        y = size/6;
        x1 = size-(size/12);
        y1 = size/6;
    }
    if(cord_x.equals("bottom")&&cord_y.equals("right")) {  
        x = size/12;
        y = size-(size/6);
        x1 = size/3;
        y1 = size-(size/6);
    }
    if(cord_x.equals("bottom")&&cord_y.equals("left")) {  
        x = (size/2)+(size/6);
        y = size-(size/6);
        x1 = size-(size/12);
        y1 = size-(size/6);
    }
    g.drawLine(x, y, x1, y1 )  
}
public static void drawOval(Graphics g, int size, String cord_x, String cord_y) {
    int longside = size / 4;
    int shortside = size / 8;
    int x = 0;
    int y = 0;
    if(cord_x.equals("top")&&cord_y.equals("right")) {  
        x = size-(size/3);
        y = size/8;
    }
    if(cord_x.equals("top")&&cord_y.equals("left")) {  
        x = size/12;
        y = size/8;
    }
    if(cord_x.equals("bottom")&&cord_y.equals("right")) {  
        x = size-(size/3);
        y = size-(size/4);
    }
    if(cord_x.equals("bottom")&&cord_y.equals("left")) {  
        x =  size/12;
        y = size-(size/4);
    }
    g.drawOval(x, y, longside, shortside);    
   }
  }

3 个答案:

答案 0 :(得分:1)

如果您执行了if-else语句,那么一旦您输入其中一个语句就不会通过if语句,这样会更高效,而且您不会再问两次相同的条件

public static void drawRectangle(Graphics g, int size, String cord_x, String cord_y) {
    int longside = size / 4;
    int shortside = size / 8;
    int x = 0;
    int y = 0;
    if(cord_x.equals("top")) {  
        if(cord_y.equals("right"))
        {
           x = size-(size/3);
           y = size/8;
        }
        else
        {
           if(cord_y.equals("left"))
           {
               x = size/12;
               y = size/8;
           }
        }
    }
    else
    {

        if(cord_x.equals("bottom")) {  
           if(cord_y.equals("right"))
           {
              x = size-(size/3);
              y = size-(size/4);
           }
           else
           {
              if(cord_y.equals("left"))
              {
                 x =  size/12;
                y = size-(size/4);
              }
           }
        }
    }
    g.drawRect(x, y, longside, shortside);

}

除此之外,目前没有什么可担心的,因为现代计算机速度非常快,且代码不长,所以速度很快

答案 1 :(得分:0)

只需使用布尔方法。

例如:

  public static boolean isRight(){
          return cord_x.equals("right");
     }

现在对每个方法都这样做,比如isLeft(),isTop()等。现在你可以做这样的事情了。

else if(isRight() && isBottom()) 

else语句会使您的代码更有效率。

答案 2 :(得分:0)

我是enums的粉丝。这是使用枚举的方法。这还包括您的代码(在另一个类中)以及并排比较它们的驱动程序。 HTH。

package test;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class LearnGraphics {
    public static void drawRectangle(Graphics g, int size, String cord_x, String cord_y) {
        Quadrant q = Quadrant.valueOf(cord_x, cord_y);
        float x = q.horiz.rectX*size, y = q.vert.rectY*size;
        g.drawRect(Math.round(x), Math.round(y), size / 4, size / 8);
    }

    public static void drawLine(Graphics g, int size, String cord_x, String cord_y) {
        Quadrant q = Quadrant.valueOf(cord_x,cord_y);
        float x0 = q.horiz.lineX0*size, y  = q.vert.lineY*size, x1 = q.horiz.lineX1*size;
        g.drawLine(Math.round(x0), Math.round(y), Math.round(x1), Math.round(y));
    }

    public static void drawOval(Graphics g, int size, String cord_x, String cord_y) {
        Quadrant q = Quadrant.valueOf(cord_x,cord_y);
        float x = q.horiz.ovalX*size, y = q.vert.ovalY*size;
        g.drawOval(Math.round(x), Math.round(y), size / 4, size / 8);
    }


    public static void invertColor(Graphics g){
        Color c = g.getColor();
        g.setColor(new Color(255-c.getRed(), 255-c.getGreen(), 255-c.getBlue()));
    }

    public static void main(String[] arg){
        JFrame jf = new JFrame("Learn Enums (NEW WAY)");
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final int size=400;

        @SuppressWarnings("serial")
        JPanel jp = new JPanel(){
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.BLUE);
                drawRectangle(g, size, "left", "top");
                g.setColor(Color.ORANGE);
                drawOval(g, size, "left", "bottom");
                g.setColor(Color.GREEN);
                drawLine(g, size, "right", "bottom");
            }
        };

        jf.setContentPane(jp);
        jf.setBounds(120, 120, size, size);
        jf.setVisible(true);

        JFrame owjf = new JFrame("Learn Graphics (OLD WAY)");
        owjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        @SuppressWarnings("serial")
        JPanel owjp = new JPanel(){
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.BLUE);
                OldWays.drawRectangle(g, size, "left", "top");
                g.setColor(Color.ORANGE);
                OldWays.drawOval(g, size, "left", "bottom");
                g.setColor(Color.GREEN);
                OldWays.drawLine(g, size, "right", "bottom");
            };
        };
        owjf.setContentPane(owjp);
        owjf.setBounds(120+50+size, 120, size, size);
        owjf.setVisible(true);

    }

}
enum Quadrant{
    top_left(Vert.top,Horiz.left), 
    top_right(Vert.top,Horiz.right), 
    bottom_left(Vert.bottom,Horiz.left), 
    bottom_right(Vert.bottom,Horiz.right);

    private Quadrant(Vert v, Horiz h){
        vert=v;
        horiz=h;
    }
    final Vert vert;
    final Horiz horiz;
    public static Quadrant valueOf(String horiz, String vert){
        return valueOf(vert+"_"+horiz);
    }
    enum Vert{
        top(   1/8f, 1/6f, 1/8f),
        bottom(3/4f, 5/6f, 3/4f);
        private Vert(float rectY, float lineY, float ovalY){
            this.rectY=rectY;
            this.lineY=lineY;
            this.ovalY=ovalY;
        }
        final float rectY, lineY, ovalY;
    }
    enum Horiz{
        left( 1/12f,  2/3f, 11/12f, 1/12f),
        right( 1/3f, 1/12f,   1/3f,  2/3f);
        private Horiz(float rectX, float lineX0, float lineX1, float ovalX){
            this.rectX=rectX;
            this.lineX0=lineX0;
            this.lineX1=lineX1;
            this.ovalX=ovalX;
        }
        final float rectX, lineX0, lineX1, ovalX;
    }
}
class OldWays{
    /** INCLUDED SO YOU CAN TEST THE VERSIONS AGAINST EACH OTHER **/
    public static void drawRectangle(Graphics g, int size, String cord_y,
            String cord_x) {
        int longside = size / 4;
        int shortside = size / 8;
        int x = 0;
        int y = 0;
        if (cord_x.equals("top") && cord_y.equals("right")) {
            x = size - (size / 3);
            y = size / 8;
        }
        if (cord_x.equals("top") && cord_y.equals("left")) {
            x = size / 12;
            y = size / 8;
        }
        if (cord_x.equals("bottom") && cord_y.equals("right")) {
            x = size - (size / 3);
            y = size - (size / 4);
        }
        if (cord_x.equals("bottom") && cord_y.equals("left")) {
            x = size / 12;
            y = size - (size / 4);
        }

        System.out.printf("ow.drawRectangle(%d, %d, %d, %d);\n", x, y, longside, shortside);
        g.drawRect(x, y, longside, shortside);

    }

    public static void drawLine(Graphics g, int size, String cord_y,
            String cord_x) {

        int x = 0;
        int y = 0;
        int x1 = 0;
        int y1 = 0;
        if (cord_x.equals("top") && cord_y.equals("right")) {
            x = size / 12;
            y = size / 6;
            x1 = size / 3;
            y1 = size / 6;
        }
        if (cord_x.equals("top") && cord_y.equals("left")) {
            x = (size / 2) + (size / 6);
            y = size / 6;
            x1 = size - (size / 12);
            y1 = size / 6;
        }
        if (cord_x.equals("bottom") && cord_y.equals("right")) {
            x = size / 12;
            y = size - (size / 6);
            x1 = size / 3;
            y1 = size - (size / 6);
        }
        if (cord_x.equals("bottom") && cord_y.equals("left")) {
            x = (size / 2) + (size / 6);
            y = size - (size / 6);
            x1 = size - (size / 12);
            y1 = size - (size / 6);
        }
        System.out.printf("ow.drawLine(%d, %d, %d, %d);\n", x, y, x1, y1);
        g.drawLine(x, y, x1, y1);
    }

    public static void drawOval(Graphics g, int size, String cord_y,
            String cord_x) {
        int longside = size / 4;
        int shortside = size / 8;
        int x = 0;
        int y = 0;
        if (cord_x.equals("top") && cord_y.equals("right")) {
            x = size - (size / 3);
            y = size / 8;
        }
        if (cord_x.equals("top") && cord_y.equals("left")) {
            x = size / 12;
            y = size / 8;
        }
        if (cord_x.equals("bottom") && cord_y.equals("right")) {
            x = size - (size / 3);
            y = size - (size / 4);
        }
        if (cord_x.equals("bottom") && cord_y.equals("left")) {
            x = size / 12;
            y = size - (size / 4);
        }
        System.out.printf("ow.drawOval(%d, %d, %d, %d);\n", x, y, longside, shortside);
        g.drawOval(x, y, longside, shortside);
    }
}

注意:有时当您创建的枚举不仅仅是一个值 - 在这种情况下,它们附加了值 - 枚举代码本身看起来有点混乱,但看看客户端代码有多清晰!