Java 如何编写平衡符号的程序?
我正在为硬件编写一个程序。我们应该平衡这些符号Java 如何编写平衡符号的程序?,java,Java,我正在为硬件编写一个程序。我们应该平衡这些符号 l{}、()、[]、“”、和/**/“忽略字符串文本和注释块,但我不知道如何做到这一点。我的代码部分正常工作,但当涉及到{}时,它无法判断。它在处理/**/时也有问题。我被困住了,不知道该走哪条路 例如,假设: public class Test { public static final void main(String[ ) args) { System.out.println("Hello."); }
l{}、()、[]、“”、和/**/“忽略字符串文本和注释块,但我不知道如何做到这一点。我的代码部分正常工作,但当涉及到{}
时,它无法判断。它在处理/**/
时也有问题。我被困住了,不知道该走哪条路
例如,假设:
public class Test {
public static final void main(String[ ) args) {
System.out.println("Hello.");
}
}
它打印两个不匹配的}
,因为{
不是紧跟在}
之前。我们需要将符号推到堆栈上,并弹出它们进行比较。我们还需要编写自己的堆栈方法MyStack.java
我在这里提供了主要信息:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class SymbolBalance {
public static void main(String[] args){
if(args.length>0){
try{
Scanner input = new Scanner(new File (args[0]));
MyStack<Character> ms = new MyStack<>();
String str;
char ch;
while(input.hasNext()){
str = input.next();
for(int i=0;i<str.length();i++){
ch = str.charAt(i);
if(ch == '{'||ch =='(' || ch=='[' ||ch=='"'||
(ch == '/'&&i<str.length() -1&&str.charAt(i+1)=='*'))
{
ms.push(ch);
}
else if (ch==')'){
if(ms.isEmpty()||ms.pop()!= '('){
System.out.println(") is mismatched");
}
}
else if(ch == ']'){
if(ms.isEmpty() || ms.pop() != '['){
System.out.println("] is mismatched");
}
}
else if(ch == '}'){
if(ms.isEmpty() || ms.pop() != '{'){
System.out.println("} is mismatched");
}
}
else if(ch == '*' && i<str.length()-1&&str.charAt(i+1) == '/'){
if(ms.isEmpty() || ms.pop() != '*'){
System.out.println("*/ is mismatched");
}
}
else if(ch == '"'){
if(ms.isEmpty() || ms.pop() != '"'){
System.out.println(" \"\" is mismateched");
}
}
}
}
input.close();
}
catch(FileNotFoundException e){
System.out.println("Cannot find file");
e.printStackTrace();
}
}
else{
System.out.println("No command line argument");
}
}
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
公共类符号平衡{
公共静态void main(字符串[]args){
如果(参数长度>0){
试一试{
扫描仪输入=新扫描仪(新文件(args[0]);
MyStack ms=新MyStack();
字符串str;
char ch;
while(input.hasNext()){
str=input.next();
对于(int i=0;i你的程序是99%正确的。唯一存在逻辑谬误的部分是双引号的特例
双引号是一种特殊符号,表示引号的开始和结束是同一符号。列表中的其他符号具有不同的开始和结束符号。例如,括号。开始括号由“[”表示,结束括号由“]”表示。但是对于双引号,“
可以表示报价的开始或结束
由于双引号是一种特殊的符号,您需要以特殊的方式处理它。当您将双引号符号推送或弹出到MyStack
堆栈时,您需要跟踪它是开始还是结束。跟踪它的方法可能有很多。我建议使用一个布尔标志指示相同的情况。您需要翻转每次向堆栈中推送或弹出双引号时,都要使用e标志
以下是我的修订版程序的工作实例:
public static void main(String[] args){
if(args.length>0){
try{
Scanner input = new Scanner(new File (args[0]));
MyStack<Character> ms = new MyStack<>();
String str;
char ch;
boolean quoteStart = true; //Flag to indicate quote start or end
while(input.hasNext()){
str = input.next();
for(int i=0;i<str.length();i++){
ch = str.charAt(i);
//Remove the " from the following condition. Handle it later.
if(ch == '{'||ch =='(' || ch=='[' ||
(ch == '/'&&i<str.length() -1&&str.charAt(i+1)=='*'))
{
ms.push(ch);
}
else if (ch==')'){
if(ms.isEmpty()||ms.pop()!= '('){
System.out.println(") is mismatched");
}
}
else if(ch == ']'){
if(ms.isEmpty() || ms.pop() != '['){
System.out.println("] is mismatched");
}
}
else if(ch == '}'){
if(ms.isEmpty() || ms.pop() != '{'){
System.out.println("} is mismatched");
}
}
else if(ch == '*' && i<str.length()-1&&str.charAt(i+1) == '/'){
if(ms.isEmpty() || ms.pop() != '*'){
System.out.println("*/ is mismatched");
}
}
//Handle the quote here
else if(ch == '"'){
if(quoteStart == true) {
ms.push(ch);
quoteStart = false;
}
else {
if(ms.isEmpty() || ms.pop() != '"'){
System.out.println(" \"\" is mismateched");
}
quoteStart = true;
}
}
}
}
input.close();
}
catch(FileNotFoundException e){
System.out.println("Cannot find file");
e.printStackTrace();
}
}
else{
System.out.println("No command line argument");
}
}
publicstaticvoidmain(字符串[]args){
如果(参数长度>0){
试一试{
扫描仪输入=新扫描仪(新文件(args[0]);
MyStack ms=新MyStack();
字符串str;
char ch;
布尔quoteStart=true;//指示报价开始或结束的标志
while(input.hasNext()){
str=input.next();
对于(int i=0;我将来到StackOverflow。请花一些时间访问并阅读。问题形式为“这是我的代码,请调试它”被视为离题。StackOverflow不是讨论、教程或调试站点。其工作方式是希望您尝试解决问题,然后在遇到困难时寻求帮助,清楚地解释您尝试过的内容和不理解的内容。至少您应该已经在IDE调试器,能够识别与预期不符的结果。是否尝试调试代码?能否说明程序无法正确处理“{”的位置或原因?请参阅并突出显示语法修复代码