1:希臘字母打印
public class GreekAlphabet {
public static void main(String[] args) {
// TODO Auto-generated method stub
char cStart='α',cEnd = 'ω';
while(cStart<=cEnd){
System.out.print(cStart+++",");
}
}
}
α,β,γ,δ,ε,ζ,η,θ,ι,κ,λ,μ,ν,ξ,ο,π,ρ,?,σ,τ,υ,φ,χ,ψ,ω,
2:猜數字遊戲
import java.util.Scanner;
import java.util.Random;
public class GuessNumber {
public static void main (String args[]) {
@SuppressWarnings("resource")
Scanner reader = new Scanner(System.in);
Random random = new Random();
System.out.println("給你一個1至100之間的整數,請猜測這個數");
int realNumber = random.nextInt(100)+1; //random.nextInt(100)是[0,100)中的隨機整數
int yourGuess = 0;
System.out.print("輸入您的猜測:");
yourGuess = reader.nextInt();
while(yourGuess!=realNumber) //循環條件
{
if(yourGuess>realNumber) //猜大了的條件代碼
{
System.out.print("猜大了,再輸入你的猜測:");
yourGuess = reader.nextInt();
}
else if(yourGuess<realNumber) //猜小了的條件代碼
{
System.out.print("猜小了,再輸入你的猜測:");
yourGuess = reader.nextInt();
}
}
System.out.println("猜對了!");
}
}
3:迴文數
import java.util.Scanner;
public class Number{
public static void main(String[] args){
int number=0, number2=0;
Scanner read=new Scanner(System.in);
System.out.println("Input a number:");
number = read.nextInt();
int d5,d4,d3,d2,d1;//萬千百十個位
if(number>0&&number<100000){
d5=number/10000%10;//萬位的數
d4=number/1000%10;//千位
d3=number/100%10;//百位
d2=number/10%10;//十位
d1=number%10;//個位
if(number<100000&&number>9999){
number2=d1*10000+d2*1000+d3*100+d4*10+d5;
}else if(number>999&&number<10000){
number2=d1*1000+d2*100+d3*10+d4;
}else if(number>99&&number>1000){
number2=d1*100+d2*10+d3;
}else if(number>9&&number<100){
number2=d1*10+d2;
}else
if(number<10&&number>0)number2=number;
if(number==number2)
{System.out.println(number+"是迴文數");}
else {System.out.println(number+"不是迴文數");
}
}else {System.out.println("輸入錯誤");
}}
}
4:IBM計算
package Lab6_1;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;
public class BMIWindow extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
JLabel weight,height,BMI;
JTextField wtext,htext,BMIt;
JButton comput;
public BMIWindow(){
setTitle("BMI計算器");
weight=new JLabel("體重(Kg)");
height=new JLabel("身高(m)");
BMI=new JLabel("身體質量指數(BMI)");
wtext=new JTextField(20);
htext=new JTextField(20);
BMIt=new JTextField(16);
BMIt.setEditable(false);
comput=new JButton("計算BMI");
setLayout(new FlowLayout());
add(weight);add(wtext);
add(height);add(htext);
add(BMI);add(BMIt);
add(comput);
//添加事件
comput.addActionListener(this);
setBounds(200, 200, 310, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
double wd=Double.valueOf(wtext.getText());
double hd=Double.valueOf(htext.getText());
double BMI=wd/hd/hd;
//String result=String.valueOf(BMI);
BMIt.setText(fit(BMI));
}
//判斷是否身體指數
public String fit(double bmi){
String b="";
if(bmi>35)b="非常嚴重超重";
else if(bmi>29)b="嚴重超重";
else if(bmi>24)b="超重";
else if(bmi>18)b="正常";
else if(bmi>16)b="偏輕";
else b="嚴重偏輕";
//保留兩位小數
String a=new DecimalFormat("#.00").format(bmi);
return a+" "+b;
}
public static void main(String[] args ){
BMIWindow bw=new BMIWindow();
}
}
5:打字母遊戲
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TypeGame {
public static void main(String[] args) {
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(300, 400);
PaintPanel panel = new PaintPanel();
window.add(panel);
Thread t = new Thread(panel);
t.start();
window.setVisible(true);
panel.requestFocus();
}
}
class PaintPanel extends JPanel implements Runnable, KeyListener {
int x[] = new int[10];
int y[] = new int[10];
char c[] = new char[10];
int score = 1000;
int speed = 80;
PaintPanel() {
setBackground(Color.darkGray);
addKeyListener(this);
for (int i = 0; i < 10; i++) {
x[i] = (int) (Math.random() * 280)+10;
y[i] = (int) (Math.random() * 300);
c[i] = (char) (Math.random() * 26 + 97);
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.orange);
for (int i = 0; i < 10; i++) {
g.drawString(new Character(c[i]).toString(), x[i], y[i]);
}
g.setColor(Color.red);
g.drawString("你的成績是:" + score, 5, 10);
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
for (int i = 0; i < 10; i++) {
y[i]++;
if (y[i] > 400) {
y[i] = 0;
x[i] = (int) (Math.random() * 280)+10;
score -= 100;
}
}
try {
Thread.sleep(speed);
} catch (Exception e) {
}
repaint();
}
}
@Override
public void keyPressed(KeyEvent e) {
char keyC = e.getKeyChar();
boolean flag = false;
for (int i = 0; i < 10; i++) {
if (keyC == c[i]) {
y[i] = 0;
x[i] = (int) (Math.random() * 280)+10;
c[i] = (char) (Math.random() * 26 + 97);
flag = true;
break;
}
}
if(flag){
score+=10;
}else{
score-=10;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
6:班級隨機點名
package views;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class MainWindow extends JFrame implements ActionListener{
JButton jbutton=new JButton("開始點名"); //點名按鍵
Random r=new Random(); //隨機數對象
ArrayList<String> student=new ArrayList<>();//數據存放數組
String filepath="src/student.txt"; //文件路徑
//構造函數
MainWindow(){
super("隨機點名1.0");
init(); //窗口初始化
readfile(); //讀取文件
}
public void init(){
jbutton.setBorderPainted(false); //按鈕無邊框
jbutton.setFocusPainted(false); //text無邊框
jbutton.setBackground(Color.orange);//設置背景顏色
jbutton.setFont(new Font("宋體",Font.BOLD,40)); //設置字體大小
jbutton.setForeground(Color.white);
jbutton.addActionListener(this);
add(jbutton); //添加到FRAME
this.setSize(500, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
int index=r.nextInt(student.size());
jbutton.setText(student.get(index));
}
public void readfile(){
try {
FileReader file=new FileReader(filepath); //建立文件對象
BufferedReader buff=new BufferedReader(file);//讀取文件到Buffer
String s; //定義空字符串,儲存從BUFf中讀到的每一行數據
while((s=buff.readLine())!=null){
student.add(s); //將讀到的字符串存放到ArrayList中
}
buff.close(); //關閉連接
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args){
MainWindow m=new MainWindow();
}
}
7:複製文件
package copyFile;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Copy extends JFrame implements ActionListener{
public static void main(String []args){
new Copy();
}
JLabel readLabel;
JLabel writeLabel;
JTextField readFiled;
JTextField writeFiled;
JButton readBut;
JButton writeBut;
JButton yesBut;
JFileChooser filechooser;
String readPath;
String writePath;
public Copy(){
readLabel=new JLabel("文件:");
writeLabel=new JLabel("保存到:");
readFiled=new JTextField(20);
writeFiled=new JTextField(20);
readBut=new JButton("選擇");
writeBut=new JButton("選擇");
yesBut=new JButton("確定");
readBut.addActionListener(this);
writeBut.addActionListener(this);
yesBut.addActionListener(this);
JPanel p=new JPanel();
p.add(readLabel);
p.add(readFiled);
p.add(readBut);
p.add(writeLabel);
p.add(writeFiled);
p.add(writeBut);
p.add(yesBut);
add(p);
this.setBounds(300, 300, 400, 400);
this.setVisible(true);
this.setTitle("複製文件");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Boolean copyFile(String oldPath,String newPath){
if(oldPath==null||newPath==null)
return false;
System.out.println(oldPath+newPath);
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { // 文件存在時
InputStream inStream = new FileInputStream(oldPath); // 讀入原文件
System.out.println(newPath);
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; // 字節數 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
fs.close();
}
} catch (Exception e) {
System.out.println("複製單個文件操作出錯");
e.printStackTrace();
return false;
}
return true;
}
public void actionPerformed(ActionEvent e) {
JButton jbutton=(JButton)e.getSource();
if(jbutton==readBut){
System.out.println("readBut");
filechooser=new JFileChooser();
filechooser.setDialogTitle("選擇要複製的文件");
if(filechooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
readPath=filechooser.getSelectedFile().getAbsolutePath();
readFiled.setText(readPath);
}
}
if(jbutton==writeBut){
System.out.println("writeBut");
filechooser=new JFileChooser();
filechooser.setDialogTitle("選擇文件夾");
filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if(filechooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION){
writePath=filechooser.getSelectedFile().getAbsolutePath()+"\\";
writeFiled.setText(writePath);
}
}
if(jbutton==yesBut){
if(!copyFile(readPath,writePath)){
System.out.println("不能爲空");
}
}
}
}
二 java實驗彙總
javaBase工程打包地址:https://www.lanzous.com/i71mm1i
Java實驗報告下載地址:https://www.lanzous.com/i71modc
Java開發文檔:https://www.lanzous.com/i71mqbc
Java隨機點名工程:下載:https://www.lanzous.com/i71mm2j 密碼:8hb6