2012-11-21 25 views
0

我需要下面的代碼來獲得BalancedString的默認構造函數,它將str初始化爲空字符串並將計數器重置爲0類的一個參數構造函數將字符串s傳遞給str並將計數器重置爲零。該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從類,但問題是上述^^這是爲什麼只outputing它是平衡

//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

有在該位的代碼邏輯問題:

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結束時括號是平衡的。

+0

應我的循環是什麼 –

相關問題