使用java中的开关案例和布尔值评估字符串中的字母
我正在学习,最近我们开始学习Java。 我有一个任务使用交换机和布尔值识别pangrams。任务指出:
创建一个字符串“句子”,其值是“从编织的黄麻袋中迅速挑选了六十个拉链”。
创建26个布尔变量,称为z
a。布尔a,b,…y,z;
b。 a,b,…y,z = true;
创建一个循环,将在句子的每个字母上迭代
a。小心您在循环中用作字母的迭代器 '我'将被采取!
使用您制作的开关案例和布尔值,识别字母并将相应的布尔人设置为true。
一旦句子被充分处理,评估每个布尔值的价值:
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:
Create a String ‘sentence’ with the value “Sixty zippers were quickly picked from the woven jute bag.”
Create 26 Boolean variables named a to z
a. Boolean a, b, … y, z;
b. a, b, … y, z = true;
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!Using switch-cases and the Booleans you made, identify the letter and set the corresponding Boolean to true.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如评论中指出的那样,for循环是不正确的。要按顺序迭代字符:
从字符串中获取字符时,请将其转换为较低的情况(否则,您需要一个单独的
案例
'a'a'和'a'
):开关语句:
现在您可以测试是否将所有变量设置为false:
如果变量设置为
false
,代码将越来越简单明了在循环中的开头和true
中。As pointed out in the comments, the for loop is not correct. To iterate over the characters in order:
When fetching a character from the string, convert it to lower case (otherwise, you'd need a separate
case
for both'a'
and'A'
):The switch statement:
Now you can test if all variables were set to false:
The code would be simpler and clearer if the variables were set to
false
in the beginning and totrue
in the loop.在不完整的解决方案中,您有很多错误。这看起来像学校作业,所以我不是编写代码,但是您应该解决一些问题。
你遇到那个角色。
字符
定义为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.
you encounter that character.
character
as anint
but it should really be achar
int
will work due to implicit casting but it may prove confusing.than the variables you defined at the beginning.
letter
to 0)