gpt4 book ai didi

java - 用枚举构建象限

转载 作者:行者123 更新时间:2023-12-01 11:45:22 25 4
gpt4 key购买 nike

我正在学习枚举,但我真的不知道它是如何工作的以及如何开始。以下是我正在做的事情。

// An enumeration which models the four quadrants of the 2D Cartesian
// plane. It has exactly four elements: Q1, Q2, Q3, Q4. It is likely
// that giving each some fields will make the implementation easier.
// A private constructor is also useful.

我首先创建了一些字段和一个构造函数:

private int x;
private int y;
private Quadrant (int x, int y)
{
this.x = x;
this.y = y;
}

public enum Quadrant { Q1,Q2,Q3,Q4;} //is this right?

// true if x-coordinates are positive in the quadrant, false otherwise

public boolean xPositive();

// true if y-coordinates are positive in the quadrant, false otherwise

public boolean yPositive();

// Return a String which represents the signs of the coordinates in
// the Quadrant as

// (+,+) for Q1
// (-,+) for Q2
// (-,-) for Q3
// (+,-) for Q4

public String signPair();

对于signPair,我真的不知道它意味着什么。如何使 (+,+) 与 Q1 相关?

// Return the Quadrant that would result from flipping the sign (pos
// to neg or neg to pos) of the x-coordinate in this Quadrant..

public Quadrant flipX();

// Given two integers, determine return the quadrant in which they
// reside. If either x or y is 0, return one of the valid quadrants
// it might be assigned to (this case is not tested).

public static Quadrant fromInts(int x, int y);

// Accept an arbitrary number of command line arguments. Adjacent
// pairs of arguments are treated as (x,y) coordinates. Print the
// quadrant in which the pair resides along with the signPair(). If
// an odd number of arguments is given, ignore the last
// argument. Any argument that cannot be converted to an integer
// should raise an exception on encountering it.

public static void main(String [] args)
  • 尝试将每个字符串参数转换为整数。函数 Integer.parseInt() 对于此类转换很有用。任何转换为​​整数的失败都应该引发异常。 Integer.parseInt() 遇到问题时会出现异常,因此无需显式抛出任何内容。

  • 将相邻参数视为整数对。在处理参数时,您将一次查看两个参数。

  • 对于每对整数,生成该对所属的象限。

  • 打印出这对数字,并使用象限打印出数字的符号和它们所属的象限。使用以下通用格式:

    (1,5) 有符号 (+,+) 并且位于 Q1

  • 如果参数数量为奇数,则忽略最后一个参数,因为它是没有 y 坐标的 x 坐标。

最佳答案

您可以通过将基本属性作为每个枚举的属性传递来减少代码量。 signPair 使用一些基本算术属性来计算翻转象限的序数。

public enum Quadrant { 
Q1(true,true),
Q2(false, true),
Q3(false, false),
Q4(true, false);

private boolean xpos;
private boolean ypos;
Quadrant( boolean xpos, boolean ypos ){
this.xpos = xpos;
this.ypos = ypos;
}
public boolean xPositive(){
return xpos;
}
public boolean yPositive(){
return ypos;
}
public String signPair(){
return "(" + (xpos ? "+" : "-") + "," + (ypos ? "+" : "-") + ")";
}
public Quadrant flipX(){
return values()[ordinal() + 1 - 2*(ordinal()%2)];
}
public static Quadrant fromInts(int x, int y){
if( x >= 0 ){
return y >= 0 ? Quadrant.Q1 : Quadrant.Q4;
} else {
return y >= 0 ? Quadrant.Q2 : Quadrant.Q3;
}
}
}

您还可以为每个枚举定义方法:

public enum Quadrant { 
Q1(true,true){
public Quadrant flipX(){ return Quadrant.Q2; }
public String signPair(){ return "(+,+)"; }
},
Q2(false, true){
public Quadrant flipX(){ return Quadrant.Q1; }
public String signPair(){ return "(-,+)"; }
},
Q3(false, false){
public Quadrant flipX(){ return Quadrant.Q4; }
public String signPair(){ return "(-,-)"; }
},
Q4(true, false){
public Quadrant flipX(){ return Quadrant.Q3; }
public String signPair(){ return "(+,-)"; }
};
//...
public abstract String signPair();
public abstract Quadrant flipX();
//...
}

这是一个测试:

public static void main(String[] args) {
for( Quadrant q: Quadrant.values() ){
System.out.println( q );
System.out.println( q.xPositive() );
System.out.println( q.yPositive() );
System.out.println( q.signPair() );
System.out.println( q.flipX() );
}
for( int x = -1; x <= 1; ++x ){
for( int y = -1; y <= 1; ++y ){
System.out.println( Quadrant.fromInts(x, y) );
}
}
}

要运行所需的主进程参数:

 public static void main(String[] args) {
for( int i = 2; i <= args.length; i++ ){
int x = Integer.parseInt( args[i-2] );
int y = Integer.parseInt( args[i-1] );
Quadrant q = Quadrant.fromInts(x, y);
System.out.println("( + x + "," + y + ") in " + q + " " + q.signPair());
}
}

关于java - 用枚举构建象限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29185112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com