關於javaelearning代碼的信息

本文目錄一覽:

java io代碼學習

package IO;

import java.io.*;

public class FileDirectoryDemo {

public static void main(String[] args) {

// 如果沒有指定參數,則缺省為當前目錄。

if (args.length == 0) {

args = new String[] { “.” };

}

try {

// 新建指定目錄的File對象。

File currentPath = new File(args[0]);

// 在指定目錄新建temp目錄的File對象。

File tempPath = new File(currentPath, “temp”);

// 用“tempPath”對象在指定目錄下創建temp目錄。

tempPath.mkdir();

// 在temp目錄下創建兩個文件。

File temp1 = new File(tempPath, “temp1.txt”);

temp1.createNewFile();

File temp2 = new File(tempPath, “temp2.txt”);

temp2.createNewFile();

// 遞歸顯示指定目錄的內容。

System.out.println(“顯示指定目錄的內容”);

listSubDir(currentPath);

// 更改文件名“temp1.txt”為“temp.txt”。

File temp1new = new File(tempPath, “temp.txt”);

temp1.renameTo(temp1new);

// 遞歸顯示temp子目錄的內容。

System.out.println(“更改文件名後,顯示temp子目錄的內容”);

listSubDir(tempPath);

// 刪除文件“temp2.txt”。

temp2.delete();

// 遞歸顯示temp子目錄的內容。

System.out.println(“刪除文件後,顯示temp子目錄的內容”);

listSubDir(tempPath);

} catch (IOException e) {

System.err.println(“IOException”);

}

}

// 遞歸顯示指定目錄的內容。

static void listSubDir(File currentPath) {

// 取得指定目錄的內容列表。

String[] fileNames = currentPath.list();

try {

for (int i = 0; i fileNames.length; i++) {

File f = new File(currentPath.getPath(), fileNames[i]);

// 如果是目錄,則顯示目錄名後,遞歸調用,顯示子目錄的內容。

if (f.isDirectory()) {

// 以規範的路徑格式顯示目錄。

System.out.println(f.getCanonicalPath());

// 遞歸調用,顯示子目錄。

listSubDir(f);

}

// 如果是文件,則顯示文件名,不包含路徑信息。

else {

System.out.println(f.getName());

}

}

} catch (IOException e) {

System.err.println(“IOException”);

}

}

}

package IO;

import java.io.*;

public class FileExample {

public FileExample() {

super();// 調用父類的構造函數

}

public static void main(String[] args) {

try {

String outfile = “demoout.xml”;

// 定義了一個變量, 用於標識輸出文件

String infile = “demoin.xml”;

// 定義了一個變量, 用於標識輸入文件

DataOutputStream dt = new DataOutputStream(

new BufferedOutputStream(new FileOutputStream(outfile)));

/**

* 用FileOutputStream定義一個輸入流文件,

* 然後用BuferedOutputStream調用FileOutputStream對象生成一個緩衝輸出流

* 然後用DataOutputStream調用BuferedOutputStream對象生成數據格式化輸出流

*/

BufferedWriter NewFile = new BufferedWriter(new OutputStreamWriter(

dt, “gbk”));// 對中文的處理

DataInputStream rafFile1 = new DataInputStream(

new BufferedInputStream(new FileInputStream(infile)));

/**

*用FileInputStream定義一個輸入流文件,

* 然後用BuferedInputStream調用FileInputStream對象生成一個緩衝輸出流

* ,其後用DataInputStream中調用BuferedInputStream對象生成數據格式化輸出流

*/

BufferedReader rafFile = new BufferedReader(new InputStreamReader(

rafFile1, “gbk”));// 對中文的處理

String xmlcontent = “”;

char tag = 0;// 文件用字符零結束

while (tag != (char) (-1)) {

xmlcontent = xmlcontent + tag + rafFile.readLine() + ‘\n’;

}

NewFile.write(xmlcontent);

NewFile.flush();// 清空緩衝區

NewFile.close();

rafFile.close();

System.gc();// 強制立即回收垃圾,即釋放內存。

} catch (NullPointerException exc) {

exc.printStackTrace();

} catch (java.lang.IndexOutOfBoundsException outb) {

System.out.println(outb.getMessage());

outb.printStackTrace();

} catch (FileNotFoundException fex) {

System.out.println(“fex” + fex.getMessage());

} catch (IOException iex) {

System.out.println(“iex” + iex.getMessage());

}

}

}

package IO;

import java.io.*;

public class FileRandomRW {

// 需要輸入的person數目。

public static int NUMBER = 3;

public static void main(String[] args) {

Persons[] people = new Persons[NUMBER];

people[0] = new Persons(“張峰”, 26, 2000, “N”);

people[1] = new Persons(“艷娜”, 25, 50000, “Y”);

people[2] = new Persons(“李朋”, 50, 7000, “F”);

try {

DataOutputStream out = new DataOutputStream(new FileOutputStream(

“peoplerandom.dat”));

// 將人員數據保存至“peoplerandom.dat”二進制文件中。

writeData(people, out);

// 關閉流。

out.close();

// 從二進制文件“peoplerandom.dat”中逆序讀取數據。

RandomAccessFile inOut = new RandomAccessFile(“peoplerandom.dat”,

“rw”);

Persons[] inPeople = readDataReverse(inOut);

// 輸出讀入的數據。

System.out.println(“原始數據:”);

for (int i = 0; i inPeople.length; i++) {

System.out.println(inPeople[i]);

}

// 修改文件的第三條記錄。

inPeople[2].setSalary(4500);

// 將修改結果寫入文件。

inPeople[2].writeData(inOut, 3);

// 關閉流。

inOut.close();

// 從文件中讀入的第三條記錄,並輸出,以驗證修改結果。

RandomAccessFile in = new RandomAccessFile(“peoplerandom.dat”, “r”);

Persons in3People = new Persons();

// 隨機讀第三條記錄。

in3People.readData(in, 3);

// 關閉流。

in.close();

System.out.println(“修改後的記錄”);

System.out.println(in3People);

} catch (IOException exception) {

System.err.println(“IOException”);

}

}

// 將數據寫入輸出流。

static void writeData(Persons[] p, DataOutputStream out) throws IOException {

for (int i = 0; i p.length; i++) {

p[i].writeData(out);

}

}

// 將數據從輸入流中逆序讀出。

static Persons[] readDataReverse(RandomAccessFile in) throws IOException {

// 獲得記錄數目。

int record_num = (int) (in.length() / Persons.RECORD_LENGTH);

Persons[] p = new Persons[record_num];

// 逆序讀取。

for (int i = record_num – 1; i = 0; i–) {

p[i] = new Persons();

// 文件定位。

in.seek(i * Persons.RECORD_LENGTH);

p[i].readData(in, i + 1);

}

return p;

}

}

class Persons {

private String name;

private int age; // 4個字節

private double salary; // 8個字節

private String married;

public static final int NAME_LENGTH = 20; // 姓名長度

public static final int MARRIED_LENGTH = 2; // 婚否長度

public static final int RECORD_LENGTH = NAME_LENGTH * 2 + 4 + 8

+ MARRIED_LENGTH * 2;

public Persons() {

}

public Persons(String n, int a, double s) {

name = n;

age = a;

salary = s;

married = “F”;

}

public Persons(String n, int a, double s, String m) {

name = n;

age = a;

salary = s;

married = m;

}

public String getName() {

return name;

}

public int getAge() {

return age;

}

public double getSalary() {

return salary;

}

public String getMarried() {

return married;

}

public String setName(String n) {

name = n;

return name;

}

public int setAge(int a) {

age = a;

return age;

}

public double setSalary(double s) {

salary = s;

return salary;

}

public String setMarried(String m) {

married = m;

return married;

}

// 設置輸出格式。

public String toString() {

return getClass().getName() + “[name=” + name + “,age=” + age

+ “,salary=” + salary + “,married=” + married + “]”;

}

// 寫入一條固定長度的記錄,即一個人的數據到輸出流。

public void writeData(DataOutput out) throws IOException {

FixStringIO.writeFixString(name, NAME_LENGTH, out);

out.writeInt(age);

out.writeDouble(salary);

FixStringIO.writeFixString(married, MARRIED_LENGTH, out);

}

// 寫入一條固定長度的記錄到隨機讀取文件中。

private void writeData(RandomAccessFile out) throws IOException {

FixStringIO.writeFixString(name, NAME_LENGTH, out);

out.writeInt(age);

out.writeDouble(salary);

FixStringIO.writeFixString(married, MARRIED_LENGTH, out);

}

// 隨機寫入一條固定長度的記錄到輸出流的指定位置。

public void writeData(RandomAccessFile out, int n) throws IOException {

out.seek((n – 1) * RECORD_LENGTH);

writeData(out);

}

// 從輸入流隨機讀入一條記錄,即一個人的數據。

private void readData(RandomAccessFile in) throws IOException {

name = FixStringIO.readFixString(NAME_LENGTH, in);

age = in.readInt();

salary = in.readDouble();

married = FixStringIO.readFixString(MARRIED_LENGTH, in);

}

// 從輸入流隨機讀入指定位置的記錄。

public void readData(RandomAccessFile in, int n) throws IOException {

in.seek((n – 1) * RECORD_LENGTH);

readData(in);

}

}

// 對固定長度字符串從文件讀出、寫入文件

class FixStringIO {

// 讀取固定長度的Unicode字符串。

public static String readFixString(int size, DataInput in)

throws IOException {

StringBuffer b = new StringBuffer(size);

int i = 0;

boolean more = true;

while (more i size) {

char ch = in.readChar();

i++;

if (ch == 0) {

more = false;

} else {

b.append(ch);

}

}

// 跳過剩餘的字節。

in.skipBytes(2 * (size – i));

return b.toString();

}

// 寫入固定長度的Unicode字符串。

public static void writeFixString(String s, int size, DataOutput out)

throws IOException {

int i;

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

char ch = 0;

if (i s.length()) {

ch = s.charAt(i);

}

out.writeChar(ch);

}

}

}

package IO;

import java.io.*;

import java.util.*;

public class FileRW {

// 需要輸入的person數目。

public static int NUMBER = 3;

public static void main(String[] args) {

Person[] people = new Person[NUMBER];

// 暫時容納輸入數據的臨時字符串數組。

String[] field = new String[4];

// 初始化field數組。

for (int i = 0; i 4; i++) {

field[i] = “”;

}

// IO操作必須捕獲IO異常。

try {

// 用於對field數組進行增加控制。

int fieldcount = 0;

// 先使用System.in構造InputStreamReader,再構造BufferedReader。

BufferedReader stdin = new BufferedReader(new InputStreamReader(

System.in));

for (int i = 0; i NUMBER; i++) {

fieldcount = 0;

System.out.println(“The number ” + (i + 1) + ” person”);

System.out

.println(“Enter name,age,salary,married(optional),please separate fields by ‘:'”);

// 讀取一行。

String personstr = stdin.readLine();

// 設置分隔符。

StringTokenizer st = new StringTokenizer(personstr, “:”);

// 判斷是否還有分隔符可用。

while (st.hasMoreTokens()) {

field[fieldcount] = st.nextToken();

fieldcount++;

}

// 如果輸入married,則field[3]不為空,調用具有四個參數的Person構造函數。

if (field[3] != “”) {

people[i] = new Person(field[0],

Integer.parseInt(field[1]), Double

.parseDouble(field[2]), field[3]);

// 置field[3]為空,以備下次輸入使用。

field[3] = “”;

}

// 如果未輸入married,則field[3]為空,調用具有三個參數的Person構造函數。

else {

people[i] = new Person(field[0],

Integer.parseInt(field[1]), Double

.parseDouble(field[2]));

}

}

// 將輸入的數據保存至“people.dat”文本文件中。

PrintWriter out = new PrintWriter(new BufferedWriter(

new FileWriter(“people.dat”)));

writeData(people, out);

// 關閉流。

out.close();

// 從文件“people.dat”讀取數據。

BufferedReader in = new BufferedReader(new FileReader(“people.dat”));

Person[] inPeople = readData(in);

// 關閉流。

in.close();

// 輸出從文件中讀入的數據。

for (int i = 0; i inPeople.length; i++) {

System.out.println(inPeople[i]);

}

} catch (IOException exception) {

System.err.println(“IOException”);

}

}

// 將所有數據寫入輸出流。

static void writeData(Person[] p, PrintWriter out) throws IOException {

// 寫入記錄條數,即人數。

out.println(p.length);

for (int i = 0; i p.length; i++) {

p[i].writeData(out);

}

}

// 將所有數據從輸入流中讀出。

static Person[] readData(BufferedReader in) throws IOException {

// 獲取記錄條數,即人數。

int n = Integer.parseInt(in.readLine());

Person[] p = new Person[n];

for (int i = 0; i n; i++) {

p[i] = new Person();

p[i].readData(in);

}

return p;

}

}

class Person {

private String name;

private int age;

private double salary;

private String married;

public Person() {

}

public Person(String n, int a, double s) {

name = n;

age = a;

salary = s;

married = “F”;

}

public Person(String n, int a, double s, String m) {

name = n;

age = a;

salary = s;

married = m;

}

public String getName() {

return name;

}

public int getAge() {

return age;

}

public double getSalary() {

return salary;

}

public String getMarried() {

return married;

}

// 設置輸出格式。

public String toString() {

return getClass().getName() + “[name=” + name + “,age=” + age

+ “,salary=” + salary + “,married=” + married + “]”;

}

// 寫入一條記錄,即一個人的數據到輸出流。

public void writeData(PrintWriter out) throws IOException {

// 格式化輸出。

out.println(name + “:” + age + “:” + salary + “:” + married);

}

// 從輸入流讀入一條記錄,即一個人的數據。

public void readData(BufferedReader in) throws IOException {

String s = in.readLine();

StringTokenizer t = new StringTokenizer(s, “:”);

name = t.nextToken();

age = Integer.parseInt(t.nextToken());

salary = Double.parseDouble(t.nextToken());

married = t.nextToken();

}

}

package IO;

import java.io.IOException;

public class FileStdRead {

public static void main(String[] args) throws IOException {

int b = 0;

char c = ‘ ‘;

System.out.println(“請輸入:”);

while (c != ‘q’) {

int a = System.in.read();

c = (char) a;

b++;

System.out.println((char) a);

}

System.err.print(“counted\t” + b + “\ttotalbytes.”);

}

}

//讀取輸入的數據,直到數據中有Q這個字母然

package IO;

import java.io.*;

public class IOStreamExample {

public static void main(String[] args) throws IOException {

// 1. 讀入一行數據:

BufferedReader in = new BufferedReader(new FileReader(

“FileStdRead.java”));

String s, s2 = new String();

while ((s = in.readLine()) != null) {

s2 += s + “\n”;

}

in.close();

BufferedReader stdin = new BufferedReader(new InputStreamReader(

System.in));

System.out.print(“Enter a line:”);

System.out.println(stdin.readLine());

// 2. 從內存中讀入

StringReader in2 = new StringReader(s2);

int c;

while ((c = in2.read()) != -1) {

System.out.print((char) c);

}

// 3. 格式化內存輸入

try {

DataInputStream in3 = new DataInputStream(new ByteArrayInputStream(

s2.getBytes()));

while (true) {

System.out.print((char) in3.readByte());

}

} catch (EOFException e) {

System.err.println(“End of stream”);

}

// 4. 文件輸入

try {

BufferedReader in4 = new BufferedReader(new StringReader(s2));

PrintWriter out1 = new PrintWriter(new BufferedWriter(

new FileWriter(“IODemo.out”)));

int lineCount = 1;

while ((s = in4.readLine()) != null) {

out1.println(lineCount++ + “: ” + s);

}

out1.close();

} catch (EOFException e) {

System.err.println(“End of stream”);

}

// 5. 接收和保存數據

try {

DataOutputStream out2 = new DataOutputStream(

new BufferedOutputStream(new FileOutputStream(“Data.txt”)));

out2.writeDouble(3.14159);

out2.writeUTF(“That was pi”);

out2.writeDouble(1.41413);

out2.writeUTF(“Square root of 2”);

out2.close();

DataInputStream in5 = new DataInputStream(new BufferedInputStream(

new FileInputStream(“Data.txt”)));

System.out.println(in5.readDouble());

System.out.println(in5.readUTF());

System.out.println(in5.readDouble());

System.out.println(in5.readUTF());

} catch (EOFException e) {

throw new RuntimeException(e);

}

// 6. 隨機讀取文件內容

RandomAccessFile rf = new RandomAccessFile(“rtest.dat”, “rw”);

for (int i = 0; i 10; i++) {

rf.writeDouble(i * 1.414);

}

rf.close();

rf = new RandomAccessFile(“rtest.dat”, “rw”);

rf.seek(5 * 8);

rf.writeDouble(47.0001);

rf.close();

rf = new RandomAccessFile(“rtest.dat”, “r”);

for (int i = 0; i 10; i++) {

System.out.println(“Value ” + i + “: ” + rf.readDouble());

}

rf.close();

}

}

package IO;

import java.io.*;

/**

* p

* Title: JAVA進階訣竅

* /p

*

* @author 張峰

* @version 1.0

*/

public class MakeDirectoriesExample {

private static void fileattrib(File f) {

System.out.println(“絕對路徑: ” + f.getAbsolutePath() + “\n 可讀屬性: “

+ f.canRead() + “\n 可定屬性: ” + f.canWrite() + “\n 文件名: “

+ f.getName() + “\n 父目錄: ” + f.getParent() + “\n 當前路徑: “

+ f.getPath() + “\n 文件長度: ” + f.length() + “\n 最後更新日期: “

+ f.lastModified());

if (f.isFile()) {

System.out.println(“輸入的是一個文件”);

} else if (f.isDirectory()) {

System.out.println(“輸入的是一個目錄”);

}

}

public static void main(String[] args) {

if (args.length 1) {

args = new String[3];

}

args[0] = “d”;

args[1] = “test1.txt”;

args[2] = “test2.txt”;

File old = new File(args[1]), rname = new File(args[2]);

old.renameTo(rname);

fileattrib(old);

fileattrib(rname);

int count = 0;

boolean del = false;

if (args[0].equals(“d”)) {

count++;

del = true;

}

count–;

while (++count args.length) {

File f = new File(args[count]);

if (f.exists()) {

System.out.println(f + ” 文件己經存在”);

if (del) {

System.out.println(“刪除文件” + f);

f.delete();

}

} else { // 如果文件不存在

if (!del) {

f.mkdirs();

System.out.println(“創建文件: ” + f);

}

}

fileattrib(f);

}

}

}

求一段java代碼

public class Student {

String name;

String sex;

int age;

public Student(){}

public Student(String name,String sex,int age){

this.name=name;

this.sex=sex;

this.age=age;

}

/**

 * PrintINFO

 */

public void printStudentInfo(){

System.err.println(“Name: “+name);

System.err.println(“Sex: “+sex);

System.err.println(“Age: “+age);

}

public static void main(String[] args) {

try {

Class? st = (Class?) Class.forName(“Student”);

Student student=(Student) st.newInstance();

student.name=”java”;

student.sex=”男”;

student.age=20;

Method[] methods=st.getMethods();

for (int i = 0; i  methods.length; i++) {

if(methods[i].getName().equals(“printStudentInfo”)){

methods[i].invoke(student);

}

System.out.println(“類方法結構:”+methods[i].getName());

}

Field[] s= st.getDeclaredFields();

for (int i = 0; i  s.length; i++) {

System.out.println(“類的結構字段:”+s[i].getName());

}

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InstantiationException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SecurityException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}catch (IllegalArgumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InvocationTargetException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

求用Java編寫的學生成績管理系統的完整代碼

package jdbcproj;

import java.sql.*;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JTextField;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.SQLException;

import java.awt.event.ActionEvent;

public class MainFrame extends JFrame {

private JPanel contentPane;

private JTextField txtname;

private JTextField txtpassword;

/**

 * Launch the application.

 */

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

MainFrame frame = new MainFrame();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

 * Create the frame.

 */

public MainFrame() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 661, 399);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

JLabel lblNewLabel = new JLabel(“\u7528\u6237\u540D”);

lblNewLabel.setBounds(114, 51, 72, 18);

contentPane.add(lblNewLabel);

JLabel lblNewLabel_1 = new JLabel(“\u5BC6\u7801”);

lblNewLabel_1.setBounds(114, 106, 72, 18);

contentPane.add(lblNewLabel_1);

txtname = new JTextField();

txtname.setBounds(261, 48, 86, 24);

contentPane.add(txtname);

txtname.setColumns(10);

txtpassword = new JTextField();

txtpassword.setBounds(261, 103, 86, 24);

contentPane.add(txtpassword);

txtpassword.setColumns(10);

JButton btnadd = new JButton(“\u589E\u52A0”);

btnadd.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(txtname.getText().equals(“”)||txtpassword.getText().equals(“”))

{

JOptionPane.showMessageDialog(getContentPane(), “用戶名和密碼不能為空”,”提示信息框”,JOptionPane.WARNING_MESSAGE);

}

else{

Users u=new Users();

u.setPwd(txtpassword.getText());

u.setUsername(txtname.getText());

UserDAO usdo=new UserDAO();

usdo.addUser(u);

}

}

});

btnadd.setBounds(45, 205, 113, 27);

contentPane.add(btnadd);

JButton btndelete = new JButton(“\u5220\u9664”);

btndelete.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

if(txtname.getText().equals(“”))

{

JOptionPane.showMessageDialog(getContentPane(), “用戶名不能為空”,”提示信息框”,JOptionPane.WARNING_MESSAGE);

}

else{

UserDAO usdo=new UserDAO();

usdo.delUser(txtname.getText());;

}

}

});

btndelete.setBounds(172, 205, 113, 27);

contentPane.add(btndelete);

JButton btnupdate = new JButton(“\u4FEE\u6539”);

btnupdate.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(txtname.getText().equals(“”)||txtpassword.getText().equals(“”))

{

JOptionPane.showMessageDialog(getContentPane(), “用戶名和密碼不能為空”,”提示信息框”,JOptionPane.WARNING_MESSAGE);

}

else{

Users u=new Users();

u.setPwd(txtpassword.getText());

u.setUsername(txtname.getText());

UserDAO usdo=new UserDAO();

usdo.updateUser(u);;

}

}

});

btnupdate.setBounds(300, 205, 113, 27);

contentPane.add(btnupdate);

JButton btnfind = new JButton(“\u67E5\u8BE2”);

btnfind.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(txtname.getText().equals(“”))

{

JOptionPane.showMessageDialog(getContentPane(), “用戶名不能為空”,”提示信息框”,JOptionPane.WARNING_MESSAGE);

}

else{

Users u=new Users();

UserDAO usdo=new UserDAO();

u=usdo.findUser(txtname.getText(), txtpassword.getText());

if(u!=null){

JOptionPane.showMessageDialog(getContentPane(), “該用戶存在!”,”提示信息框”,JOptionPane.WARNING_MESSAGE);

}

else{

JOptionPane.showMessageDialog(getContentPane(), “該用戶不存在!”,”提示信息框”,JOptionPane.WARNING_MESSAGE);

}

}

}

});

btnfind.setBounds(427, 205, 113, 27);

contentPane.add(btnfind);

//記得要寫這個

setVisible(true);

}

}

Java編程,填寫下面的代碼

class NoLowerLetterException extends Exception {

public NoLowerLetterException(String msg) {

super(msg);

}

}

class NoDigitException extends Exception {

public NoDigitException(String msg) {

super(msg);

}

}

class People {

void printLetter(char c) {

if (c = ‘a’ c = ‘z’) {

System.out.println(c);

} else {

try {

throw new NoLowerLetterException(String.valueOf(c));

} catch (NoLowerLetterException e) {

e.printStackTrace();

}

}

}

void printDigit(char c) {

if (c = ‘0’ c = ‘9’) {

System.out.println(c);

} else {

try {

throw new NoDigitException(String.valueOf(c));

} catch (NoDigitException e) {

e.printStackTrace();

}

}

}

}

public class ExceptionExample {

public static void main(String args[]) {

People people = new People();

for (int i = 0; i 128; i++) {

// 【代碼5】

// //將i轉換為char類型,執行people.printLetter()方法,如果出現異常則捕獲,並輸出異常的錯誤信息!

people.printLetter((char) i);

}

for (int i = 0; i 128; i++) {

// 【代碼6】 //將i轉換為char類型,執行people. printDigit

// ()方法,如果出現異常則捕獲,並輸出異常的錯誤信息!

people.printDigit((char) i);

}

}

}

原創文章,作者:ECFW,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/133518.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
ECFW的頭像ECFW
上一篇 2024-10-03 23:59
下一篇 2024-10-03 23:59

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python字符串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字符串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字符串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變量和數…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • 倉庫管理系統代碼設計Python

    這篇文章將詳細探討如何設計一個基於Python的倉庫管理系統。 一、基本需求 在着手設計之前,我們首先需要確定倉庫管理系統的基本需求。 我們可以將需求分為以下幾個方面: 1、庫存管…

    編程 2025-04-29
  • 寫代碼新手教程

    本文將從語言選擇、學習方法、編碼規範以及常見問題解答等多個方面,為編程新手提供實用、簡明的教程。 一、語言選擇 作為編程新手,選擇一門編程語言是很關鍵的一步。以下是幾個有代表性的編…

    編程 2025-04-29
  • Python實現簡易心形代碼

    在這個文章中,我們將會介紹如何用Python語言編寫一個非常簡單的代碼來生成一個心形圖案。我們將會從安裝Python開始介紹,逐步深入了解如何實現這一任務。 一、安裝Python …

    編程 2025-04-29
  • 怎麼寫不影響Python運行的長段代碼

    在Python編程的過程中,我們不可避免地需要編寫一些長段代碼,包括函數、類、複雜的控制語句等等。在編寫這些代碼時,我們需要考慮代碼可讀性、易用性以及對Python運行性能的影響。…

    編程 2025-04-29
  • Python愛心代碼動態

    本文將從多個方面詳細闡述Python愛心代碼動態,包括實現基本原理、應用場景、代碼示例等。 一、實現基本原理 Python愛心代碼動態使用turtle模塊實現。在繪製一個心形的基礎…

    編程 2025-04-29
  • 北化教務管理系統介紹及開發代碼示例

    本文將從多個方面對北化教務管理系統進行介紹及開發代碼示例,幫助開發者更好地理解和應用該系統。 一、項目介紹 北化教務管理系統是一款針對高校學生和教職工的綜合信息管理系統。系統實現的…

    編程 2025-04-29

發表回復

登錄後才能評論