【问题标题】:Check if string contains a character with for loop?检查字符串是否包含带有for循环的字符?
【发布时间】:2017-08-27 16:20:03
【问题描述】:

我目前正在编写一个简单的代码,它将检查用户输入的字符串是否包含在 for 循环中指定的字符。

我当前的代码

import java.util.Scanner;
public class AutumnLeaves {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int G = 0;
    int R = 0;
    int Y = 0;
    int B = 0;
    String S = sc.nextLine();
    for (int i = 0; i < S.length(); i++) {
        if (S.contains("G")) {
            G++;
        } else {
            if (S.contains("R")) {
                R++;
            } else {
                if (S.contains("Y")) {
                    Y++;
                } else {
                    if (S.contains("B")) {
                        B++;
                    }
                }
            }
        }
    }
    int total = G + R + Y + B;
    System.out.println(G/total);
    System.out.println(R/total);
    System.out.println(Y/total);
    System.out.println(B/total);
}

}

如您所见,它会检查字符串是否包含此类字符,并将字符的计数器加一。但是,当我运行它时,我没有收到我预测的结果。 如果我输入GGRY,它会输出1 0 0 0。当期望的输出是

0.5 0.25 0.25 0.0

任何帮助将不胜感激!

【问题讨论】:

标签: java string for-loop char


【解决方案1】:

问题在于,如果整个字符串包含给定字符,S.contains 会返回 true。 S.charAt 应该可以解决你的问题:

for (int i = 0; i < S.length(); i++) {
    if (S.charAt(i) == 'G') G++;
    else if (S.charAt(i) == 'R') R++;
    else if (S.charAt(i) == 'Y') Y++;
    else if (S.charAt(i) == 'B') B++;
}

此外,除以整数将返回一个整数(向下舍入)。因此,除非所有字符都相同,否则您的输出将始终为 0。只需在打印前将它们投射到double

System.out.println((double) G/total);
System.out.println((double) R/total);
System.out.println((double) Y/total);
System.out.println((double) B/total);

编辑:正如 Sumit Gulati 在评论中指出的那样,switch 语句在 Java 7 中将具有更好的性能。此外,正如 David Conrad 指出的那样,在 for 循环中仅使用 ifs 也可以作为条件是互斥的。

【讨论】:

  • else 不是严格需要的,因为条件是互斥的。)
  • 如果您使用的是 Java 7,请使用 switch case。它具有更好的性能。
  • 我目前正在使用带有 Java8 的 IntelliJ Idea,但如果我将某些方法更改为 7,肯定会使用 switch case,谢谢。
【解决方案2】:

您之前的代码S.contains("some character") 是在整个字符串中查找字符的索引。使用S.charAt(i) 专门查找字符串中ith 位置的索引。 最后,您需要将整数转换为浮点数才能将输出打印为浮点值。

public class AutumnLeaves {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int G = 0;
        int R = 0;
        int Y = 0;
        int B = 0;
        String S = sc.nextLine();
        for (int i = 0; i < S.length(); i++) {
            if (S.charAt(i) == 'G') {
                G++;
            } else {
                if (S.charAt(i) == 'R') {
                    R++;
                } else {
                    if (S.charAt(i) == 'Y') {
                        Y++;
                    } else {
                        if (S.charAt(i) == 'B') {
                            B++;
                        }
                    }
                }
            }
        }
        int total = G + R + Y + B;
        System.out.println(G * 1.0 / total);
        System.out.println(R * 1.0 / total);
        System.out.println(Y * 1.0 / total);
        System.out.println(B * 1.0 / total);
    }
}

【讨论】:

  • 为什么将变量乘以 1.0 有效?当我将它乘以 1 时,我会得到完全不同的结果。
  • 乘以 double 类型的 1.0 会自动将表达式转换为 double 类型。所以 5/2 = 2 如果不是两倍。但是,5.0/2 = 2.5,因为它现在是双精度 :)
  • 啊,太酷了!因此,当我将其乘以 1 时,编译器实际上将其四舍五入为最接近的整数,因为它是一个整数。我认为添加 (double) 也会像 Leon 所说的那样起作用。
  • 我已经对答案投了赞成票,但我需要 15 声望才能看到它!对此感到抱歉
  • Glace 没问题。我很感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-09
  • 2013-05-18
  • 2017-01-20
  • 2013-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多