为什么我的代码只输出字符串是平衡的

时间:2012-11-21 01:00:06

标签: java

我需要以下代码才能有一个BalancedString的默认构造函数,它将str初始化为空字符串并将计数器重置为0该类的一个arguement构造函数将字符串s传递给str并将counter重置为零。 BalancedString类还提供了一个boolean方法,它是boolean balanced(),如果字符串包含平衡量的括号,则返回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;
    }

}

2 个答案:

答案 0 :(得分: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");

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

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

答案 1 :(得分: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();
}