Java中Applet小程序的编写

Java中Applet小程序的编写,第1张

import javaawt;

import javaapplet;

import javaawtevent;

public class L07_D extends Applet implements ActionListener{

public void init() {

Button one;

Button two;

one = new Button("Button one");

two = new Button("Button two");

add(one);add(two);

oneaddActionListener(this);

twoaddActionListener(this);

}

public void actionPerformed(ActionEvent e) {

showStatus("One of my buttons was clicked");

}

}

3:编写一个方法统计一个字符在某字符串中的出现次数; public class Test03{

public int getCounts(String s,char c){

char[] cc = stoCharArray();

int size = cclength;

int counts = 0;

for(int i=0;i<size;i++){

if(cc[i]==c)counts++;

}

return counts;

}

public static void main(String[] args){

Test03 t = new Test03();

String s = "hello! java I love you";

char c = 'o';

Systemoutprintln("字符串s: "+s);

Systemoutprintln("字符c: "+c);

Systemoutprintln("c在s中出现的个数: "+tgetCounts(s,c));

}

}

可以使用数组,来存储输入的3个数字,然后排序,得到最大值

也可以直接一边输出一边判断,参考代码如下

import javautilScanner;

public class Exam {

public static void main(String[] args) {

int len =3;

Systemoutprintln("请输入"+len+"个数字");

Scanner sc = new Scanner(Systemin);

int max = scnextInt();//假设第一次输入的数字是最大,存在这里

for (int i = 0; i <len-1; i++) {

int x = scnextInt();

if(x>max){//如果比max还要大那么就替换掉max的值

max = x;

}

}

Systemoutprintln("最大的数字是"+max);

}

}

测试

请输入3个数字

2

1

6

最大的数字是6

import javautilCollections;

import javautilHashMap;

import javautilLinkedList;

import javautilList;

import javautilMap;

public class Test {

    public static void main(String[] args) {

        final String[] str = { "a-b 1:1", "a-c 2:1", "b-c 0:1" };

        

        final Map<String, Integer> map = new HashMap<String, Integer>();

        for (String s : str) {

            final String[] temp = ssplit(" ");

            final String[] names = temp[0]split("-");

            final String[] counts = temp[1]split(":");

            for (int i = 0; i < 2; i++) {

                String name = names[i];

                int count = IntegerparseInt(counts[i]);

                if (mapcontainsKey(name)) {

                    mapput(name, mapget(name) + count);

                } else {

                    mapput(name, count);

                }

            }

        }

        

        final List<Team> teams = new LinkedList<Team>();

        for (String name : mapkeySet()) {

            teamsadd(new Team(name, mapget(name)));

        }

        Collectionssort(teams);

        Systemoutprintln("队伍 : 分");

        for (Team t : teams) {

            Systemoutprintln(tgetName() + "   : " + tgetCount());

        }

    }

}

class Team implements Comparable<Team> {

    private String name;

    private int count;

    public Team(String name, int count) {

        thisname = name;

        thiscount = count;

    }

    public void addCount(int count) {

        thiscount = thiscount + count;

    }

    public String getName() {

        return name;

    }

    public int getCount() {

        return count;

    }

    public int compareTo(Team o) {

        return ogetCount() - thiscount;

    }

}

class Ball {

public void play() {

Systemoutprintln("玩球儿");

}

}

class Football extends Ball {

public void play() {

Systemoutprintln("使用足球运动");

}

}

class Basketball extends Ball {

public void play() {

Systemoutprintln("使用篮球运动");

}

}

public class TestMain {

public static void main(String[] args) {

TestMain tm = new TestMain();

tmtestPlay();

}

public void testPlay() {

Ball ball = new Football();

ballplay();

ball = new Basketball();

ballplay();

}

}

/

D:\>javac TestMainjava

D:\>java TestMain

使用足球运动

使用篮球运动

/

已完成,复制粘贴即可。

import javautilScanner;

public class YuGiOh

{

private static void checkPass ( String password )

{

String regp = "[A-Z]";

String rego = "[a-z]";

String regq = "\\d";

String regs = "[\\`\\-\\!\\@\\#\\$\\%\\^\\&\\\\(\\)\\_\\+\\=\\[\\{\\}\\]\\;\\:\\,\\\\<\\>\\/\\]";

String regex = "^[a-zA-Z\\d\\`\\-\\!\\@\\#\\$\\%\\^\\&\\\\(\\)\\_\\+\\=\\[\\{\\}\\]\\;\\:\\,\\\\<\\>\\/\\]{6,}$";

String result = "";

if (passwordlength () < 6)

{

result += "\t\t-contain at least six characters Your password is only " + passwordlength () + " characters long\n";

}

if (!passwordmatches (regp))

{

result += "\t\t-contain at least one uppercase letter\n";

}

if (!passwordmatches (rego))

{

result += "\t\t-contain at least one lowercase letter\n";

}

if (!passwordmatches (regq))

{

result += "\t\t-contain at least one number\n";

}

if (!passwordmatches (regs))

{

result += "\t\t-contain at least one of the following special characters: `!@#$%^&()_+=[{}];:,<>/\n";

}

else if (!passwordmatches (regex))

{

result += "\t\t-not contain an invalid character The valid special characters: `!@#$%^&()_+=[{}];:,<>/\n";

}

if (!""equals (result))

{

Systemoutprintln ("Your password must:\n" + result);

}

else

{

Systemoutprintln ("Your valid password is: " + password);

}

Systemoutprintln ();

}

public static void main ( String[] args )

{

Scanner scanner = new Scanner (Systemin);

while (true)

{

Systemoutprint ("Enter a valid password: ");

String password = scannernextLine ();

checkPass (password);

}

}

}

class Test{

public static void main(String [] arg){

Test test = new Test();

testOutPut(50 , 9); /第一个参数50为你要输出的最大数,你改成30就是输出1-30的数,50则为输出1-50/

/第二个参数9,就是控制你换行的值,你改成10就是每输出10个字就换行一次/

} public void OutPut(int length , int size){

int temp = 1;

for(int i = 1 ; i <= length ; i ++ ,temp ++){

Systemoutprint(i+"\t");

if(temp % size == 0){

Systemoutprintln();

}

}

}

} 将上面的代码保存成为Testjava文件(其实后缀为java的就行了),编译就能运行了。。

以上就是关于Java中Applet小程序的编写全部的内容,包括:Java中Applet小程序的编写、这几个小程序用完整的JAVA语言如何编写、编写一个JAVA小程序,从键盘输入3个数,求这三个数的最大值。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址: http://outofmemory.cn/zz/9459292.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存