二级Java干货整理(一)
基本操作题
话不多说上干货
1、import javax.swing.JOptionPane;
public class Java_1 {
public static void main( String args[] ){
//变量初始化
int passes = 0, //考生通过的数目
failures = 0, //考生不通过的数目
student = 1, //学生计数器
result; //一门考生结果
String input, //用户输入的值
output; //输出字符串
//处理10名学生,用计数器控制循环
while ( student <= 10 ) {
input = JOptionPane.showInputDialog(
"输入结果(1=通过,2=不通过)" );
//*********Found**********
result = Integer.parseInt( input );
if ( result == 1 )
passes = passes + 1;
else
failures = failures + 1;
student = student + 1;
}
//结果处理
output = "通过: " + passes +
"\n不通过: " + failures;
if( passes > 8 )
output = output + "\n提高学费";
//*********Found**********
JOptionPane.showMessageDialog( null, output,
"对考试结果的分析示例",
JOptionPane.INFORMATION_MESSAGE );
//*********Found**********
System.exit 0 );
}
}
考后分析:
1、interger.parseInt();//类型转换
2、JOptionPane.showMessageDialog();弹窗问题
3、第一种showMessageDialog(Component parentComponent, Object message)
4、第二种showMessageDialog(Component parentComponent, Object message, String title, int messageType)
system.exit(0):正常退出,程序正常执行结束退出
system.exit(1):是非正常退出,就是说无论程序正在执行与否,都退出,
System.exit(status)不管status为何值都会退出程序。和return 相比有以下不同点:return是回到上一层,而System.exit(status)是回到最上层
N0.2
public class Java_1 extends TT
{
//*********Found**********
public static void main(String args[])
{
Java_1 t = new Java_1("小龙");
}
public Java_1(String s)
{
super(s);
System.out.println("您好吗?");
}
public Java_1()
{
this("我是文朋");
}
}
class TT
{
public TT()
{
System.out.println("多高兴啊!");
}
public TT(String s)
{
//*********Found**********
this();
System.out.println("我是"+s);
}
}
No.3
public class Java_1
{
//*********Found**********
public static void main (String args[])
{
new SimpleThread("第1").start();
new SimpleThread("第2").start();
}
}
//*********Found**********
class SimpleThread extends Thread
//创建线程有两种方式,这题是继承Thread类,来实现Runnable接口
{
public SimpleThread(String str)
{
super(str);
}
public void run()
{
for (int i = 0; i < 5; i++)
{
//*********Found**********
System.out.println(i + " " + getName());
try
{
sleep((int)(2 * 100));
}
catch (InterruptedException e) { }
}
System.out.println("运行! " + getName());
}
}
做后小结:这一题主要是考的有关线程的创建,我上网查了一下线程的创建主要有四种方式
1、通过实现Runnable接口来创建线程
package com.mystudy.test;
public class TestRunnable implements Runnable {
@Override
public void run() {
System.out.println("线程中需要执行的代码块···");
}
public static void main(String[] args) {
Thread thread = new Thread(new TestRunnable());
thread.start();
}
}
2、通过继承Thread类来创建线程
package com.mystudy.test;
public class TestThread extends Thread {
@Override
public void run() {
System.out.println("线程中需要执行的代码块···");
}
public static void main(String[] args) {
Thread th = new TestThread();
th.start();
}
}
3、通过实现callable接口来创建线程
package com.mystudy.test;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class TestCallable implements Callable {
public Object call() throws Exception {
System.out.println("线程中需要执行的代码块···");
return "success";
}
public static void main(String[] args) {
Callable call = new TestCallable();
FutureTask future = new FutureTask(call);
future.run();
}
}
4、使用Executor窗口来创建线程池
package com.mystudy.test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class TestThreadPoolExecutor {
public static void main(String[] args) {
//创建使用单个线程的线程池
ExecutorService es1 = Executors.newSingleThreadExecutor();
for (int i = 0; i < 10; i++) {
es1.submit(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "正在执行任务");
}
});
}
//创建使用固定线程数的线程池
ExecutorService es2 = Executors.newFixedThreadPool(3);
for (int i = 0; i < 10; i++) {
es2.submit(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "正在执行任务");
}
});
}
//创建一个会根据需要创建新线程的线程池
ExecutorService es3 = Executors.newCachedThreadPool();
for (int i = 0; i < 20; i++) {
es3.submit(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "正在执行任务");
}
});
}
//创建拥有固定线程数量的定时线程任务的线程池
ScheduledExecutorService es4 = Executors.newScheduledThreadPool(2);
System.out.println("时间:" + System.currentTimeMillis());
for (int i = 0; i < 5; i++) {
es4.schedule(new Runnable() {
@Override
public void run() {
System.out.println("时间:" + System.currentTimeMillis() + "--" + Thread.currentThread().getName() + "正在执行任务");
}
}, 3, TimeUnit.SECONDS);
}
//创建只有一个线程的定时线程任务的线程池
ScheduledExecutorService es5 = Executors.newSingleThreadScheduledExecutor();
System.out.println("时间:" + System.currentTimeMillis());
for (int i = 0; i < 5; i++) {
es5.schedule(new Runnable() {
@Override
public void run() {
System.out.println("时间:" + System.currentTimeMillis() + "--" + Thread.currentThread().getName() + "正在执行任务");
}
}, 3, TimeUnit.SECONDS);
}
}
}
5、通过匿名内部类创建多线程
代码如下
package com.mystudy.test;
/**
* Author:dk
* Date:2020/7/27 16:20
* Description:
*/
public class ThreadTest {
public static void main(String[] args) {
/*
* 第一种方式:
* 1.继承Thread类
* 2.重写run方法
* 3.将要执行的代码写在run方法中
*/
new Thread() {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("aaaaaaaaa");
}
}
}.start();//开启线程
/*
* 第二种方式:
* 1.将Runnable的子类对象传递给Thread的构造方法
* 2.重写run方法
* 3.将执行的代码写在run方法中,最后我们开启线程
*/
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("bbbbbbbbb");
}
}
}).start();//开启线程
}
}
NO.4
//*********Found**********
import javax.swing.*;
public class Java_1
{
//*********Found**********
public static void main(String[] args)
{
System.out.println();
System.out.println("这是一个指定球半径,求球体积的程序。");
String input=JOptionPane.showInputDialog("请输入球半径。");
//*********Found**********
double r=Double.parseDouble(input);
System.out.println("当球的半径是" + r + "时,该球的体积是 " + (Math.PI*r*r*r*4/3));
System.exit(0);
}
}
做后小结
Swing是java的扩展程序包之一,javax中存储的都是扩展程序包。
No5
//阅读下列代码:
public class Java_1
{
//*********Found**********
public static void main(String []args)
{
String s1=new String("你正在考试");
String s2=new String("你正在考试");
System.out.println(s1==s2);
//*********Found**********
System.out.println(s1.equals(s2));
}
}
做后小结:
字符串比较大小使用的是equals()而不是==
No6
public class Java_1 {
//*********Found**********
public static void main(String []args)
{
char ch='d';
//*********Found**********
switch(ch)
{
case 'a': System.out.print("a");break;
case 'b': System.out.print("b");
case 'c': System.out.print("c");break;
//*********Found**********
case 'd': System.out.print("abc");
}
}
}
//做后小结:
主要考察switch语句
详情请查看链接:
https://blog.csdn.net/qq_39220043/article/details/82225024?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161630617516780261916819%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=161630617516780261916819&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduend~default-1-82225024.first_rank_v2_pc_rank_v29&utm_term=Java%E4%B8%ADswitch
No7
public class Java_1
{
//*********Found**********
public static void main(String args[])
{
byte b = 8;
//*********Found**********
long g = 567L;
float f = 3.1415f;
double d = 2.789;
int ii = 207;
//*********Found**********
long gg = g+ii;
float ff = b*f;
double dd = ff/ii+d;
//*********Found**********
System.out.print("ii= "+ii+" ");
System.out.println("gg= "+gg);
System.out.println("ff= "+ff);
System.out.println("dd= "+dd);
}
}
做后小结:做题时就像在考试一样对待,这样考试的时候才会取得成功!!!
看清代码与题目,这样避免不必要的失分!
No8
import java.applet.*;
import java.awt.Graphics;
//*********Found********
public class Java_1 extends Applet {
public void paint( Graphics g )
{
//*********Found********
g.drawString( "欢迎你来参加Java 语言考试!", 25, 25 );
}
}
考后小结:
在Applet中呢,在Java中要实现applet需要继承applet类。
在applet中drawString()是将需要显示的内容在给定坐标显示出来。
G.drawString(“语句”,坐标);
No.9
public class Java_1
{
public static void main(String[] args)
{
long sum;
//*********Found**********
sum=0;//变量初始化
for(int i=1;i<8;i+=2){
long b=1;
//*********Found**********
for(int j=1; j<=i; j++)
//*********Found**********
b=j*b;
System.out.println( i + "!= " + b);
sum+=b;
}
System.out.println("sum=" + sum);
}
}
No.10
public class Java_1 {
public static void main(String args[]) {
int x,n;
//*********Found********
n=0;
for( x = 100 ; x <= 200 ; x++)
if ( x % 9 == 0 ) {
//*********Found********
System.out.print(" " + x);
n++;
//*********Found********
if ( n>5 ) System.out.println( );//n%5==0
}
}
}
//*********Found********
import java.swing.*;//applet
import java.awt.Graphics;
//*********Found********
public class Java_1 extends Applet {
public void paint( Graphics g )
{
//*********Found********
g.paint( "欢迎你来参加Java 语言考试!", 25, 25 );//drawString
}
}