简体   繁体   English

检查字符串是否包含带for for循环的字符?

[英]Check if string contains a character with for loop?

I am currently working on a simple code that will check if an user inputted String contains character(s) that are specified in the for loop. 我目前正在研究一个简单的代码,它将检查用户输入的String是否包含for循环中指定的字符。

My current code 我目前的代码

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);
}

} }

As you can see, it checks if the string contains such characters and it will increase the counter of the character by one. 如您所见,它检查字符串是否包含此类字符,并将字符的计数器增加一。 However when I run it, I don't receive the results I predicted. 但是,当我运行它时,我没有收到我预测的结果。 If I input GGRY, it outputs 1 0 0 0. When the desired out put is 如果我输入GGRY,它输出1 0 0 0.当所需的输出为

0.5 0.25 0.25 0.0

Any help would be appreciated! 任何帮助,将不胜感激!

The problem is that S.contains returns true if the whole string contains the given character. 问题是如果整个字符串包含给定的字符,则S.contains返回true。 S.charAt should solve your problem: 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++;
}

Also, dividing integers will return an integer (rounded down). 此外,除以整数将返回一个整数(向下舍入)。 As such your output would always be 0 unless all the characters are the same. 因此,除非所有字符都相同,否则输出将始终为0 Just cast them to double before printing: 只需在打印前将它们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);

Edit: As pointed out by Sumit Gulati in a comment, a switch statement will have better performance in Java 7. Also, as David Conrad pointed out using only if s in the for loop would work too as the conditions are mutually exclusive. 编辑:正如在评论中指出萨米特古拉蒂,switch语句中会有更好的表现在Java 7中另外,大卫康拉德指出,仅使用if S IN的for作为条件是相互排斥的循环将工作太。

Your earlier code S.contains("some character") was finding the index of the character in the entire string. 你早期的代码S.contains("some character")是在整个字符串中找到字符的索引。 Use S.charAt(i) to specifically find the index at i th location in the string. 使用S.charAt(i)专门找到字符串中第i个位置的索引。 Finally, you need to convert the integer to floating point in order to print output as floating values. 最后,您需要将整数转换为浮点,以便将输出打印为浮点值。

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);
    }
}

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM