2012-11-21 3 views
0

str을 빈 문자열로 초기화하고 카운터를 0으로 초기화하는 BalancedString의 기본 생성자를 사용하려면 다음 코드가 필요합니다. 클래스의 argue 생성자 중 하나가 문자열 s를 str에 전달합니다 카운터를 0으로 재설정합니다. BalancedString 클래스는 문자열을 괄호문자열이 균형 잡힌 코드 만 출력하는 이유

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있어 다른 프로젝트의 경우는 true 반환) (균형 부울하는 부울 방법을 제공 에서 클래스,하지만 문제는 이유는 그것이 균형이라고하고 출력하기입니다 ^^ 위입니다

코드의이 비트의 논리 문제가있다
//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; 
    } 

} 

답변

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(); 
} 
+0

내가 도움이 필요 이러한 변경을 어떻게 선생님은 나에게 아무것도 –

+0

을 말하지 않는다 질문은 매우 오해의 소지가 있습니다. 'BalancedString'은 실제로 사용되지 않습니다 (따라서 문제가 아닙니다). – Vulcan

+0

@ Vulcan - 'BalancedString'은 지금 무시할 수 있다고 생각하지만, OP가 결국 그것을 사용한다고 가정합니다. 마지막 단락에서는 문제의 핵심을 다뤘습니다. –

1

:

문자열을 한 번만 (, 한 번 )으로 검색합니다. 문자열에 ()이 순서대로 포함되어 있으면 카운터는 항상 1을 계산 한 다음 0을 계산하고 괄호가 균형을 이루고 있다고 생각합니다.

괄호가 균형을 이루고 있는지 여부를 확인하려면 루프에 계산을 넣어야합니다. 각 문자를 반복하여 각 단계에서 개수를 확인해야합니다. 각 단계에서 음수가 아니고 0에서 끝나는 경우 괄호가 균형을 이룹니다.

+0

내 루프가 있어야한다 무엇을 –

관련 문제