史萊姆論壇

史萊姆論壇 (http://forum.slime.com.tw/)
-   程式語言討論區 (http://forum.slime.com.tw/f76.html)
-   -   回文 (java 程式) (http://forum.slime.com.tw/thread168529.html)

psac 2006-02-18 05:50 AM

回文 (java 程式)
 
回文

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
public class HuiWen extends Applet implements ActionListener
{
Label label1,label2;
Button button1;
TextField tf;
public void init()
{
Panel panel1=new Panel();
label1=new Label("請輸入一個整數");
label2=new Label(" ");
label2.setBackground(Color.red);
button1=new Button("判斷");
tf=new TextField();
button1.addActionListener(this);
panel1.add(label1);
panel1.add(tf);
panel1.add(button1);
panel1.add(label2);
add(panel1);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
char ch;
Stack stack=new Stack();
String str1=tf.getText();
String str2="";
for(int i=0;i<str1.length();i++)
{
ch=str1.charAt(i);
stack.push(ch);
}
while(!stack.isEmpty())//while和for實現的功能是一樣的
{
Object cch=stack.pop();//Vector裡面的E是指Object
// str3=Character.toString(cch);
str2=str2+cch;
}
int compare1=str2.compareTo(str1);
if(compare1==0)
label2.setText("是回文!");
else
label2.setText("不是回文!");
}
}
}


所有時間均為台北時間。現在的時間是 09:35 AM

Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2025, Jelsoft Enterprises Ltd.

『服務條款』

* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *


SEO by vBSEO 3.6.1