java中怎样从一段词法里找出并打印关键字, *** 作符,变量名,方法名啊??

java中怎样从一段词法里找出并打印关键字, *** 作符,变量名,方法名啊??,第1张

/
词法分析

/
import javaioFile;
import javaioFileReader;
public class Compiler {
private static String string;
private static String str;
private static char ch;
/
读取文件
/
public static void getChar() throws Exception{
File f= new File("C:\\","testtxt");
if(!fexists()){
Systemoutprintln("文件不存在,请输入正确的文件路径");
}
FileReader fr = new FileReader(f);
int rs = 0;
char []data = new char[256];
Systemoutprint("");

while((rs = frread(data)) > 0){
string = new String(data,0,rs)trim();
}
}
/
判断读入的字符是否为字母
/
public static boolean isLetter(char c){
if((ch >= 'a' && ch <= 'z') || (ch >+ 'A' && ch <= 'Z')){
return true;
}
else
return false;
}
/
判断读入的字符是否为数字
/
public static boolean isDigit(char c){
if(ch >='0' && ch <= '9'){
return true;
}
else
return false;
}
/
判断是否为关键字
/
public static boolean isKey(String string) {
if(stringequals("void") || stringequals("if")|| string equals("for")|| stringequals("while")
|| stringequals("do")|| stringequals("return")|| stringequals("break")
|| stringequals("main"))
{
return true;
}
else return false;
}
/
判断输入的字符并输出单词符号
/
public static void judgement() throws Exception {
CompilergetChar();
int m = 0;
string +=' ';
for(int i = 0;i < stringlength();i++){
switch (m)
{
case 0:
ch = stringcharAt(i);
if(ch == '+' || ch == '-' || ch == '' || ch == '/' || ch == '='
|| ch == '>' || ch == '<')
{
m = 4;
}
else if(ch == ',' || ch == ';' || ch == '{' || ch == '}' || ch == '(' || ch == ')')
{
m = 5;
}
else if ( isDigit((ch =stringcharAt(i)) ) )
{
str = "";
str += ch;
m = 3;
}
else if ( isLetter(ch =stringcharAt(i)) )
{
str = "";
str += ch;
m = 2;
}else {}
break;

case 4:
i--;
Systemoutprintln(("( 4 " + "“ " + ch + " ” )"));
m = 0;
break;

case 5:
i --;
Systemoutprintln(("( 5 " + "“ " + ch + " ” )"));
m = 0;
break;
case 2:
if (isLetter(ch = stringcharAt(i)))
{
str += ch;
}
else
{
if ( isKey(str) )
{
Systemoutprintln("( 1 " + "“ " + str + " ” )");
}else {
Systemoutprintln(("( 2 " + "“ " + str + " ” )"));
}
i--;
m = 0;
}
break;

case 3:
if (isDigit((ch =stringcharAt(i)) ) )
{
str += ch;
}
else
{
Systemoutprintln(("( 3 " + "“ " + str + " ” )"));
i --;
m = 0;
}
break;
}
}
}

public static void main(String[] args) throws Exception{
Compilerjudgement();
// Systemoutprint(b)

}
}

你说只用while 循环实现对不?
这个绝对正确!
char i='a'; int num=1; int b=1;
while(b<=5){
while(num<=b){ Systemoutprint(i);
num++;
} Systemoutprint("\n");
b++;
i++;
num=1;
}

"a["输出 a[ 然后 “]="输出 ]= ; +号就是把他们连接起来; 0本来是int型的,但是有了+号他就和其他string合成一个string;所以最终输出 a[0]=

把1234更改为其他字符串也能达到要求的效果,主要是利用数组下标越界的异常来做。
不明白可以追问
public class Test {
public static void main(String[] args) {
String src = new String("1234");
for(int i = srclength() ;i>0;i--){
try{
Systemoutprintln("-------");
int j = 0 ;
int z = i;
while(true){
Systemoutprint(srcsubstring(j++,z++)+" ");
}
}catch(Exception e){
Systemoutprintln("");
continue;
}
}
}
}

import javautilScanner;
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(Systemin);
Systemoutprintln("请输入字符串");
String str = scannextLine();
Systemoutprintln(str);
}
}

有两个方法可以做,1,用stringindexof方法,for循环,先打印1,4,,7,用索引就可以找到(i+2),然后打印回车,接着重复打印2,5,8,然后再打印3,,6,9方法2,可以先放到字符数组中,用循环打印出来。

要看起来一个字一个字的输出就需要停顿时间,这样才能看得到这种效果,停顿的话又需要新建线程

代码如下:

public class AutomaticTyping {
public static void main(String[] args) {
ATy aty = new ATy();// 创建一个对象
Thread t = new Thread(aty);// 创建一个线程
tstart();// 线程开始
}
}
class ATy implements Runnable {
String text = "秋名山上行人稀,常有车手较高低。\n如今车道依旧在,不见当年老司机。";
public void run() {
for (int i = 0; i < textlength(); i++) {
Systemoutprint(textcharAt(i));
try {
Threadsleep(200);//停顿02秒
} catch (InterruptedException e) {
eprintStackTrace();
}
}
}
}

方法不唯一。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/13346630.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-08-31
下一篇2025-08-31

发表评论

登录后才能评论

评论列表(0条)

    保存