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;
}
}
내가 도움이 필요 이러한 변경을 어떻게 선생님은 나에게 아무것도 –
을 말하지 않는다 질문은 매우 오해의 소지가 있습니다. 'BalancedString'은 실제로 사용되지 않습니다 (따라서 문제가 아닙니다). – Vulcan
@ Vulcan - 'BalancedString'은 지금 무시할 수 있다고 생각하지만, OP가 결국 그것을 사용한다고 가정합니다. 마지막 단락에서는 문제의 핵심을 다뤘습니다. –