Java Enum - Java 中的枚举和示例
来源:
FreeCodeCamp Java 中的 枚举(enumeration 或简称
enum )是一种特殊的数据类型,包含一组预定义的常量。通常,枚举用于处理不需要更改的值,例如一周中的几天、季节、颜色等。
在本文中,我们将了解如何创建
枚举以及如何将其值分配给其他变量。我们还将学习如何在
switch语句中使用枚举或循环遍历其值。
如何在 Java 中创建枚举
要创建
枚举,我们使用enum关键字。
这与使用class关键字创建类的方式类似。这是一个例子:
enum Colors {
RED,
BLUE,
YELLOW,
GREEN
}
在此代码中,我们创建了一个名为
Colors的枚举。您可能会注意到这里的所有
枚举值都是大写的 - 但这只是一般约定。如果数值写成小写,就不会出错。
枚举中的每个值都用逗号分隔。接下来我们将创建一个新变量并为其分配 enum 的值
之一。
enum Colors {
RED,
BLUE,
YELLOW,
GREEN
}
public class Main {
public static void main(String[] args) {
Colors red = Colors.RED;
System.out.println(red);
}
}
这类似于初始化任何其他变量。在代码中,我们初始化了
Colors变量,并使用点语法为其分配了
枚举值之一:
Colors red = Colors.RED; 。
请注意,我们可以在Main类中创建枚举,并且代码仍然有效:
public class Main {
enum Colors {
RED,
BLUE,
YELLOW,
GREEN
}
public static void main(String[] args) {
Colors red = Colors.RED;
System.out.println(red);
}
}
如果我们想获取任何值的序数,我们必须使用
ordinal()方法。这是另一个例子:
enum Colors {
RED,
BLUE,
YELLOW,
GREEN
}
public class Main {
public static void main(String[] args) {
Colors red = Colors.RED;
System.out.println(red.ordinal());
}
}
上述代码中的
red.ordinal()返回 0。
如何在 Switch 语句中使用 Enum
现在我们将学习如何在
switch语句中使用
枚举:
public class Main {
enum Colors {
RED,
BLUE,
YELLOW,
GREEN
}
public static void main(String[] args) {
Colors myColor = Colors.YELLOW;
switch(myColor) {
case RED:
System.out.println("The color is red");
break;
case BLUE:
System.out.println("The color is blue");
break;
case YELLOW:
System.out.println("The color is yellow");
break;
case GREEN:
System.out.println("The color is green");
break;
}
}
}
这是一个非常简单的示例,说明了如何在
switch语句中 使用
枚举。我们可以向控制台输出
“The color is Yellow” ,因为这是唯一符合switch语句条件的情况。
如何循环遍历枚举值
对于枚举,Java有一个
values()方法,它返回一个枚举值数组。我们将使用
foreach循环来迭代并打印 enum 的
值。你可以这样做:
enum Colors {
RED,
BLUE,
YELLOW,
GREEN
}
public class Main {
public static void main(String[] args) {
for (Colors allColors : Colors.values()) {
System.out.println(allColors);
}
}
}
结论
在本文中,我们了解了Java 中的
枚举是什么、如何创建枚举以及如何将其值分配给其他变量。我们还了解了如何将
枚举类型与
switch语句一起使用,以及如何循环遍历
枚举值。快乐编码!
Java 中的 While 循环:语法和用途
来源:
Dev.to今天我们将了解 Java 中while循环 的概念和语法,并了解如何使用此循环来迭代数组。
While 循环概念
while循环是 Java 中的基本循环类型之一,可在程序中用于多种用途。
与for循环一样,
while循环可用于根据条件执行操作。在
while循环中,计数器并不总是打开。
while循环的迭代次数取决于循环条件返回
true的频率。
与for循环不同,在大多数情况下, while循环中的初始化是可选的。有时,
while循环并不总是在循环中执行。让我们举一个具体的例子:
System.out.println("You Input a wrong number");
int i = input.nextInt();
while(i==1||i==2||i==3||i==4){
System.out.println("inside the while loop block");
}
System.out.println("outside the while loop block")
这里的代码使用
while循环根据某些条件验证用户输入,因此仅重复一次。
While 循环语法
while(condition){
}
while 循环条件采用一个返回布尔值
true或
false 的参数。当
true时,执行
while循环中的代码,当
false时,循环结束。
每个while循环的流程
while循环有一个可接受的流程:初始化 > 条件 > 语句执行。第一阶段是初始化阶段。它可以通过声明变量并初始化它来完成。第二阶段是条件阶段,该阶段必须返回
true或
false。如果返回
true ,则执行
while循环中的语句,如果返回
false,则循环结束。
使用 while 循环作为计数器
与
for循环一样,
while循环也可以用作计数器。下面的程序使用
while循环从 2 迭代到 20。这是为了打印偶数。
int j = 2;
while(j <=20){
System.out.println(j);
j+=2;
}
可以看到,在程序中,
j被初始化为 2,如果j小于或等于 20,则测试条件。如果返回
true,则执行该语句,如果返回
false,则
while循环结束。
while 循环可用于迭代数组
int[] age = {20, 30, 40, 40, 90};
int i = 0;
while (i < age.length)
{
System.out.println(age[i]);
i++;
}
在此代码中,我们有一个年龄数组,并使用
while循环来迭代该数组并打印出数组中的每个元素。为了检查数组中元素的总数,使用了
age.length,在上面的程序中
i作为索引。
while 循环中的 Break 语句
Break语句也可以用在
while循环中,就像
for循环一样:
int i = 1;
while (true)
{
System.out.println(i);
if (i == 20)
{
break;
}
i++;
}
在上面的程序中,
break语句用于无限期地停止程序执行。
While(true)意味着程序将始终返回
true。
while循环可用于验证用户输入
System.out.println("Select an option\n"+
"1.Add Users\n"+
"2.Delete Users");
int x = input.nextInt();
while(x==1||x==2){
}
System.out.println("You Input a wrong number");
在上面的程序中,使用
while循环来检查用户的输入并确保用户选择 1 或 2。如果用户选择不同的数字,则
System.out.println("您输入了错误的数字"); 。
带 while 循环的条件语句
int i = 0;
while(i<10){
i++;
if(i==5){
continue;
} System.out.println(i);
}
条件语句也可以用在
while循环中来跳过特定的迭代。在上面的程序中,
if语句用于检查
i==5,如果它返回
true则将跳过该特定迭代。
while循环设计
让我们使用
while循环创建一个猜谜游戏。
import java.util.*;
public class Main
{
static Scanner input = new Scanner(System.in);
static boolean hasWon;
static int count;
static int guess;
public static void main (String[] args)
{
System.out.println("Welcome to guess world");
System.out.println("Do you want to start the game? ");
System.out.println("1.Yes\n" +
"2.No");
int option= input.nextInt();
int guessNumber = (int) Math.floor(Math.random() * 10 + 1);
if (!( option == 2 ))
{
System.out.println("Welcome, choose a guess between 1 and 10\n" +
"you have only ten trials");
while (count <= 10)
{
int userGuess = input.nextInt();
if (userGuess == guessNumber)
{
hasWon = true;
System.out.println("Congratulation, you guessed right at " + count + " count");
count++;
break;
}
else if (userGuess < guessNumber)
{
hasWon = false;
System.out.println("Oops, you guessed a lower number, try an higher one");
count++;
}
else if (userGuess > guessNumber)
{
System.out.println("Oops, you guessed an high number, try an lower one");
hasWon = false;
}
}
if (hasWon == false)
{
System.out.println("You trials exceeds 10");
}
}
}
}
在此程序中,我们使用while循环 创建一个猜数字游戏。玩家只有 10 次尝试机会。一旦他玩游戏的次数超过10次,游戏就结束。如果玩家猜对了正确的数字,控制台上会显示一条祝贺消息。
结论
while循环可能看起来很复杂,但掌握起来并不难。它也是 Java 中第二常用的循环,可用于多种目的。
GO TO FULL VERSION