JAVA课堂练习
更新时间:2024-04-27 22:50:01 阅读量: 综合文库 文档下载
- 腾讯课堂Java推荐度:
- 相关推荐
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);
正在阅读:
JAVA课堂练习04-27
2012中考历史复习试题含11真题带解析第10单元建设有中国特色的社03-28
少儿编程有哪些课程?05-08
电机学实验指导书04-19
计算机专业的职业生涯规划书范文08-09
用列举法求概率与利用频率估计概率03-08
【礼仪主持】“三下乡”启动仪式演出主持词04-26
试验检测工程师模拟题04-06
中秋节活动策划方案【精选4篇】04-02
区委员会落实全面从严治党主体责任报告02-23
- 2009中西部家居博览会总体策划
- 2009 Revit 1级工程师学生用
- 天津地铁建设工程试验检测机构管理办法(TJDT-ZY-AQ-29)
- 新四年级数学暑期班第七次教案
- 机械制造企业隐患排查治理检查表 - 图文
- 2008届全国百套高考数学模拟试题分类汇编-103概率与统计解答题 -
- 职场健身防病试题及答案
- Excel操作技巧大全II - --数据输入和编辑技巧
- 南开大学2018春季《行政管理学》离线作业考核答案
- 2015年医师定考简易程序试卷及答案
- 新《预算法》对行政事业单位预算管理的挑战解读
- 轴的课件
- 电动汽车充电桩设计 毕业论文
- 必修2、选修2-1、1-1期末模拟试题2
- 桌面远程运维管理系统实施-可行性研究报告120306
- 西气东输水土保持工程工作总结 - 图文
- 正宁县基本县情及经济社会发展情况简介
- SATWE参数设置(巨详细)
- 儒家法思想研究综述
- 生活家政服务电子商务平台建设运营整合方案书【审报完稿】
- 课堂
- 练习
- JAVA
- VB讲义
- 像我这样一个女子续写
- 心理学资料
- 青海大学招收2012年攻读硕士学位研究生招生简章 - 图文
- 2016一带一路公需科目考试参考答案
- 数字测图原理与方法复习总结
- 第七章到第十五章的答案毛概课后答案
- 三阴乳腺癌治疗进展
- 焊缝的着色渗透探伤检验
- 青年员工培养方案
- after effects教案
- 综采设备(液压支架)技术协议(签字版)
- 员工守则7-15版一最新版(加页码后)
- (试卷合集)台州市2019届九年级英语期中考试卷16份试卷合集含答
- 热管热泵低温热能回收应用研究新11 - 图文
- 《小学数学课堂教学活动化》课题研究方案
- 壶口瀑布说课稿
- 上海市十四校2017届高三12月联考语文试卷汇总
- 被访者的期待与家用报警系统的市场前景
- 消费者行为学心得体会