使用java中的开关案例和布尔值评估字符串中的字母

发布于 2025-01-24 06:11:24 字数 1193 浏览 0 评论 0原文

我正在学习,最近我们开始学习Java。 我有一个任务使用交换机和布尔值识别pangrams。任务指出:

  1. 创建一个字符串“句子”,其值是“从编织的黄麻袋中迅速挑选了六十个拉链”。

  2. 创建26个布尔变量,称为z

    a。布尔a,b,…y,z;

    b。 a,b,…y,z = true;

  3. 创建一个循环,将在句子的每个字母上迭代

    a。小心您在循环中用作字母的迭代器 '我'将被采取!

  4. 使用您制作的开关案例和布尔值,识别字母并将相应的布尔人设置为true。

  5. 一旦句子被充分处理,评估每个布尔值的价值:

    a。如果所有人都是真实的,请打印消息'句子 “”是pangram!'

    b。如果他们中的任何一个是错误的,请打印消息'句子 “”不是pangram!'

我不确定是否只是任务的措辞使我绊倒了,但是我不确定如何执行它。 这就是我目前得到的:

String sentence = "Sixty zippers were quickly picked from the woven jute bag.";
        Boolean a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;
        
        a = b = c = d = e = f = g = h = i = j = k = l = m = n = o = p = q = r = s = t = u = v = x = y = z = true;
        
            for (int letter = sentence.length() - 1; letter <= 0; letter++) {
                
                int character = sentence.charAt(letter);
                
                switch(character) {
                case a:
                case b:
                case c:

当我这样做的情况下,显然不会转换,那么我将如何评估每个角色?

I'm on course and we've recently started learning Java.
I've got a task identify pangrams using switch-cases and booleans. The task states:

  1. Create a String ‘sentence’ with the value “Sixty zippers were quickly picked from the woven jute bag.”

  2. Create 26 Boolean variables named a to z

    a. Boolean a, b, … y, z;

    b. a, b, … y, z = true;

  3. Create a loop that will iterate over each letter of the sentence

    a. Be careful of the iterator you use within your loop as the letter
    ‘i’ will already be taken!

  4. Using switch-cases and the Booleans you made, identify the letter and set the corresponding Boolean to true.

  5. Once the sentence has been fully processed, evaluate the value of each Boolean:

    a. Should all of them be true, print the message ‘the sentence
    “” is a pangram!’

    b. Should any of them be false, print the message ‘the sentence
    “” is not a pangram!’

I'm not sure if it's just the wording of the task that's tripping me up, but I'm not sure how to execute it.
This is what I've got at the moment:

String sentence = "Sixty zippers were quickly picked from the woven jute bag.";
        Boolean a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;
        
        a = b = c = d = e = f = g = h = i = j = k = l = m = n = o = p = q = r = s = t = u = v = x = y = z = true;
        
            for (int letter = sentence.length() - 1; letter <= 0; letter++) {
                
                int character = sentence.charAt(letter);
                
                switch(character) {
                case a:
                case b:
                case c:

When I do the case like this, obviously it won't convert, so how would I evaluate each character?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青萝楚歌 2025-01-31 06:11:24

正如评论中指出的那样,for循环是不正确的。要按顺序迭代字符:

for (int letter = 0; letter < sentence.length(); letter++) {

从字符串中获取字符时,请将其转换为较低的情况(否则,您需要一个单独的案例 'a'a'和'a'):

char character = Character.toLowerCase(sentence.charAt(letter)); 

开关语句:

switch(character) {
    case 'a': 
        a = false; 
        break; // needed to avoid fall-through to case 'b'
    case 'b': 
        b = false; 
        break; 
    ...

现在您可以测试是否将所有变量设置为false:

boolean isPangram = !a && !b && ... && !z;

如果变量设置为false,代码将越来越简单明了在循环中的开头和true中。

As pointed out in the comments, the for loop is not correct. To iterate over the characters in order:

for (int letter = 0; letter < sentence.length(); letter++) {

When fetching a character from the string, convert it to lower case (otherwise, you'd need a separate case for both 'a' and 'A'):

char character = Character.toLowerCase(sentence.charAt(letter)); 

The switch statement:

switch(character) {
    case 'a': 
        a = false; 
        break; // needed to avoid fall-through to case 'b'
    case 'b': 
        b = false; 
        break; 
    ...

Now you can test if all variables were set to false:

boolean isPangram = !a && !b && ... && !z;

The code would be simpler and clearer if the variables were set to false in the beginning and to true in the loop.

愛放△進行李 2025-01-31 06:11:24

在不完整的解决方案中,您有很多错误。这看起来像学校作业,所以我不是编写代码,但是您应该解决一些问题。

  • 不要将布尔标志初始化为true。将它们设置为真
    你遇到那个角色。

    • 您可能需要将标志的类型更改为原始布尔值和/或您需要将它们初始化为false。
  • 您已将字符定义为int,但实际上应该是char
    • 请注意,使用int由于隐式铸造而起作用,但可能会令人困惑。
  • 案例名称应单句话,以表明它们是字符而不是
    比您在开始时定义的变量。
  • 在每种情况下,设置适当的标志并断开以防止秋季分配分配错误
  • 设置错误。您是从末端开始的,并增加了,因此它将立即停止。
    • 从开始(即初始化字母至0)
    • 更改条件以覆盖字符串的长度
    • 增量很好。

You have a lot wrong in an incomplete solution. This looks like a school assignment so I'm not writing code but here are some things you should address.

  • Do not initialize your boolean flags to true. Set them to true when
    you encounter that character.

    • you may want to change the type of the flags to be primitive boolean and/or you will want to initialize them to false.
  • You've defined character as an int but it should really be a char
    • note that using an int will work due to implicit casting but it may prove confusing.
  • The cases names should be in single quotes to indicate they are characters rather
    than the variables you defined at the beginning.
  • within each case, set the appropriate flag and break to prevent fall-through assignments
  • the for-loop is set up wrong. You are starting at the end and increment so it will stop immediately.
    • start at the beginning (i.e. initialize letter to 0)
    • change the condition to cover the length of the String
    • the incrementor is fine.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文