Java中的switch语句

huangapple go评论66阅读模式
英文:

switch method in java

问题

我正在尝试使用switch方法来统计字符串中的元音和辅音数量。
在线上有很多关于这个问题的解决方案,但我必须确保字符(!,$,&),数字和空格不被包括在辅音中。

我目前有这个:

switch (ch) {
  case 'a':
  case 'e':
  case 'i':
  case 'o':
  case 'u':
    vowelCount++; 
    break;
  default:
    consonantCount++;
    break;
}

我该如何将default更改为一个特定的情况,只允许介于a和z之间的字符?

英文:

I'm trying to use the switch method to count the number of vowels and number of consonants in a string.
There is alot online for this problem but I must make sure characters like (!,$,&), numbers and spaces are not included as a consonants.

I have this atm.

  switch (ch) {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
      vowelCount ++; 
      break;
    default:
      consonantCount ++;
      break;
  }

How could I change the default to a specific case that only allows characters between a and z?

答案1

得分: 1

如@Oliver在他的回答中指出的,有其他(可以说更好的)方法来编写上述代码,而不使用switch语句。但我不同意他将在default中使用if语句描述为“不良做法”的看法。

在我看来,谈论代码质量时最重要的两点是:

  • 代码应该正确,即它应该按照预期的方式工作。
  • 代码应该足够可读,以便需要阅读和维护代码的人能够理解它。

(我认为我们都可以同意这些是良好代码的必要条件,即使不是充分条件,但我承认为了论点的简单性,我已经将其简化了。)

然而,基于“特定构造的预期用法”的推理(依我看来)基于两个错误的假设:

  • 假设Java的设计者实际上确实打算这样做...没有任何关于他们意图的文件证据。

  • 假设Java中的常见用法模式不能(或不应该)超越设计者的原始意图。

因此,出于这些原因,我不同意Oliver的断言,即在default中使用if语句是“不良做法”。这取决于代码在上下文中的正确性和可读性/可维护性。

英文:

> How could I change the default to a specific case that only allows characters between a and z?

The default case matches everything that has not be matched by other cases, and you can't change that. Non-default cases only match a single compile time constant, and you can't change that either.

So the closest you can come with a switch statement will be to use an if statement within the default case.

  switch (ch) {
  case 'a': case 'e': case 'i': case 'o': case 'u':
      vowelCount++; 
      break;
  default:
      if (ch >= 'a' && ch <= 'z') {
          consonantCount++;
      }
      break;
  }

As @Oliver points out in his Answer, there are other (arguably better) ways to write the above without using a switch statement. But I disagree with his characterization of if in default as "bad practice".

To my mind, the two most important things when talking about code quality are:

  • The code should be correct in the sense that it does what it is supposed to do.
  • The code should be sufficiently readable to whoever will need to read and maintain the code.

<sup>(I think we can all agree that these are necessary (if not sufficient) for good code, but I acknowledge that I have simplified this, for the sake of argument.)</sup>

However, reasoning that is based on "how specific constructs are intended to be used" is (IMO) based on two false assumptions:

  • An assumption that the designers of Java did actually intend this ... absent any documentary evidence of their intention.

  • An assumption that common usage patterns in Java cannot (or should not) evolve beyond the designers' original intentions.

For these reasons, I disagree with Oliver's blanket assertion that if statements in a default are "bad practice". It depends on correctness and readability / maintainability of the code in context.

答案2

得分: 0

一个switch语句不会很好地解决你的问题。

个人认为,在switch语句的default情况中嵌套一个if语句是不良实践,并且表明你的模式可能需要调整。例如

if (a == b || a == c || a == d)

可以改写为

switch (a) {
  case b:
  case c:
  case d:
    // 代码。
    break;
}

switchif语句有不同的用途。如果你需要在default情况中检查变量的条件,那么你可能应该使用一个if语句,或者添加更多的case

我建议以下方式:

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
  vowelCount++;
} else if (ch >= 'a' && ch <= 'z') { // 如果字符代码在'a'和'z'之间,包括这两个字符。
  consonantCount++;
}
英文:

A switch statement is not going to solve your problem nicely.

Personally, I consider it bad practice to nest an if statement inside the default case of a switch statement and is a sign that your pattern may need tweaking. For example

if(a == b || a == c || a == d)

could be written as

switch(a) {
  case b:
  case c:
  case d:
    // Code.
    break;
}

Switch and if statements serve different purposes. If you need to check a condition on the variable you are switching in your default case, then you probably should use an if statement or add more cases.

I suggest the following:

if(ch == &#39;a&#39; || ch == &#39;e&#39; || ch == &#39;i&#39; || ch == &#39;o&#39; || ch == &#39;u&#39;) {
  vowelCount++;
} else if(ch &gt;= &#39;a&#39; &amp;&amp; ch &lt;= &#39;z&#39;) { // If the character code is between &#39;a&#39; and &#39;z&#39;, inclusive.
  consonantCount++;
}

答案3

得分: 0

这一切看起来更清晰,如果你放弃你的开关。

private static final String VOWELS = "aeiou";

public void count(String in) {
    int vowel = 0, consonant = 0;
    for (char c : in.toCharArray()) {
        if (VOWELS.indexOf(c) > -1) vowel++;
        else if (c >= 'a' && c <= 'z') consonant++;
    }
    System.out.printf("Vowels: %d Consonants: %d\n", vowel, consonant);
}
英文:

This all seems a lot cleaner if you ditch your switch.

private static final String VOWELS = &quot;aeiou&quot;;

public void count(String in) {
    int vowel = 0, consonant = 0;
    for (char c : in.toCharArray()) {
        if (VOWELS(c) &gt; -1) vowel++;
        else if (c &gt;= &#39;a&#39; &amp;&amp; c &lt;= &#39;z&#39;) consonant++;
    }
    System.out.printf(&quot;Vowels: %d Consonants: %d\n&quot;, vowel, consonant);
}

huangapple
  • 本文由 发表于 2020年8月10日 09:17:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63332925.html
匿名

发表评论

匿名网友

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

确定