You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java数组元素比较提取问题:命题逻辑语句变量统计代码异常排查

问题原因分析

你的代码逻辑存在几个关键漏洞,直接导致统计结果完全不符合预期:

  1. 错误的计数逻辑
    你用双重循环实现的逻辑是:只要当前字符和后面某个字符不同,就给变量计数+1——这其实是在统计所有不同的字符对数量,而非唯一变量的数量。比如输入hello时:

    • h和e、l、l、o都不同,会被计数4次
    • e和l、l、o都不同,会被计数3次
    • 第一个l只和o不同,计数1次
    • 第二个l和o不同,计数1次
      加起来4+3+1+1=9,这就是你得到9的核心原因。
  2. 变量存储的逻辑漏洞
    你直接把charStatement[i]存入variables[i],这会导致重复字符(比如两个l)被存在数组的不同位置,而且数组未赋值的位置是默认空字符,输出时会出现无效内容。

  3. 缺少去重检查
    你没有判断当前字符是否已经被记录过,导致同一个变量会被重复处理多次。


修复方案

方案1:使用Set(最简洁高效)

Java的Set集合天然自带去重特性,完美适配这个统计场景:

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class PropLogic {
    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        System.out.println("Enter your propositional statement:");
        String statement = stdin.nextLine();
        
        Set<Character> variables = new HashSet<>();
        for (char c : statement.toCharArray()) {
            // 仅将字母视为命题变量
            if (Character.isLetter(c)) {
                variables.add(c);
            }
        }
        
        System.out.println("The number of variables is " + variables.size());
        System.out.print("The variables are ");
        // 遍历输出所有唯一变量
        for (char var : variables) {
            System.out.print(var + " ");
        }
        stdin.close();
    }
}

输入hello时,输出会是:

The number of variables is 4
The variables are h e l o 

方案2:手动实现去重(贴合你最初的思路)

如果你想保持用数组存储的逻辑,可以修改为:每次处理字符前先检查是否已经在变量数组中,仅新增未记录的变量:

import java.util.Scanner;

public class PropLogic {
    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        String statement;
        int numOfVariables = 0;
        System.out.println("Enter your propositional statement:");
        statement = stdin.nextLine();
        char[] charStatement = statement.toCharArray();
        // 字母最多26个,初始化足够大的数组
        char[] variables = new char[26];
        
        for (char c : charStatement) {
            if (Character.isLetter(c)) {
                // 检查当前字符是否已被记录
                boolean exists = false;
                for (int i = 0; i < numOfVariables; i++) {
                    if (variables[i] == c) {
                        exists = true;
                        break;
                    }
                }
                // 未记录过则加入数组并更新计数
                if (!exists) {
                    variables[numOfVariables] = c;
                    numOfVariables++;
                }
            }
        }
        
        System.out.println("The number of variables is " + numOfVariables);
        // 仅输出数组中已赋值的部分
        System.out.println("The variables are " + new String(variables, 0, numOfVariables));
        stdin.close();
    }
}

这个版本会严格统计唯一变量的数量,输入hello时就能得到正确的4个变量结果。


内容的提问来源于stack exchange,提问作者adc395

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:55:40