2022编程设计与开发各个项目代码

目录

九九乘法表 

统计1-100间的数的总和

 请找出100以内3的倍数输出并统计个数

假设某小组成员的考试成绩为“68,78,96,56,47,56,75,12,54,96,45,68”,请将其存入数组,并判断各成绩等级有多少人

输入一个人的年龄,判断其属于什么年纪。

 键盘输入一个学生成绩,对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”

 请编写一个程序,实现文件内容的读写操作

请编写一个程序操作src\aa.txt文件,若文件不存在则创建,并判断该对象是否已存在,输出文件名、路径名和文件大小等信息

在电脑E:\TXT下创建一个文件为HC.txt文件,判断他是文件还是目录,在创建一个目录IOTest,之后将文件移动到IT目录下去;之后遍历IT这个目录下的

请定义两个类,实现继承关系。

要求:一个父类Animal和子类Dog ,类中有方法shout(),实现两个类间的继承

请编写一个居民身份证信息的正确输入与判断的程序。

要求:

该身份证须从键盘输入;

请用字符串类的方法判断身份证是否合法(长度18位,前17位必须为数字,最后一位为数字或X);

根据身份证判断是否为山东省居民;

根据身份证判断性别;

根据身份证判断年龄;

有异常类的应用。

请定义三个类,并实现类的继承和多态。

要求:

有Animal类、Tiger类、Sheep类;

Sheep和Tiger两类继承自Anima类;

正确生成各类的对象;

各类中都有cry()方法;

实现cry()的多态

请编写一个聊天室的程序

利用Java的GUI编程,编写一个窗体,包含两个文本框和一个命令按钮。其中一个文本框接收用户输入的一行字符串,回车后在另一个文本框中重复输出三行,单击命令按钮可清空两个文本框的所有内容


 

九九乘法表 



public class 九九乘法表 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//第一种方法
	/*	for (int i=1;i<=9;i++) {
			for (int j=1;j<=i;j++) {
				System.out.print(j+"*"+i+"="+i*j+"\t");
			}
			System.out.println();
		}*/
		
		//第二种方法
		int i=1;
		while(i<=9){
			int j=1;
			while (j<=i) 
			{
			System.out.print(i+"*"+j+"="+i*j+"\t");
			j++;
			}
			System.out.println();
			i++;
		}
	}

}

统计1-100间的数的总和


public class Sum {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
			
				int sum=0;
				int i=1;
				do
				{
					sum+=i;
					i++;
				}
				while(i<=100); 
		                System.out.println("1到100(包括100)的数的总和为:"+sum); 
		        } 


	}

 请找出100以内3的倍数输出并统计个数

 


public class three {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int count =0;
		int i;
		for(i=1;i<=100;i++)
		{
		if(i%3==0)
		{
		System.out.print(i+" ");
		count ++;
		}
		i++;
		}
		System.out.print("共计"+count+"个");

		
	}

}

假设某小组成员的考试成绩为“68,78,96,56,47,56,75,12,54,96,45,68”,请将其存入数组,并判断各成绩等级有多少人

public class Score {

		public static void main(String[] args) {
			// TODO Auto-generated method stub
			int[] score={68,78,96,56,47,56,75,12,54,96,45,68};
			int dj,yx=0,lh=0,zd=0,jg=0,bjg=0,sum=0,avg=0;
			for(int i=0;i<12;i++) 
			{
				sum=sum+score[i];
				if(score[i]>=90) 
					yx++;
				else
					if(score[i]>=80) 
						lh++;
					else
						if(score[i]>=70) 
							zd++;
						else
							if(score[i]>=60) 
								jg++;
							else
								bjg++;
			}
			avg=sum/12; 
			System.out.println("优秀人数:"+yx);
			System.out.println("良好人数:"+lh); 
			System.out.println("中等人数:"+zd); 
			System.out.println("及格人数:"+jg);
	 		System.out.println("不及格人数:"+bjg); 
			System.out.println("平均分:"+avg); 
		}
	}

输入一个人的年龄,判断其属于什么年纪。

import java.util.Scanner;

public class nianji { 
	
public static void main(String[] args) {
Scanner rc=new Scanner(System.in);
int age= rc.nextInt(); 
//此处开始年龄判断 
if (age>=55) {  
System.out.println("此人的年龄为老年");
} else if (age >30) {
System.out.println("此人的年龄为中年");
} else if (age > 18) {
System.out.println("此人的年龄为青年");	
} else {System.out.println("此人的年龄为少年");}
}
} 

 键盘输入一个学生成绩,对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”

import java.util.Scanner;

public class cjpd {
    public static void main(String[] args) {
    /*
    对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”
     */
    Scanner scan = new Scanner(System.in);
        System.out.println("请输入该同学的成绩检验是否合格:");
        int score = scan.nextInt();
        if(score<0 && score > 100){
            System.out.println("抱歉,您输入的数据非法,请重新输入");
        }
        int i = score / 10;
        switch (i){
            case 1:
                System.out.println("不合格");
                break;
            case 2:
                System.out.println("不合格");
                break;
            case 3:
                System.out.println("不合格");
                break;
            case 4:
                System.out.println("不合格");
                break;
            case 5:
                System.out.println("不合格");
                break;
            case 6:
                System.out.println("合格");
                break;
            case 7:
                System.out.println("合格");
                break;
            case 8:
                System.out.println("合格");
                break;
            case 9:
                System.out.println("合格");
                break;
        }
    }
}

 请编写一个程序,实现文件内容的读写操作

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
	
public class Read {
public static void main(String args[])
{
	try 
{
	FileWriter f_out=new FileWriter("D:/test.txt",true);
	f_out.write("有志者,事竟成");
	f_out.close();
	FileReader f_in=new FileReader("D:/test.txt");
	for (int c = f_in.read(); c!=-1;c = f_in.read()) 
		System.out.print((char)c);
		f_in.close();
} catch (IOException e) {
	System.err.println("发现异常"+e);
	e.printStackTrace();
	// TODO: handle exception
}}
}

请编写一个程序操作src\aa.txt文件,若文件不存在则创建,并判断该对象是否已存在,输出文件名、路径名和文件大小等信息

import java.io.*;
public class fileread {
	public static void main(String[] args) {
		File f1=new File("src\\aa.txt");//相对路径,如果没有前面的src,就在当前目录创建文件
		if(f1.exists()) {
			System.out.println("文件已经存在");
		}else {
			try {
				f1.createNewFile();
				System.out.println("文件创建成功");
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		System.out.println("文件已经存在:"+f1.exists());
		System.out.println("文件的名字:"+f1.getName());
		System.out.println("文件的路径:"+f1.getPath());
		System.out.println("文件的绝对路径:"+f1.getAbsolutePath());
		System.out.println("文件大小:"+f1.length());
	}
}

在电脑E:\TXT下创建一个文件为HC.txt文件,判断他是文件还是目录,在创建一个目录IOTest,之后将文件移动到IT目录下去;之后遍历IT这个目录下的

import java.io.File;
import java.io.IOException; 
public class iotest {
	public static void main(String[] args) {
		
		File file=new File("e:/TXT","HC.txt");
		//创建文件,返回一个布尔值
		boolean isCreate; 
		try {
			isCreate = file.createNewFile();
			if (isCreate) {
				System.out.println("创建文件成功!");
			}else {
				System.out.println("创建文件失败!文件已经存在");
			}
		} catch (IOException e) {
			System.out.println("创建文件失败!");
		}
		
		if (file.isFile()) {
			System.out.println("这是一个文件");
		} else {
			System.out.println("这是一个目录");
		}
		
		File file2=new File("D:/ IT");
		file2.mkdirs();
		if (file.renameTo(file2)) {
			System.out.println("文件移动成功!");
		} else {
			System.out.println("文件移动失败");
		}
		String[] arr=file2.list();
		for (String string : arr) {
			System.out.println(string);
		}
	}
}

请定义两个类,实现继承关系。

要求:一个父类Animal和子类Dog ,类中有方法shout(),实现两个类间的继承

public class Animal {
	String name="动物"; 
			void shout(){
			System.out.println("动物发出叫声"); 
			}
	      }
class Dog extends Animal
{
String name="犬"; 

/*void shout()
{
super.shout();
}*/
void printN()
{
	System.out.print(name+"="+super.name); }
}

public class extend
{
	public static void main(String[] args)
{
Dog dog =new Dog();
dog.shout();
dog.printN();
}
}

 

请编写一个居民身份证信息的正确输入与判断的程序。

要求:

  1. 该身份证须从键盘输入;

  2. 请用字符串类的方法判断身份证是否合法(长度18位,前17位必须为数字,最后一位为数字或X);

  3. 根据身份证判断是否为山东省居民;

  4. 根据身份证判断性别;

  5. 根据身份证判断年龄;

有异常类的应用。

import java.util.Scanner; 
public class sfz {
	public static void main(String[] args) {
try{
		Scanner jp=new Scanner(System.in); 
		String sfzh="";
		boolean xhflag=true; 
		boolean sfzflag=true;
		while(xhflag==true)  
		{
			sfzflag=true;
			sfzh=jp.next();
			if(sfzh.length()!=18)  
				System.out.println("位数错!请重新输入。");
			else
			{
				char []sfz=sfzh.toCharArray();
				for(int i=0;i<17;i++)
				{
					if(sfz[i]<'0'||sfz[i]>'9') 
						sfzflag=false;
				}
				if(sfzflag==false)
					{
					System.out.println("号码字符内容错!请重新输入。");
					}
				else
				{
		if((sfz[17]<'0'||sfz[17]>'9')&&((sfz[17]<'x'||sfz[17]>'x')))
					{             	
						System.out.println("尾数号码字符内容错!请重新输入。");
					}
					else
					{
						xhflag=false;
						}
						if(sfzh.startsWith("37"))
							{System.out.println("为山东省居民。"); }
						else
							{System.out.println("非山东省居民。");
						}
						char k=sfzh.charAt(16);
						int l=Integer.valueOf(k);	
                   if(l%2==0)
                   { 
							System.out.println("性别为:女");//老师提供的代码不对嗷,奇数男偶数女
							} 
						else{
							System.out.println("性别为:男");
						}
						int age=2022-Integer.valueOf(sfzh.substring(6,10));
					    
					    	System.out.println("年龄:"+age);					
						
                   }
				}
			}
		}

			
			
		
			catch(Exception e)
{
 System.out.println("异常信息为:"+e.getMessage());
}
				}
			
		
	}

  1. 请定义三个类,并实现类的继承和多态

要求:

  1. Animal类、Tiger类、Sheep类;

  2. SheepTiger两类继承自Anima类;

  3. 正确生成各类的对象;

  4. 各类中都有cry()方法;

实现cry()的多态

class Animal {
void cry() {
System.out.println("动物发出叫声");}}
class Tiger extends Animal {
void cry() {
System.out.println("嗷呜……");}}
class Sheep extends Animal {
void cry() {
System.out.println("咩咩……");}}
public class jichengheduotai {
public static void main(String[] args) {
Tiger  tiger = new Tiger();
Sheep sheep = new  Sheep();
tiger.cry();
sheep.cry();
}}

请编写一个数字计算器的程序

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
public class jsq {

	public static void cu()
	{
	JFrame f=new JFrame();
	f.setSize(600,200); 
	f.setLayout(new FlowLayout());
	final JTextField num1=new JTextField(12);
	JLabel jia=new JLabel("+");
	final JTextField num2=new JTextField(12);
	JLabel deng=new JLabel("=");
	final JLabel he=new JLabel("和");
	JButton bt=new JButton("计算");

	f.add(num1);
	f.add(jia);
	f.add(num2);
	f.add(deng);
	f.add(he);
	f.add(bt);
	
	ActionListener lst=new ActionListener() 
	{
		public void actionPerformed(ActionEvent e)
		{
			String ss1=num1.getText();
			if(ss1==null||ss1.trim().equals(""))
				ss1="0"; 
			int s1=Integer.parseInt(ss1); 
			String ss2=num2.getText();
			if(ss2==null||ss2.trim().equals(""))
				ss2="0";
			int s2=Integer.parseInt(ss2);
			int sum=s1+s2; 
		
           he.setText(String.valueOf(sum));
		}
	};
	bt.addActionListener(lst); 
	
	f.setVisible(true);
	}
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable(){public void run(){cu();}});
			}

}

请编写一个聊天室的程序

import java.awt.*; 
import java.awt.event.*;
import javax.swing.*;

public class chat {
	private static void cu()
	{
		JFrame f=new JFrame("聊天室");
		f.setSize(400,400); 
		f.setLayout(new BorderLayout());  
		f.setVisible(true);
		final JTextArea show=new JTextArea(10,20); 
		JScrollPane sp=new JScrollPane(show); 
		show.setEditable(false);
		final JTextField inp=new JTextField(20);
		JButton btu=new JButton("发送");
		JPanel pa=new JPanel();
		JLabel la=new JLabel("聊天信息:"); 
		pa.add(la); 
		pa.add(inp);
		pa.add(btu);
		f.add(sp,BorderLayout.PAGE_START);
		f.add(pa,BorderLayout.PAGE_END);
		ActionListener lst=new ActionListener()

		{
			public void actionPerformed(ActionEvent e)
			{
				String s=inp.getText(); 

				if(s!=null&&!s.trim().equals(""))


					show.append("我的信息:"+s+"\n");
				else
					show.append("宝贝,信息不能为空嗷!");
               inp.setText("");
			}
		};
		btu.addActionListener(lst);
		f.setVisible(true);
	}
	public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){public void run(){cu();}});
	}
}


利用JavaGUI编程,编写一个窗体,包含两个文本框和一个命令按钮。其中一个文本框接收用户输入的一行字符串,回车后在另一个文本框中重复输出三行,单击命令按钮可清空两个文本框的所有内容

import  java.awt.*; 
import  java.awt.event.*;
public class Gui extends Frame implements ActionListener{
TextField  text1,text2;
Button button; 
   Gui(){
     setLayout(new FlowLayout());
     text1=new TextField(10); 
     text2=new TextField(10);
     button=new Button("清空");
     add(text1);
     add(text2);
     add(button);
     addWindowListener(new WindowAdapter(){
              public void windowClosing(WindowEvent e){
                     System.exit(0);
}});
   text1.addActionListener(this); 
text2.addActionListener(this);
   button.addActionListener(this); 
   setBounds(100,100,400,200);
   setVisible(true); 
   validate();
}
public void actionPerformed(ActionEvent e){
   String ss=""; 
   if(e.getSource()==text1){ 
      String s=text1.getText();
      for(int i=0;i<3;i++)
     ss+=s+" ";
      text2.setText(ss);
}
if(e.getSource()==button){
  text1.setText("");text2.setText("");
}
}
public static void main(String[] args) {
   new Gui();
}}

如果对你有帮助,关注收藏走一波吧!!!

打赏更是极好的[doge]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值