在Java NetBeans中的Switch语句

huangapple 未分类评论53阅读模式
英文:

Switch Statement in Java netbeans

问题

// 使用相同的分数标准来为不同领域返回成绩,可以使用如下的 switch 语句
int attend = 5;
int job = 5;
int initiative = 5;

int getattendo; // 需要先声明变量

switch (attend) {
    case 1:
        getattendo = 5;
        break;
    case 2:
        getattendo = 4;
        break;
    case 3:
        getattendo = 3;
        break;
    case 4:
        getattendo = 2;
        break;
    case 5:
        getattendo = 1;
        break;
    default:
        getattendo = 0; // null
}
英文:

I have different fields using the same parameters i.e. same grading scale. I want to use switch statement to return grades for different fields using the same scale. Something like this. I thought that there was something like this: switch (attend, job, initiative) { that would combine the three variables.

int attend = 5;
int job = 5;
int initiative = 5;

switch (attend) {
    case  1:
        getattendo = 5;
        break;
    case  2:
        getattendo = 4;
        break;
    case  3:
        getattendo = 3;
        break;
    case  4:
        getattendo = 2;
    case  5:
        getattendo = 1;
        break;
    default:
        getattendo = 0; // null
}

Your help is appreciated.

fmk

答案1

得分: 0

switch(a,b,c) 是不可能的。

如果所有值都相同,只需使用其中一个值,并验证所有值是否相同。

然而,如果您想要使用多个数字进行 switch-case,有一些解决方法:


数学解决方案

例如,您可以使用质数来实现。由于您只想要在数字之间进行 switch,只要存在一个高于预期最大值(对于 attendprimejob)的质数,这就是可能的。

不是 switch(attend, job, initiative),而是 switch((attend*prime+job)*prime+initiative),不是 case (exampleAttend, exampleJob, exampleInitiative):,而是 case ((exampleAttend*prime+exampleJob)*prime+exampleInitiative):

注意 primeswitchcase 语句中必须相同。

请注意,您应该测试任何输入数字是否大于该质数。这在逻辑上将导致默认情况,但可能会导致冲突。

您还可以确保质数的四次方小于数据类型的最大值,否则可能会溢出。

另一方面,这种方法应该比第二种方法更高效。


简单字符串连接

另一种选择是使用字符串。由于数字的字符串表示是唯一的(针对该数字),并且它不包含某些字符(如空格),您可以将这些数字连接起来,并使用此类字符进行分隔。

不是 switch(attend, job, initiative),而是 switch(attend+" "+job+" "+initiative),不是 case (exampleAttend,exampleJob,exampleInitiative):,而是 case (exampleAttend+" "+exampleJob+" "+exampleInitiative):

显然,这比涉及质数的第一种方法更简单且更安全,但是字符串连接比整数乘法慢,因此可能会对性能产生影响。


另一种可能性是使用枚举。查看 @Hassam Abdelillah其他答案 以了解其工作原理。如果您喜欢枚举方法,请随意为其他答案投票。

英文:

Something like switch(a,b,c) is not possible.

If all values are the same, just use one of the valueslandmaybe verify that all values are the same).

However, there are workarounds if you want to switch-case with multiple numbers:


mathematical solution

For example, you could use prime numbers for this. As you only want to switch numbers, this is possible as long as there is a prime number higher than the highest expected value(for attend, prime and job).

Instead of switch(attend, job, initiative), you use switch((attend*prime+job)*prime+initiative) and instead of case (exampleAttend, exampleJob, exampleInitiative):, you use case ((exampleAttend*prime+exampleJob)*prime+exampleInitiative):

Note that prime must be the same in the switch and case statements.

Note that you should test if any of the input numbers is higher than the prime. This would logically lead to the default case but it could lead to collissions.

You may also want to make sure that the prime to the forth power is lower than the max value of the data type or there may be overflows.

On the other side, this method should be more performant than the second.


simple string concadation

Another option is to work with strings. As the string representation of a number is unique (to the number) and it does not contain some characters (like spaces), you can concadate those numbers and use such a character to seperate them.

Instead of switch(attend, job, initiative), you use switch(attend+" "+job+" "+initiative) and instead of case (exampleAttend,exampleJob,exampleInitiative):, you use case (exampleAttend+" "+exampleJob+" "+exampleInitiative):.

This is obviously easier and fail-safer than the first method involving prime numbers but there should be a performance impact as concadating strings is slower than multiplying ints.


Another possibility is to use enums. Look at the other answer by @Hassam Abdelillah
if you want to know how this works. If you like the enum approach, feel free to upvote the other answer.

答案2

得分: 0

Enum可以很好地与switch case配合使用。因此,您可以定义一个枚举,以表示您的值范围,如果它是一个有限且合理的值范围:

public enum OPTIONS {
    OPTION1(5, 5, 5),
    OPTION2(5, 2, 4),
    OPTION3(1, 2, 3),
    OPTION4(4, 4, 1);

    private final int attend;
    private final int jobs;
    private final int initiative;

    OPTIONS(int attend, int jobs, int initiative) {
        this.attend = attend;
        this.jobs = jobs;
        this.initiative = initiative;
    }

    // ... 可选的设置器和访问器
}

给定您创建的OPTIONS枚举,您可以使用switch来处理不同的情况:

switch (option) {
    case OPTION1:
        getattendo = 5;
        break;
    case OPTION2:
        getattendo = 4;
        break;
    case OPTION3:
        getattendo = 3;
        break;
    case OPTION4:
        getattendo = 2;
        break;
    default:
        getattendo = 0; // null
        break;
}

注意: 只有在您有有限数量的条件时,您的switch才是合法的。否则,请使用一个方法来计算您的结果。

英文:

Enum works well with switch cases. So, you can define an enum that represents your range of value of it is a finite and reasonable range of values :

public enum OPTIONS {
	OPTION1(5, 5, 5),
	OPTION2(5, 2, 4),
	OPTION3(1, 2, 3),
	OPTION4(4, 4, 1);

	private final int attend;
    private final int jobs;
    private final int initiative;

	Directive(int attend, int jobs, int initiative) {
		this.attend = attend;
		this.jobs = jobs;
		this.initiative = initiative;
	}

    // ... optional setters & getters
}

Given your create OPTION Enum, you can use a switch to handle the different cases :

switch (OPTION) {
    case  OPTION1:
        getattendo = 5;
        break;
    case  OPTION2:
        getattendo = 4;
        break;
    case  OPTION3:
        getattendo = 3;
        break;
    case  OPTION4:
        getattendo = 2;
        break;
    default:
        getattendo = 0; // null
        break;
}

Note: Your switch is legitimate only if you have a finite number of condition. Otherwise, use a method to calculate your result.

答案3

得分: 0

一个你使用的技巧是利用一元或操作来检查二进制位的存在。

这将帮助你根据不同条件开始切换。

这与Linux中的文件权限工作方式类似。

public class ScoreCombinator {
    public static final int ATTEND     = 1; // 二进制: 001
    public static final int JOB        = 2; // 二进制: 010
    public static final int INITIATIVE = 4; // 二进制: 100

    public static void main(String[] args) {
        evaluate(ATTEND | INITIATIVE);       // 参与和主动性
        evaluate(INITIATIVE | ATTEND | JOB); // 参与、工作和主动性
    }

    private static void evaluate(int value) {
        switch (value) {
            case ATTEND: {
                System.out.println("参与");
                break;
            }
            case ATTEND | JOB: {
                System.out.println("参与和工作");
                break;
            }
            case ATTEND | JOB | INITIATIVE: {
                System.out.println("参与、工作和主动性");
                break;
            }
            case ATTEND | INITIATIVE: {
                System.out.println("参与和主动性");
                break;
            }
            case JOB: {
                System.out.println("工作");
                break;
            }
            case JOB | INITIATIVE: {
                System.out.println("工作和主动性");
                break;
            }
            case INITIATIVE: {
                System.out.println("主动性");
                break;
            }
        }
    }
}
英文:

A trick you use utilizes the unary or operation for checking binary digit presence.

This will help get you started on switching according to various conditions.

This is similar to how file permissions work in Linux.

public class ScoreCombinator {
    public static final int ATTEND     = 1; // binary: 001
    public static final int JOB        = 2; // binary: 010
    public static final int INITIATIVE = 4; // binary: 100

    public static void main(String[] args) {
        evaluate(ATTEND | INITIATIVE);       // Attend and Initiative
        evaluate(INITIATIVE | ATTEND | JOB); // Attend, Job, and Initiative
    }

    private static void evaluate(int value) {
        switch (value) {
            case ATTEND: {
                System.out.println("Attend");
                break;
            }
            case ATTEND | JOB: {
                System.out.println("Attend and Job");
                break;
            }
            case ATTEND | JOB | INITIATIVE: {
                System.out.println("Attend, Job, and Initiative");
                break;
            }
            case ATTEND | INITIATIVE: {
                System.out.println("Attend and Initiative");
                break;
            }
            case JOB: {
                System.out.println("Job");
                break;
            }
            case JOB | INITIATIVE: {
                System.out.println("Job and Initiative");
                break;
            }
            case INITIATIVE: {
                System.out.println("Initiative");
                break;
            }
        }
    }
}

huangapple
  • 本文由 发表于 2020年4月11日 04:49:27
  • 转载请务必保留本文链接:https://java.coder-hub.com/61148474.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定