Skip to content

Commit be7262e

Browse files
committed
练习2
1 parent d2f76a1 commit be7262e

25 files changed

+2357
-3
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 85 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 Bytes
Binary file not shown.

src/com/practise/practise1.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.practise;
2+
3+
import java.util.Arrays;
4+
import java.util.Scanner;
5+
/*
6+
* 编写Java程序,从键盘上输入5个数,赋值给一个长度为5的数组,利用冒泡排序法为数组排序。
7+
* */
8+
public class practise1 {
9+
public static void main(String[] args){
10+
int[] a= new int[5];
11+
Scanner sc = new Scanner(System.in);
12+
System.out.println("请输入5个数");
13+
for(int i=0;i<a.length;i++){
14+
System.out.println("请输入第"+(i+1)+"个数");
15+
a[i] = sc.nextInt();
16+
}
17+
for(int i=0;i<a.length-1;i++) {
18+
for(int j=0;j<a.length-1-i;j++) {
19+
if(a[j]>a[j+1]) {
20+
int temp=a[j];
21+
a[j]=a[j+1];
22+
a[j+1]=temp;
23+
}
24+
}
25+
}
26+
System.out.println(Arrays.toString(a));
27+
}
28+
}

src/com/practise/practise2.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.practise;
2+
3+
import org.omg.CORBA.INTERNAL;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
8+
/*
9+
*判断101-200之间有多少个素数,并输出所有素数。
10+
* */
11+
public class practise2 {
12+
public static void main(String[] args) {
13+
int arrIndex = 0;
14+
// int[] a = new int[];
15+
// 使用 ArrayList 代替数组
16+
ArrayList<Integer> a=new ArrayList();
17+
for (int i =101;i<=200;i++){
18+
if(isPrime(i)){
19+
// a[arrIndex] = i;
20+
a.add(i);
21+
arrIndex++;
22+
}
23+
}
24+
System.out.println(a);
25+
}
26+
public static boolean isPrime(int num) {
27+
boolean isPrime = true;
28+
int len = num/2;
29+
for(int i=2;i<len;i++) {
30+
if(num%i==0) {
31+
isPrime=false;
32+
break;
33+
}
34+
}
35+
if(num==2) {
36+
return true;
37+
}
38+
return isPrime;
39+
}
40+
}

src/com/volume1/LambdaTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.volume1;
2+
3+
import java.util.*;
4+
5+
import javax.swing.*;
6+
import javax.swing.Timer;
7+
8+
/**
9+
* This program demonstrates the use of lambda expressions.
10+
* @version 1.0 2015-05-12
11+
* @author Cay Horstmann
12+
*/
13+
/*
14+
* 如何在一个比较器和一个动作监听器中使用 lambda 表达式
15+
* */
16+
public class LambdaTest
17+
{
18+
public static void main(String[] args)
19+
{
20+
String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
21+
"Jupiter", "Saturn", "Uranus", "Neptune" };
22+
System.out.println(Arrays.toString(planets));
23+
System.out.println("Sorted in dictionary order:");
24+
Arrays.sort(planets);
25+
System.out.println(Arrays.toString(planets));
26+
System.out.println("Sorted by length:");
27+
Arrays.sort(planets, (first, second) -> first.length() - second.length());
28+
System.out.println(Arrays.toString(planets));
29+
30+
Timer t = new Timer(1000, event ->
31+
System.out.println("The time is " + new Date()));
32+
t.start();
33+
34+
// keep program running until user selects "Ok"
35+
JOptionPane.showMessageDialog(null, "Quit program?");
36+
System.exit(0);
37+
}
38+
}

src/com/volume1/TimerTest.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.volume1;
2+
3+
/**
4+
@version 1.01 2015-05-12
5+
@author Cay Horstmann
6+
*/
7+
8+
import java.awt.*;
9+
import java.awt.event.*;
10+
import java.util.*;
11+
import javax.swing.*;
12+
import javax.swing.Timer;
13+
// to resolve conflict with java.util.Timer
14+
/*
15+
* 程序启动后, 将会立即显示一个包含“ Quit program?”
16+
字样的对话框, 10 秒钟之后, 第 1 条定时器消息才会显示出来。
17+
*
18+
* 接口与回调 :
19+
* java.swing 包中有一个 Timer 类,可以使用它在到达给定的时间间隔时发出通告。 例
20+
如,假如程序中有一个时钟, 就可以请求每秒钟获得一个通告, 以便更新时钟的表盘。
21+
在构造定时器时,需要设置一个时间间隔, 并告之定时器,当到达时间间隔时需要做些
22+
什么操作
23+
如何告之定时器做什么呢? 在很多程序设计语言中,可以提供一个函数名, 定时器周期性地调用它。
24+
但是, 在 Java 标准类库中的类采用的是面向对象方法。它将某个类的对象传递给定时器,然后,定时器调用这个对象的方法。由于对象可以携带一些附加的信息,所以传递一个对象比传递一个函数要灵活得多。
25+
当然, 定时器需要知道调用哪一个方法,并要求传递的对象所属的类实现了java.awt.event 包的ActionListener 接口。下面是这个接口:
26+
public interface ActionListener
27+
{
28+
void actionPerfonned(ActionEvent event);
29+
}
30+
31+
当到达指定的时间间隔时,定时器就调用 actionPerformed 方法。
32+
假设希望每隔 10 秒钟打印一条信息“ At the tone, the time is . . .”, 然后响一声, 就应该
33+
定义一个实现 ActionListener 接口的类, 然后将需要执行的语句放在 actionPerformed 方法中。
34+
class TinePrinter implements ActionListener
35+
{
36+
public void actionPerformed(ActionEvent event)
37+
{
38+
System.out.println("At the tone, the time is " + new Date());
39+
Toolkit.getDefaultToolkit().beep();
40+
}
41+
}
42+
* */
43+
public class TimerTest
44+
{
45+
public static void main(String[] args)
46+
{
47+
ActionListener listener = new TimePrinter();
48+
49+
// construct a timer that calls the listener
50+
// once every 10 seconds
51+
Timer t = new Timer(10000, listener);
52+
t.start();
53+
54+
JOptionPane.showMessageDialog(null, "Quit program?");
55+
System.exit(0);
56+
}
57+
}
58+
/*当到达指定的时间间隔时,定时器就调用 actionPerformed 方法。
59+
假设希望每隔 10 秒钟打印一条信息“ At the tone, the time is . . .”, 然后响一声, 就应该
60+
定义一个实现 ActionListener 接口的类, 然后将需要执行的语句放在 actionPerformed 方法中。*/
61+
class TimePrinter implements ActionListener
62+
{
63+
public void actionPerformed(ActionEvent event)
64+
{
65+
System.out.println("At the tone, the time is " + new Date());
66+
Toolkit.getDefaultToolkit().beep();
67+
}
68+
}

src/com/volume1/helloJava.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package com.volume1;
22

3+
/*
4+
* 每一个 Java 应用程序都有一个带 String arg[],参数的 main 方法。这个参数表明 main 方法将接收一个字符串数组, 也就是命令行参数。
5+
*
6+
* 需要注意, 不需要使用对象调用静态方法。例如,不需要构造 Math 类对象就可以调用Math.pow。
7+
*同理, main 方法也是一个静态方法。
8+
* main 方法不对任何对象进行操作。事实上,在启动程序时还没有任何一个对象。静态的main 方法将执行并创建程序所需要的对象。
9+
* */
310
public class helloJava {
411
public static void main(String[] args){
512
System.out.println("hello java,I am comming back!");

0 commit comments

Comments
 (0)