gpt4 book ai didi

java - 如何查询报关单?

转载 作者:行者123 更新时间:2023-12-01 11:24:55 24 4
gpt4 key购买 nike

我正在解决一个问题,我有一个字符串列表,它只不过是一个java代码。因此,如果该代码在方法体之前或之后有任何声明,那么该脚本应该给我一个错误。如果变量声明位于方法体内,那么它应该可以正常工作。
例如:

int abcs; // This should generate error.
double cad; //This should generate error.

Void main(){
int test; //This should not generate error.

void test1(){
int test3; //This should not generate error.
}
}

我为此编写了一个小程序,但无法获得所需的结果,我的代码是:

private void check( String line)

{ 整数计数 = 0; int 标志 = 0;

    // is there an open brace on the current line
int bracketpos = line.indexOf('{');
// is there a close brace on the current line
int closebracketpos = line.indexOf('}');
// is there a semicolon on the current line
int charpos = line.indexOf(';');

// increment count if there is an open brace
if(bracketpos>=0 )
++count;

// decrement count if there is a close brace
if(closebracketpos>=0 )
--count;

// if there is an open brace on the current line
// ...and there's a close brace on the current line
// ...and there's a semicolon on the current line
// ...and the number of brackets are balanced (count==0)
if(bracketpos>=0 && closebracketpos>=0 && charpos>=0 && count==0){
// if the first semicolon appears after the first close brace
if(charpos>closebracketpos){
flag =2;
}
else
// the close brace appears after the semicolon, or neither exist
flag =0;
}

else if (charpos >= 0 && count ==0) {

flag= 1;
}
else {
flag = 0;
// the line does not have a ;,{,}
}

if(flag!=0)
{
//ERROR
}
}

我正在逐行传递示例,因此 count 正在记录“{”和“}”。

问题:我将示例逐行输入到 check() 中。我想检查一下示例中是否有任何声明。但是如果方法体内有任何声明那就没问题,但是如果方法体外有声明那么我的 check() 应该提示/或者给我一个错误。

最佳答案

你的逻辑是错误的。您应该添加注释来描述代码在每个阶段的作用。这是让您开始的...

private void check( String line)
{ int count = 0;
int flag = 0;

// is there an open brace on the current line
int bracketpos = line.indexOf('{');
// is there a close brace on the current line
int closebracketpos = line.indexOf('}');
// is there a semicolon on the current line
int charpos = line.indexOf(';');

// increment count if there is an open brace
if(bracketpos>=0 )
++count;

// decrement count if there is a close brace
if(closebracketpos>=0 )
--count;

// if there is an open brace on the current line
// ...and there's a close brace on the current line
// ...and there's a semicolon on the current line
// ...and the number of brackets are balanced (count==0)
if(bracketpos>=0 && closebracketpos>=0 && charpos>=0 && count==0){
// if the first semicolon appears after the first close brace
if(charpos>closebracketpos){
flag =2;
}
else
// the close brace appears after the semicolon, or neither exist
flag =0;
}

else if (charpos >= 0 && count ==0) {

flag= 1;
}
else {
flag = 0;
// the line does not have a ;,{,}
}

if(flag!=0)
{
//ERROR
}
}

关于java - 如何查询报关单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30897012/

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