Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在尝试计数图表时,输出重复的次数与插入的字母重复的次数相同 专用静态无效计数字母(扫描仪sc){ System.out.println(“输入字符串”); 字符串s=sc.nextLine(); System.out.println(“输入字母”); char c=sc.nextLine().charAt(0); int res=0; 对于(int i=0;i_Java - Fatal编程技术网

Java 在尝试计数图表时,输出重复的次数与插入的字母重复的次数相同 专用静态无效计数字母(扫描仪sc){ System.out.println(“输入字符串”); 字符串s=sc.nextLine(); System.out.println(“输入字母”); char c=sc.nextLine().charAt(0); int res=0; 对于(int i=0;i

Java 在尝试计数图表时,输出重复的次数与插入的字母重复的次数相同 专用静态无效计数字母(扫描仪sc){ System.out.println(“输入字符串”); 字符串s=sc.nextLine(); System.out.println(“输入字母”); char c=sc.nextLine().charAt(0); int res=0; 对于(int i=0;i,java,Java,输出重复的次数和字母重复的次数一样多。你知道为什么以及如何修复它吗? 电流输出示例: 输入一个字符串 你好,世界 输入一封信 l Hello world中的0 l Hello world中的0 l 1l在Hello world中 2L在Hello world 2L在Hello world 2L在Hello world 2L在Hello world 2L在Hello world 2L在Hello world 3L在Hello world 3 l在Hello world中因为循环中存在print语句

输出重复的次数和字母重复的次数一样多。你知道为什么以及如何修复它吗? 电流输出示例:

输入一个字符串

你好,世界 输入一封信

l

Hello world中的0 l

Hello world中的0 l

1l在Hello world中

2L在Hello world

2L在Hello world

2L在Hello world

2L在Hello world

2L在Hello world

2L在Hello world

3L在Hello world


3 l在Hello world中

因为循环中存在print语句,所以每次执行循环时,它都会打印当前计数,这将为您提供多行/计数。将打印移到循环之外将确保它仅在解析整个字符串后执行一次:

private static void CountLetters(Scanner sc) {

    System.out.println("Enter a string");
    String s = sc.nextLine();

    System.out.println("Enter a letter");
    char c = sc.nextLine().charAt(0);

    int res = 0;
    for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) == c) 
            res++;

        System.out.println(res + " " + c + " i " + s);
        }
专用静态无效计数字母(扫描仪sc){
System.out.println(“输入字符串”);
字符串s=sc.nextLine();
System.out.println(“输入字母”);
char c=sc.nextLine().charAt(0);
int res=0;
对于(int i=0;i
如果不跟踪代码,则不应将输出语句放入循环中:

private static void CountLetters(Scanner sc) {
        System.out.println("Enter a string");
        String s = sc.nextLine();

        System.out.println("Enter a letter");
        char c = sc.nextLine().charAt(0);

        int res = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == c)
                res++;
        }
        // Moved print statement outside of the loop; changed "i" to "in".
        System.out.println(res + " " + c + " in" + s);
    }
int res=0;
对于(int i=0;i
您希望代码输出什么?根据我看到的情况,代码正在执行编写时应该执行的操作。是否希望它只打印一行,表示总计数?如果是,请将打印语句移到循环之外。“打印”语句在循环中。要执行一次,应该在循环的结束括号之后执行。天哪,我怎么会错过它。非常感谢大家!您是awesom:)
int res = 0;
for (int i = 0; i < s.length(); i++)
{
    if (s.charAt(i) == c)
        res++;
}
System.out.print(res + " " + c + " in" + s);