gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:26:44 24 4
gpt4 key购买 nike

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

}

最佳答案

您在 BalancedString 类定义中犯了一些错误。首先,str 字段不应该是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();
}

关于java - 为什么我的代码只输出字符串是平衡的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13484810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com