初学java,终于肯动手自己写程序了,原来写起来才知道自己基础很不好啊,这是今晚的学习笔记:
1。多路选择语句switch只能用int 类型;
2。”a“表示了一个字符串,‘a’表示了一个字符;
以下是今晚自己的代码:
这个是学习字符串时写的一个将输入的字符转换成暗语:
import java.util.*;
public final class PigLatin
{
private PigLatin(){}
public static String translate(String plain){
StringBuffer result = new StringBuffer("");
StringTokenizer words = new StringTokenizer (plain);
while(words.hasMoreTokens()){
result.append(translateWord(words.nextToken()) + " ");
}
return result.toString();
} //translate()
private static String translateWord(String word){
int num = findFirstVowel(word);
StringBuffer result = new StringBuffer("");
if (num == 0)
return word + "yay";
else{
result.append(word.substring(num) + word.substring(0,num-1) + "ay");
return result.toString();
}
} //transtlateWord()
private static int findFirstVowel(String word){
char vowel;
for(int k=0; k < word.length(); k++){
vowel = word.charAt(k);
if (vowel == 'a' || vowel == 'e' || vowel == 'i' || vowel == 'o' || vowel == 'u')
return k;
}
return 0;
} //findFirstVowel()
} //PigLatin
|
以下是Applet:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
public class PigLatinApplet extends Applet implements ActionListener
{
private Label prompt = new Label("Input a word and : ");
private TextField input = new TextField(30);
private TextArea display = new TextArea(10,40);
public void init(){
add(prompt);
add(input);
input.addActionListener(this);
add(display);
display.setEditable(false);
} // init()
public void actionPerformed(ActionEvent e){
String plaintext = input.getText();
display.append(plaintext + "\n" +
PigLatin.translate(plaintext) + "\n");
} //actionPerformed()
}
|
阅读(1803) | 评论(1) | 转发(0) |