JAVA课堂练习

更新时间:2024-04-27 22:50:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

1.将矩阵行列互调并输出。 public class ArrayExe {

public static void main(String[] args) { int count1=1; int count2=0; int array[][]=new int[3][3]; ArrayExe arr=new ArrayExe(); ///////////////// for(int i=0;i

2.计算购物结算,用户可以享受购物8折的优惠,请计算实际消费金额, (1)创建Java类Pay

(2)在Pay.java文件中声明变量存储信息

(3)计算总金额,消费总额 = 各商品的消费金额之和 * 折扣 public class Pay{ public static void main(String[] args) { // TODO Auto-generated method stub float applePrice,orangePrice; int appleNum,orageNum; float appleAmount,orangeAmount; float amount; float discountAmount; //给变量赋值 applePrice=2.5f; //float类型后加上f orangePrice=3; appleNum=3; orageNum=2; amount=applePrice*appleNum+orangePrice*orageNum; discountAmount=amount*0.8f; //discountAmount是float类型,所以是.8f System.out.println(\打折后的金额:\ } }

2.打印购物小票并计算积分. import java.util.Scanner; public class Array2 {

public static void main(String[] args) { int count; int array[]=new int[10]; int array2[]=new int[10]; for(count=0;count<5;count++){ Scanner input=new Scanner(System.in); System.out.print(\请输入第\个会员的积分:\ array[count]=input.nextInt(); } System.out.println(\ System.out.println(\序号\\t\\t历史积分\\t\\t最新积分\ for(int i=0;i<5 ;i++){ array2[i]=array[i]+500; System.out.println((i+1)+\ } } }

3.求1~10之间的所有偶数和(使用for循环) public class sum {

public static void main(String[] args) { int countSum=0;

for(int count=2;count<=10;count+=2){ countSum+=count; }

System.out.println(\之间偶数之和:\\ncountSum=\ }

}

4. 依次录入同学学习JAVA的成绩,统计各个分数段的成绩的人数。 import java.util.Scanner; public class javaGrade {

public static void main(String[] args) { Scanner input=new Scanner(System.in); int count1=0,count2=0,count3=0,count4=0,count5=0,count6=0 ; System.out.println(\输入所有JAVA成绩:\ for(int i=0;i<5;i++){ int stb=input.nextInt(); switch(stb/10){ case 9:count1++; break; case 8:count2++; break; case 7:count3++; break; case 6:count4++; break; case 10:count5++; break; default:count6++; break; } } System.out.println(\分的人数:\ System.out.println(\分的人数:\ System.out.println(\分的人数:\ System.out.println(\分的人数:\ System.out.println(\分的人数:\ System.out.println(\小于分的人数:\ } }

5. 以表格的形式输出笔购物金额及总金额 升序排列金额数据 import java.util.Scanner;

import java.util.Arrays; public class ArrayExe {

public static void main(String[] args) { int count; double Price[]=new double[5]; double sum=0; /////////////////////////////////////////输入部分 for(count=0;count<5;count++){ Scanner input=new Scanner(System.in); System.out.print(\请输入第\笔购物金额:\ Price[count]=input.nextDouble(); } System.out.println(\ /////////////////////////////////////输出部分 System.out.println(\序号\\t\\t金额(元)\ for(int i=0;i<5 ;i++){ System.out.println((i+1)+\ sum+=Price[i]; } System.out.println(\总金额\\t\\t\ System.out.println(\ /////////////////////////////表格输出部分 Arrays.sort(Price); System.out.println(\ System.out.print(\序号\\t|\ for(int i=0;i<5;i++){ System.out.print((i+1)+\ } System.out.print(\总金额\ System.out.println(\ System.out.println(\ System.out.print(\金额\\t|\ for(int i=0;i<5;i++){ System.out.print(Price[i]+\ } System.out.print(sum+\ System.out.println(\ System.out.println(\}

6.(1)创建两个管理员类对象,输出他们的相关信息 (2)设计一个有关修改密码的方法 /////1

public class Manage{ String name;

String password; public void show(){

System.out.println(\姓名:\密码:\ } } /////

import java.util.Scanner; public class InitManage{

public static void main(String[] args) { Manage m1=new Manage(); Manage m2=new Manage();

Scanner input=new Scanner(System.in);

System.out.println(\请分别输入两个用户名和密码\ m1.name=input.next(); m1.password=input.next(); m2.name=input.next(); m2.password=input.next();

System.out.println(\输出用户名和密码\ m1.show(); m2.show(); } }

/////////2修改密码

import java.util.Scanner; public class Management { String name; String password; String newPassword;

public void show(){

System.out.println(\姓名:\密码:\ }

//修改密码

public void changePassword(){

while(password.equals(newPassword)==false ){ System.out.print(\密码输入错误,请重新输入:\ Scanner input=new Scanner(System.in); newPassword=input.next(); }

System.out.println(\密码修改成功!\ } } //

import java.util.Scanner;

public class InitManagement {

public static void main(String[] args) { Management m1=new Management(); //Management m2=new Management(); Scanner input=new Scanner(System.in); m1.name=\张三\

System.out.print(\请输入密码\ m1.password=input.next();

System.out.print(\请重新输入密码\ m1.newPassword=input.next(); m1.changePassword(); m1.show(); } }

7.(1) 在setAge(int age) 中对年龄进行判断,如果年龄介于1到100直接赋值,否则抛出异常

在测试类中创建对象并调用setAge(int age)方法,使用try-catch捕获并处理异常 public class Person {

private String name=\张三\ private int age;

public void setAge(int age)throws Exception{ if(age>0&&age<100){ this.age=age; } else{

throw new Exception(\年龄必须在1-100之间\ } }

public void printAge(){

System.out.println(\年龄:\ } }

public class testPerson {

public static void main(String[] args){ Person person=new Person(); try{

person.setAge(101); person.printAge(); }catch(Exception e){ e.printStackTrace(); } }

}

public class testPerson {

public static void main(String[] args){ Person person=new Person(); try{

person.setAge(101); person.printAge(); }catch(Exception e){ e.printStackTrace(); } } }

public class testPerson {

public static void main(String[] args){ Person person=new Person(); try{

person.setAge(101); person.printAge(); }catch(Exception e){ e.printStackTrace(); } } }

(2)按照控制台提示输入1~3之间任一个数字,程序将输出相应的课程名称

根据键盘输入进行判断。如果输入正确,输出对应课程名称。如果输入错误,给出错误提示

不管输入是否正确,均输出“欢迎提出建议”语句 import java.util.Scanner; public class Courses {

public static void main(String[] args) { int n; try{

Scanner input=new Scanner(System.in); System.out.print(\请输入数字:1,2或3:\ n=input.nextInt(); switch(n){ case 1:

System.out.println(\英语\ break; case 2:

System.out.println(\数学\ break; case 3:

System.out.println(\语文\

break; default:

System.out.println(\必须使用1-3\ }

}catch(Exception e){

System.err.println(\发生异常,必须使用1-3\ }finally{

System.out.println(\欢迎提出建议\ } } }

8.事件的操作 import java.awt.*; import javax.swing.*; import java.awt.event.*; public class FirstFrame {

public static class moniter1 implements ActionListener{ public void actionPerformed(ActionEvent arg0){ System.out.print(\我被击中了\ } }

public static class moniter2 implements ActionListener{ public void actionPerformed(ActionEvent arg0){ System.out.print(\你被击中了\ } }

public static void main(String[] args) { // TODO Auto-generated method stub JFrame jf = new JFrame();

jf.setTitle(\ jf.setBounds(10,10,400,400); jf.setVisible(true);

GridLayout gridLayout=new GridLayout(3,3,10,20); jf.setLayout(gridLayout);

JButton btn1 = new JButton(\我\

btn1.addActionListener(new moniter1()); jf.add(btn1);

JButton btn2 = new JButton(\你\

btn2.addActionListener(new moniter2()); jf.add(btn2);

jf.add(new JButton(\ jf.add(new JButton(\ jf.add(new JButton(\

jf.add(new JButton(\ jf.add(new JButton(\ jf.add(new JButton(\ jf.add(new JButton(\ jf.validate(); } }

控件综合实验 import java.awt.*;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

import javax.print.DocFlavor.URL; import javax.swing.*;

import javax.swing.border.Border;

import javax.swing.border.EtchedBorder; import javax.swing.border.TitledBorder; @SuppressWarnings({ \public class SwingRegister extends JFrame {

public SwingRegister() { this.init(); }

public void init() {

this.setTitle(\用户注册\

this.setBounds(100, 100, 340,600);////////////// this.createUI();

this.setVisible(true);

this.setDefaultCloseOperation(EXIT_ON_CLOSE); }

JTextField nameTxt = new JTextField();

JPasswordField pwd = new JPasswordField(); JPanel panel = new JPanel(); public void createUI() {

// 添加边框

Border border = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED); TitledBorder tBorder = BorderFactory.createTitledBorder(border, \注册面板\TitledBorder.CENTER, TitledBorder.TOP); panel.setBorder(tBorder); panel.setLayout(null);

this.add(panel); //密码

JLabel pwdLbl = new JLabel(\输入密码:\pwdLbl.setBounds(10, 80, 60, 25); panel.add(pwdLbl);

pwd.setBounds(80, 80, 120, 22); panel.add(pwd);

//性别

JLabel genderLbl = new JLabel(\性别:\genderLbl.setBounds(10, 110, 60, 25); panel.add(genderLbl);

ButtonGroup group = new ButtonGroup();

JRadioButton fRdo = new JRadioButton(\男\fRdo.setBounds(80, 110, 50, 25); group.add(fRdo);

JRadioButton mRdo = new JRadioButton(\女\mRdo.setBounds(140, 110, 50, 25); group.add(mRdo); panel.add(fRdo); panel.add(mRdo); //姓名

JLabel nameLbl = new JLabel(\输入姓名:\nameLbl.setBounds(10, 50, 60, 25); panel.add(nameLbl);

//final JTextField nameTxt = new JTextField(); nameTxt.setBounds(80, 50, 120, 22); panel.add(nameTxt);

//学历

JLabel ediLbl = new JLabel(\学历:\ediLbl.setBounds(10, 140, 60, 25); panel.add(ediLbl);

//下拉列表

JComboBox edicbo = new JComboBox(); edicbo.addItem(\高中\edicbo.addItem(\大专\edicbo.addItem(\本科\edicbo.addItem(\其它\

edicbo.setBounds(80, 140, 80, 22); panel.add(edicbo);

本文来源:https://www.bwwdw.com/article/j6cg.html

Top