0

我需要下面的代码来拥有一个 BalancedString 的默认构造函数,它将 str 初始化为空字符串并将计数器重置为 0 该类的一个争论构造函数将字符串 s 传递给 str 并将计数器重置为零。BalancedString 类还提供了一个布尔方法,它是 boolean balance(),如果字符串包含平衡数量的括号,则返回 true

import java.util.*;
public class BalancedString {
    private static String str;


    public BalancedString()
    {
        str = "";

    }

    public BalancedString(String s)
    {
        s = "";
        str = s;

}



public boolean balanced(){

    return true;

}
public static void main(String[] args) {
    int n = 0;
    CounterFancy.setCounter(n);
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a string that has any number of Left and right Parenthesis");
    String s = input.next();


        if (s.indexOf('(') != -1)

            CounterFancy.incCounter();

        if (s.indexOf(')') != -1)

            CounterFancy.decCounter();


    int counterValue = CounterFancy.getCounter();
    if (counterValue == 0)
        System.out.println("The string is Balanced");
    else 
        System.out.println("The string is NOT Balanced");
    input.close();
}

public String getStr()
{
    return str;
}
public String setStr(String s)
{
    str = s;
    return str;
}

}

以下是我从中获得 CounterFancy 类的另一个项目,但问题在上面^^ 为什么这只是输出它是平衡的

//Joe D'Angelo
//CSC 131-03
//Chapter 10 Programming Assignment 5a.
//Takes the user's input of whether they want the counter to be negative or positive and outputs
//10 values of the user's selected input, then restarts the counter at 0
import java.util.*;
public class CounterFancy { //I messed up the first time and had to change FancyCounter to CounterFancy that is why this is changed

    private static int counter;

    public CounterFancy()
    {
        counter = 0;        
    }

    public  CounterFancy(int n){
        counter = n;
    }

    public static int incCounter() //inc stands for increment
    {
            counter++;
        return counter;
    }
    public static int decCounter() //dec stands for decrement
    {
        counter--;
        return counter;
    }

    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.println("Press 1 for Possitive or Press 2 for Negative");
        int reply = input.nextInt();

        if (reply == 1)
        {
        for (int i = 1; i <=10; i ++)
        System.out.println("counter: " + CounterFancy.incCounter());
        CounterFancy.setCounter(5);
        System.out.println("Counter: " + CounterFancy.getCounter());

        }

        if (reply == 2)
        {
            for (int i = 1; i <=10; i ++)
                System.out.println("counter: " + CounterFancy.decCounter());
            CounterFancy.setCounter(5);
            System.out.println("Counter: " + CounterFancy.getCounter());

        }
        input.close();
        }



    public static int getCounter()
    {
        return counter;
    }

    public static void setCounter(int n)
    {
        counter = 0;
    }

}
4

2 回答 2

1

这段代码存在逻辑问题:

String s = input.next();

if (s.indexOf('(') != -1)
    CounterFancy.incCounter();

if (s.indexOf(')') != -1)
    CounterFancy.decCounter();

int counterValue = CounterFancy.getCounter();
if (counterValue == 0)
    System.out.println("The string is Balanced");
else 
    System.out.println("The string is NOT Balanced");

您只搜索字符串一次 a(和一次 a )。如果字符串以任意顺序同时包含 a(和 a ),则计数器将始终计数 1,然后计数 0,并认为括号是平衡的。

您需要将计数放入循环中以检查括号是否平衡。您应该遍历每个字符并检查每个步骤的计数。如果每一步的计数都是非负数并且以 0 结束,则括号是平衡的。

于 2012-11-21T01:03:32.727 回答
1

您在BalancedString类定义中犯了几个错误。首先,该str字段不应该是static. 通过制作它static,所有实例共享相同的str字段。

其次,也许更关键的是,你没有BalancedString正确构建你的。您每次都将参数设置回空字符串!

public BalancedString(String s) {
        s = ""; // THIS LINE SHOULD NOT BE HERE!
        str = s;
}

最后,无论字符串如何,您的balanced()方法都会简单地返回。true你需要在这里实现一些逻辑。

关于主程序:您需要遍历所有字符,每个字符递增,每个字符 '('递减。而不是这个: ')'

if (s.indexOf('(') != -1)

        CounterFancy.incCounter();

if (s.indexOf(')') != -1)

    CounterFancy.decCounter();

你应该有一个这样的循环:

for (int i = 0; i < s.length(); ++i) {
    char c = s.charAt(i);
    if (c == '(')
        CounterFancy.incCounter();
    else if (c == ')')
        CounterFancy.decCounter();
}
于 2012-11-21T01:05:40.027 回答