Java Switch 语句操作 Enum 大小写
我有一个java文件的以下片段:
Integer x; Integer y; Face facing;
enum Rotate { Clockwise, Anticlockwise };
enum Face { East, North, West, South };
并且无法弄清楚如何实现一个函数来改变对象的面(即对象面向的方向)。
该函数开始如下
private void rotateIt(Rotate rotateIt) {
{
我已经开始使用 switch 语句如下(下面的文本在上面的大括号内):
switch (facing)
case North : ...?;
case West : ...?;
case East : ...?;
case South : ...?;
我想使用 Clockwise
枚举将其从 East
到南
等并逆时针
进行相反的IYGWIM。
I have the following segment of a java file:
Integer x; Integer y; Face facing;
enum Rotate { Clockwise, Anticlockwise };
enum Face { East, North, West, South };
and am having trouble figuring out how to implement a function to change the Face of an object (i.e. the direction that the object is facing).
The function begins as follows
private void rotateIt(Rotate rotateIt) {
{
I have begun using the switch statement as follows ( below text is inside braces above ):
switch (facing)
case North : ...?;
case West : ...?;
case East : ...?;
case South : ...?;
I'd like to use the Clockwise
enumeration to turn it from East
to South
etc. and Anticlockwise
to do the reverse IYGWIM.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我应该可以追溯你的成绩的很大一部分!
I should get a large percent of your grade retroactively!
我会将旋转实现为面部方向的函数:
然后您可以执行以下操作:
此代码利用了很少使用的枚举的“序数”属性。因此,它要求这些值按逻辑顺序排列,例如(东、北、西、南)
I would implement rotate as a function of the Face orientation:
Then you can do things like:
This code makes use of the seldom used 'ordinal' property of Enums. It therefore requires that the values are in a logical turning order e.g. (east, north, west, south)
另一种选择是使用枚举来完成这项工作。
Another option is to use the enum to do the work.
等等...
and so on...
你开始得很好。以下是关于枚举操作必须执行的操作的更完整版本:
当然,可以优化此代码,但它让您了解如何在
switch
中处理enum
代码> 语句。You are starting fine. Here is a more complete version of what you have to do regarding enum manipulation:
Of course, this code could be optimized, but it gives you an idea of how to handle
enums
inswitch
statements.