zoukankan      html  css  js  c++  java
  • java所有内容练习

    分支, 循环, 数据类型

    有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

    	int sum = 0;
    
        	for (int i = 1; i < 5; i++) {
    			for (int j = 1; j < 5; j++) {
    				for (int j2 = 1; j2 < 5; j2++) {
    					if (i != j&&i != j2&&j!=j2) {  //不能有重复数字
    						sum++;   //计算能够组成的三位数的个数
    						System.out.println(i+" "+j+" "+j2);
    					}
    					
    				}
    			}
    		}
    		
    		System.out.println(sum);
    

    有一分数序列:2 / 1,3 / 2,5 / 3,8 / 5,13 / 8,21 / 13...求出这个数列的前20项之和。

    double j = 1.0;   //分子
    		double k = 1.0;	//分母  规律:分子加分母等于下一个的分子,分子当分母
    		double sum2 = 0.0;  
    		int ci = 0;
    		for (int i = 0; i < 20; i++) {
    			j = j+k;
    			k = j-k;
    			ci++;
    			sum2 = sum2 + j/k;  //求20项的和
    			System.out.println(j/k);
    		}
    		System.out.println("和:"+sum2);
    		System.out.println("打印数量:"+ci);
    

    求1! + 2! + 3! +...+ 20!的和

    	 long result = 0;
    		 int f = 1;
    		 for (int i = 1; i <= 20 ; i++) {
    			f = f*i;
    			result += f;
    		}
    		 System.out.println(result);
    

    一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。

    String s = "22327";
    		if (s.charAt(0)== s.charAt(4)&&s.charAt(1)==s.charAt(3)) {  //取索引值上的字符
    			System.out.println("是回文数");
    		}else {
    			System.out.println("不是回文数");
    		}
    

     使用最少的代码将字符串String转换成字符数组

    String  s = "132";
    		char[] ch = new char[s.length()];  //定义一个字符数组
    		ch = s.toCharArray();  //   将字符串转换成一个字符数组
    		System.out.println(ch);
    
    //另一种方法(最少的代码)
    		String s = "shutup";
    		System.out.println(Arrays.toString(s.toCharArray()));
    

     将二进制字符串("11010011"), 八进制字符串("3325"), 十六进制字符串("5a3f")转换成数字, 在控制台上打印出来

    String e = "11010011";
    		String b = "3325";
    		String shi = "5a3f";
    		int e2 = Integer.parseInt(e);
    		System.out.println(e2);
    		int b2 = Integer.parseInt(b);
    		System.out.println(b2);
    		System.out.println(Integer.parseInt(shi,16)); //把字符串转换成16制
    

     取一个随便的字符串中字母出现的次数, 打印出来

    	Scanner sc = new Scanner(System.in);
    		System.out.println("请输入一串字符");
    		String shu = sc.nextLine();
    		char c [] = shu.toCharArray();  //将输入的字符串转换成一个字符数组
    		  Map<Character,Integer> map = new HashMap<Character,Integer>();
    		 for (char d : c) {  //遍历
    			map.put(d, map.get(d) == null ? 1: map.get(d)+1);  //三目运算符
    		}
    		  System.out.println(map);
    

     总结:此题用到了三目运算符,此处的字母为键值,如果此字母在map里面没有的,就是等于null,那么添进去的时候
        就是记一个1,下次此字母再次出现的时候,数字加1,put不是添加的意思,而是修改的意思

     有一个字符串形式的任意日期是"yyyy-MM-dd"的格式, 计算这个日期到1949年10月1日上午8点差了多少小时

     String date1 = "1949-10-01  08:00";
    		  	String date2 = "2017-12-07 09:58";
    		  	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    		  	Date d1 = null;
    			Date d2 = null;
    			try {
    				d1 = sdf.parse(date1);
    				d2 = sdf.parse(date2);
    			} catch (ParseException e) {
    				e.printStackTrace();
    			}
    			 long l1 = d1.getTime();  //获取的毫秒数
    			 long l2 = d2.getTime();
    			 System.out.println((l2-l1)/1000/60/60); //差几个小时
    			 //一秒 = 1000毫秒,一分=60秒,一小时 = 60分
    

    从data.txt文件中读取数据到程序中, 实现一个联动功能,
        即输入主食会显示"1---馒头    2---煎饼    3---米饭", 再次输入会显示下一级菜单
        data.txt文件中每一行都有被 "," 分割的三个值, 第一个值代表这项食物的编号(对于整个数据来说是唯一的),
        第三个值表示所属的上一级食物分类

    data.txt文件如下图:

    实现的效果:

    public class Food {
    		private String  xu;
    		private String ming ;
    		private String caidan;
    		public Food() {
    		}
    		public Food(String xu, String ming, String caidan) {
    			super();
    			this.xu = xu;
    			this.ming = ming;
    			this.caidan = caidan;
    		}
    		public String getXu() {
    			return xu;
    		}
    		public void setXu(String xu) {
    			this.xu = xu;
    		}
    		public String getMing() {
    			return ming;
    		}
    		public void setMing(String ming) {
    			this.ming = ming;
    		}
    		public String getCaidan() {
    			return caidan;
    		}
    		public void setCaidan(String caidan) {
    			this.caidan = caidan;
    		}
    		@Override
    		public String toString() {
    			return "菜单 [编号" + xu + ", 名字=" + ming + ", 类别=" + caidan + "]";
    		}
    		
    }
    
    public class Read {  
    
                public static void main(String[] args) {
    			
    		
    		List<Food> list2 = PrientFood("F:\data.txt");
    		for (Food ff : list2) {
    			if (ff.getCaidan().equals("0")) {  //把一开始的那个显示出来就可以了
    				System.out.println(ff);
    			}
    			
    		}
    			Scanner scanner = new Scanner(System.in);
    			System.out.println("请选择输入编号");
    			String str = scanner.nextLine();
    			
    				for (Food food : list2) {
    				if (str.equals(food.getCaidan())) {
    					System.out.println(food);
    				}
    			  }
    			
    			
    			System.out.println("请选择输入编号");
    			String str2 = scanner.nextLine();
    			for (Food food : list2) {
    				if (str2.equals(food.getCaidan())) {
    					System.out.println(food);
    				}
    			}
    			scanner.close();
    			
    	}
    	public static List<Food> PrientFood (String dizhi) {  //此方法是把读到的文件放在一个集合里面
    		File file = new File(dizhi);
    		List<Food> list  = null;
    		if (file.exists()) {  //判断如果文件存在的话
    			
    			try {
    				InputStream is = new FileInputStream(file);
    				InputStreamReader ir = new 	InputStreamReader (is,"UTF-8"); 
    				//字节流转换成字符流,碰到了乱码的问题,此时要考虑的就是这样的一种情况
    				BufferedReader br = new BufferedReader (ir);
    				 
    				String ss = null;
    				list = new ArrayList<Food>();
    				while ((ss = br.readLine())!= null) {
    					String [] str = ss.split(",");  //将读到的内容转换成字符串数组
    					Food fd = new Food(str[0],str[1],str[2]); 
    					//每次读一行,用","隔开的数组,每次取索引值
    					list.add(fd);  //添加进集合里面
    					
    				}
    				br.close();
    			} catch (FileNotFoundException e) {
    				e.printStackTrace();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}else {
    			System.out.println("没有找到文件");
    		}
    		
    		return list;  //返回值
    		
    		
    	}
    
    }
    

    写一个程序统计一个项目中src下的所有 .java 文件的代码行数总和(空行不算,)

    public class Daima {
    
                    public class Daima {  //写一个程序统计一个项目中src下的所有 .java 文件的代码行数总和(空行不算,)
    		private static int daima;  //代码行数
    		
    	public static void main(String[] args) {
    		
    		File file = new File("F:\1117\diyi\src");  //文件
    		SuanShu(file);
    		System.out.println(daima);
    		
    		
    	}
    	public static void SuanShu(File file) {
    		if (file.exists()) {
    			File [] shu = file.listFiles();  //获取文件夹下文件和子文件夹
    			if (shu != null) {  //判断里面有没有文件夹,或者是文件
    			for (File file2 : shu) { //遍历
    				if (file2.isDirectory()) { //是否是一个目录
    					SuanShu(file2); //递归,自身调用自身
    				}else {
    					if (file2.getName().endsWith(".java")) {
    						try {
    							Reader r = new FileReader(file2);
    							BufferedReader br = new BufferedReader(r);
    							String s = null;
    							while (( s = br.readLine()) != null) {
    								if (s.trim().length() >= 0) {
    									daima++; 
    								}
    							}
    							br.close();
    						} catch (FileNotFoundException e) {
    							e.printStackTrace();
    						} catch (IOException e) {
    							e.printStackTrace();
    						}
    					}
    		}
    		
    				}
    			}
    		}
    
    	}
    }
    

    写一个带线程同步的程序模拟5个人在火车站买票, 总共有4张票

    public class TestCount {
    
                private static int i = 4; //  票数
    		public static  synchronized void sail(String name) {
    			if( i > 0) { 
    			try {
    				Thread.sleep(1);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    				
    				System.out.println(name+"买了第"+i-- + "张票");
    				}else {
    					System.out.println("没票了");
    				}
    		}
    }
    

      注意的一点:      静态方法里只能用静态变量,不可以是非静态变量  ! ! ! ! ! !

    public class TestSynchronize {
    
    	public static void main(String[] args) {
    			TestThread tt = new TestThread();
    			Thread t1 = new Thread(tt);
    			Thread t2 = new Thread(tt);
    			Thread t3 = new Thread(tt);
    			Thread t4 = new Thread(tt);
    			Thread t5 = new Thread(tt);
    			t1.setName("小王");
    			t2.setName("小吴");
    			t3.setName("小刚");
    			t4.setName("小刘");
    			t5.setName("小孙");
    			t1.start();
    			t2.start();
    			t3.start();
    			t4.start();
    			t5.start();
    	}
    	
    }
    class TestThread implements Runnable{
    	//public Testcount testcount = new Testcount();
    	
    	@Override
    	public void run() {
    		TestCount.sail(Thread.currentThread().getName());   //获取当前线程的名字
    		//调取的是Testcount里的sail方法,里面传进去的名字是当前线程的名字,名字就可以改了
    	}
    	
    }
    

    设计一个分页工具类, 具有两个属性: page(当前页数),
        rows(每页显示行数), 实例化一个List<String>, 并为其添加30条以上的任意数据,
        再定义一个方法, 参数为此List和分页工具类, 调用时显示出相应的数据
        要求:
            分页工具类具有可设置属性的构造方法, 具有空参构造方法, 调用空参构造方法则自动为其设置属性
            调用方法时需要显示List里面相应的内容, 并打印"当前是第__页, 显示从__到__条"

    public class YeShu {
    	private int page;  //当前多少页
    	private int rows;  //一页多少行
    	private int start;  //在集合里面一开始要取的头(每一页的开头)
    	private int end;   //在集合里面一开始要取的尾(每一页的结尾)
    	public YeShu() {
    		this.page = 1;  //默认第一页
    		this.rows = 10;  //默认的一页有10行数据
    		this.start = (page - 1) * rows;  //此处注意的是集合里面的索引是从0开始的,也就是0-9
    		this.end = (page * rows) - 1;    
    	}
    	public YeShu(int page, int rows) {
    		this.page = page;
    		this.rows = rows;
    		this.start = (page - 1) * rows;  
    		this.end = (page * rows) - 1;
    	}
    	public void showpage () {
    		System.out.println("当前是第"+page+"页,从第"+(start+1)+"行,到第"+(end+1)+"行");
    	}
    	public int getPage() {
    		return page;
    	}
    	public void setPage(int page) {
    		this.page = page;
    	}
    	public int getRows() {
    		return rows;
    	}
    	public void setRows(int rows) {
    		this.rows = rows;
    	}
    	
    	public int getStart() {
    		return start;
    	}
    	public void setStart(int start) {
    		this.start = start;
    	}
    	public int getEnd() {
    		return end;
    	}
    	public void setEnd(int end) {
    		this.end = end;
    	}
    	
    	
    }
    
    public class TestYeShu { 
    
    
                    public static void main(String[] args) {
    		List<String> list = new ArrayList<String>();  //实例化一个集合
    		for (int i = 1; i < 37; i++) {
    			list.add("aaa"+i);  //添加了36条数据
    		}
    		YeShu ys = new YeShu(4,7);
    		showinfo(list,ys);
    		ys.showpage();
    		
    		
    	}
    	public static void showinfo (List<String> list,YeShu ys) {
    		for (int i = ys.getStart(); i <= ys.getEnd(); i++) {  //循环取出里面集合里的数据
    			try {
    				System.out.println(list.get(i));
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    
  • 相关阅读:
    Python菜鸟之路:Python基础-线程、进程、协程
    Python菜鸟之路:Python基础-Socket编程-2
    Python菜鸟之路:Python基础-Socket基础-1
    react-native 使用 antd-mobile-rn UI进行开发app
    Java基础
    Java基础
    Java基础
    Java基础
    Java基础
    Java基础
  • 原文地址:https://www.cnblogs.com/zuo72/p/7999744.html
Copyright © 2011-2022 走看看