Java 案例大全(详细)一

一直在更新

  • 案例汇总
    • 比身高
    • 判断奇偶数
    • 考试评价
    • 春夏秋冬
    • 正反输出数据
    • 求和1
    • 逢七过
    • 不死神兔
    • 百钱买百鸡
    • 输出所有时间
    • 珠穆朗玛峰
    • 求和2
    • 猜数字
    • 数组直接操作
    • 比较最大值
    • 获取最小值
    • 数组内容相同
    • 查找元素
    • 反转元素
    • 评委打分
    • 用户登录
    • 遍历字符串
    • 统计字符次数
    • 字符串的拼接1
    • 字符串的拼接2
    • 字符串反转1
    • 字符串反转2
    • 猫和狗1
    • 猫和狗2
    • 猫和狗3

案例汇总

比身高

三元运算符(?)

public class Test {
	//案例:三个和尚比身高
	public static void main(String args[]) {
		//三元运算符的使用
		int height1 = 160;
		int height2 = 170;
		int height3 = 180;
		int result1 = height1 > height2 ? height1 : height2;
		int result2 = result1 > height3 ? result1 : height3;
		System.out.println("身高最高是:" + result2);
	}
}

Scanner类

import java.util.Scanner;

public class Test {
	//案例:三个和尚比身高
	public static void main(String args[]) {
		//Scanner类的使用
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入第一个人的身高:");
		int height1 = sc.nextInt();
		System.out.print("请输入第二个人的身高:");
		int height2 = sc.nextInt();
		System.out.print("请输入第三个人的身高:");
		int height3 = sc.nextInt();
		int result1 = height1 > height2 ? height1 : height2;
		int result2 = result1 > height3 ? result1 : height3;
		System.out.println("身高最高的是:" + result2);
		sc.close();
	}
}

判断奇偶数

if … else …

import java.util.Scanner;

public class Test {
	//案例:判断奇偶数
	public static void main(String args[]) {
		System.out.print("请输入一个数字:");
		Scanner sc = new Scanner(System.in);
		int number = sc.nextInt();
		if (number % 2 == 0) {
			System.out.println(number + " 是偶数");
		}else {
			System.out.println(number + " 是奇数");
		}
		sc.close();
	}
}

考试评价

if……else if……else

import java.util.Scanner;

public class Test {
	//案例:考试成绩
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入你的考试成绩:");
		int score = sc.nextInt();
		if (score > 100 || score < 0) {
			System.out.println("你输入的有误!!");
		}else if (score > 90 && score <= 100) {
			System.out.println("优秀!");
		}else if (score > 80 && score <= 90) {
			System.out.println("良好!");
		}else if (score >= 60 && score <= 80) {
			System.out.println("及格!!");
		}else {
			System.out.println("不及格!!!");
		}
		sc.close();
	}
}

春夏秋冬

switch

import java.util.Scanner;

public class Test {
	//案例:春夏秋冬
	public static void main(String args[]) {
	
		Scanner sc=new Scanner(System.in);
		System.out.print("请输入对应的月份:");
		int month=sc.nextInt();
		
		switch(month) {
			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;
			case 10:
				System.out.println("秋季");
				break;
			case 11:
				System.out.println("秋季");
				break;
			case 12:
				System.out.println("冬季");
				break;
			default:
				System.out.println("输入的数字有误!!");
		}
		sc.close();
	}
}

补充 case穿透

import java.util.Scanner;

public class Test {
	//案例:春夏秋冬
	public static void main(String args[]) {
		Scanner sc=new Scanner(System.in);
		System.out.print("请输入对应的月份:");
		int month=sc.nextInt();
		
		switch(month) {
			case 12:
			case 1:
			case 2:
				System.out.println("冬季");
				break;
			case 3:
			case 4:
			case 5:
				System.out.println("春季");
				break;
			case 6:
			case 7:
			case 8:
				System.out.println("夏季");
				break;
			case 9:
			case 10:
			case 11:
				System.out.println("秋季");
				break;
			default:
				System.out.println("输入的数字有误!!");
		}
		sc.close();
	}
}

正反输出数据

for 循环

public class Test {
	//案例:正反输出数据
	public static void main(String args[]) {
		for(int i = 1;i <= 5; i++) {
//			System.out.println(i);
			System.out.print(i + " ");
		}
		System.out.println();
		for(int i = 5; i >= 1; i--) {
			System.out.print(i + " ");
		}
	}
}

求和1

100以内求和

public class Test {
	//案例:100以内求和
	public static void main(String args[]) {
		int sum = 0;
		for(int i = 1;i <= 100; i++) {
			sum += i;
		}
		System.out.println("总共:" + sum);
	}
}

求偶数和

public class Test {
	//案例:100以内求偶数和
	public static void main(String args[]) {
		int sum=0;
		for(int i = 1;i <= 100; i++) {
			if(i % 2 == 0) {
				sum += i;
			}
		}
		System.out.println("偶数和为:" + sum);
	}
}

求水仙花数

public class Test {
	//案例:求水仙花数
	//水仙花数是一个三位数,百位数,十位数,个位数的立方和等于它本身
	public static void main(String args[]) {
		int hunderds = 0, decade = 0, unit = 0;
		int result = 0;
		int count = 0; //计数
		for(int i = 100; i < 1000; i++) {
			hunderds = i / 100;
			decade = i / 10 % 10;
			unit = i % 10;
			result = hunderds * hunderds * hunderds + decade * decade * decade + unit * unit * unit;
			if(result == i) {
				System.out.println(i);
				count++;
			}
		}
		System.out.println("总共:" + count + "个数");
	}
}

逢七过

for 循环


public class Test {
	//案例:逢七过
	public static void main(String args[]) {
		int count = 0;
		for(int i = 1; i < 101; i++) {
			if(i % 7 !=0 && i / 10 != 7 && i % 10 != 7) {
				System.out.print(i + " ");
				count++;
			}
		}
		System.out.println("总共有:" + count + "个数");
	}	
}

不死神兔

for循环


public class Test {
	//案例:不死神兔
	//需求:有一对兔子,从出生后第三个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问第二十个月的兔子对数为多少?
	public static void main(String args[]) {
		int arr[] = new int[20];
		arr[0] = 1;
		arr[1] = 1;
		for(int i = 2; i < arr.length; i++) {
			arr[i] = arr[i-2] + arr[i-1];
		}
		System.out.println("第二十个月为:" + arr[19]);
	}	
}

百钱买百鸡

for循环嵌套


public class Test {
	//案例:百钱买百鸡
	//需求:公鸡一只5文钱 母鸡一只3文钱 鸡仔3只一文钱
	public static void main(String args[]) {
		for (int i = 1; i < 21; i++) {
			for (int j = 1; j < 34; j++) {
				int z = 100 - i -j;
				if ( z % 3 == 0 && i * 5 + j * 3 + z / 3 == 100 ) {
					System.out.println("公鸡:" + i + "个," +"母鸡:" + j + "个," + "鸡仔" + z +"个");
				}
			}
		}
	}	
}

输出所有时间

for循环嵌套

public class Test {
	//案例:输出所有时间
	public static void main(String args[]) {
		for (int hours = 0; hours < 24; hours++) {
			for (int minutes = 0; minutes < 60; minutes++) {
				for (int seconds = 0; seconds < 60; seconds++) {
					System.out.println(hours + " 时  " + minutes + " 分 " + seconds + " 秒 ");
				}
			}
		}
	}
}

珠穆朗玛峰

while循环

public class Test {
	//案例:珠穆朗玛峰
	public static void main(String args[]) {
		//一张厚度为0.1cm足够大的纸,需要折叠多少次,才能达到珠穆朗玛峰高度8848860cm
		long zf = 8848860;
		double paper = 0.1;
		int count = 0;
		while ( paper < zf) {
			paper *= 2;
			count++;
		}
		System.out.println("折叠的次数为:" + count);
	}
}

求和2

do……while循环

public class Test {
	//案例:求和
	public static void main(String args[]) {
		int i = 5, sum = 0;
		do {
			i++;
			sum += i;
		} while (i < 10);
		System.out.println("和为:" + sum);
	}
}

猜数字

Random类使用

import java.util.Random;
import java.util.Scanner;

public class Test {
	//案例:猜数字
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int number = 0;
		
		Random r = new Random();
		int num = r.nextInt(100) + 1; //随机产生1~100之间的数
		
		while(true) {
			System.out.print("请输入一个数字:");
			number = sc.nextInt();
			if (number > num) {
				System.out.println("猜的数字大了!");
			}else if(number < num) {
				System.out.println("猜的数字小了!");
			}else if(num == number){
				System.out.println("恭喜你,猜对了!");
				break;
			}else{
				System.out.println("输入的数字不合法!!");//java.util.InputMismatchException   不是数字,就会出报错!
			}
		}
		sc.close();
	}
}

数组直接操作

Arrays类

import java.util.Arrays;

public class Test {
	public static void main(String[] args) {
		int array1[] = {11, 1, 23, 55, 123};
		System.out.println("原数组:" + Arrays.toString(array1));
		int array2[] = {1, 2, 23, 55, 123};
		int array3[] = {1, 11, 23, 55, 123};
		Arrays.sort(array1);
		System.out.println("排序后的数组为:" + Arrays.toString(array1));
		int index = Arrays.binarySearch(array1, 23);
		System.out.println("23元素所在位置:" + index);
//		System.out.println("相同/不相同:" + array1.equals(array2));
		System.out.println("相同/不相同:" + Arrays.equals(array1, array2));
		System.out.println("数组2:" + Arrays.toString(array3));
		System.out.println("相同/不相同:" + Arrays.equals(array1, array3));
		Arrays.fill(array3, 2, 4, 1);	//算头不算尾
		System.out.println("修改后的数组2:" + Arrays.toString(array3));
	}
}

比较最大值

数组操作1


public class Test {
	//案例:比较最大值
	public static void main(String args[]) {
		int array[] = {1,2,3,4,5,6,7,8,9,10};
		int max = array[0];
		
		for(int i = 1; i < array.length; i++) {
			if(max < array[i]) {
				max = array[i];
			}
		}
		System.out.println("最大值为:" + max);
	}	
}

获取最小值

数组操作2


public class Test {
	//案例:获取最小值
	public static void main(String args[]) {
		int array[] = {1,2,3,4,5,6,7,8,9,10};
		int min = array[0];
		
		for(int i = 1; i < array.length; i++) {
			if(min > array[i]) {
				min = array[i];
			}
		}
		System.out.println("最小值为:" + min);
	}	
}

数组内容相同

方法的使用


public class Test {
	
	public static boolean compare(int arr1[], int arr2[]) {
		boolean flag = true;
		if (arr1.length == arr2.length) {
			for (int i = 0; i < arr1.length; i++) {
				if (arr1[i] != arr2[i]) {
					flag = false;
					break;
				}
			}
			return flag;
		} else {
			return false;
		}
	}
	
	//案例:数组内容相同
	public static void main(String args[]) {
		int arr1[] = {11, 22, 33, 44, 55};
		int arr2[] = {11, 22, 33, 44, 55};
		boolean flag = compare(arr1, arr2);
		System.out.println("内容为:" + flag);
	}	
}

查找元素

方法使用

import java.util.Scanner;

public class Test {
	
	public static int select(int arr[], int number) {
		int index = -1;
		for (int i = 0; i < arr.length; i++) {
			if (number == arr[i]) {
				index = i;
				break;
			}
		}
		return index;
	}
	
	//案例:查找元素
	public static void main(String args[]) {
		int arr[] = {1, 22, 33, 444, 555};
		System.out.print("请输入你要查找的数:");
		Scanner sc = new Scanner(System.in);
		int number = sc.nextInt();
		int index = select(arr, number);
		System.out.println("元素在第" + index + "个位置");
		sc.close();
	}	
}

反转元素

方法的使用


public class Test {
	
	public static void reverse(int arr[]) {
		for (int start = 0, end = arr.length - 1; start <= end; start++, end--) {
			int temp = 0;
			temp = arr[start];
			arr[start] = arr[end];
			arr[end] = temp;
		}
	}
	
	public static void printArray(int arr[]) {
		System.out.print("[");
		for (int i = 0; i < arr.length; i++) {
			if (i == arr.length - 1) {
				System.out.print(arr[i]);
			} else {
				System.out.print(arr[i] + ",");
			}
		}
		System.out.println("]");
	}
	
	//案例:反转元素
	public static void main(String args[]) {
		int arr[] = {1, 2, 3, 4, 5, 6};
		reverse(arr);
		printArray(arr);
	}	
}

评委打分

方法的使用

import java.util.Scanner;

public class Test {
	public static int getMax(int arr[]) {
		int max = arr[0];
		for (int i=1; i < arr.length; i++) {
			if (max < arr[i]) {
				max = arr[i];
			}
		}
		return max;
	}
	
	public static int getMin(int arr[]) {
		int min = arr[0];
		for (int i = 1; i < arr.length; i++) {
			if (min > arr[i]) {
				min = arr[i];
			}
		}
		return min;
	}
	
	public static int getSum(int arr[]) {
		int sum = 0;
		for (int i = 0; i < arr.length; i++) {
			sum += arr[i];
		}
		return sum;
	}
	//案例:评委打分
	public static void main(String args[]) {
		int arr[] = new int[6];
		Scanner sc = new Scanner(System.in);
		for (int i=0; i < arr.length; i++) {
			System.out.print("请输入第" + (i+1) + "评委分数:");
			arr[i] = sc.nextInt();
		}
		int max = getMax(arr);
		int min = getMin(arr);
		int sum = getSum(arr);
		double average = (sum - max - min) / (arr.length - 2);
		System.out.println("平均分为:" + average);
		sc.close();
	}	
}

用户登录

字符串使用

import java.util.Scanner;

public class Test {
	private static String userName="user";
	private static String password="password";
	
	//案例:用户登录
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		for (int i = 1; i <= 3; i++) {
			System.out.print("请输入用户名:");
			String username = sc.nextLine();
			System.out.print("请输入密码:");
			String pwd = sc.nextLine();
			if (username.equals(userName) && pwd.equals(password)) {
				System.out.println("登录成功");
				break;
			} else {
				if (3-i == 0) {
					System.out.println("账号已冻结!!");
				} else {
					System.out.println("你还有" + (3-i) + "次机会");
				}
			}
		}
		sc.close();
	}
}

遍历字符串

字符串操作

import java.util.Scanner;

public class Test {

	//案例:遍历字符串
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入一个字符串:");
		String string1 = sc.nextLine();
		for (int i = 0; i < string1.length(); i++) {
			if (i != string1.length() - 1) {
				System.out.print(string1.charAt(i) + " ");
			} else {
				System.out.print(string1.charAt(i));
			}
		}
		sc.close();
	}
}

统计字符次数

字符串操作

import java.util.Scanner;

public class Test {

	//案例:统计字符串次数
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入一个字符串:");
		String string1 = sc.nextLine();
		int bigCount = 0,smallCount = 0,number = 0;
		boolean flag = true;
		for (int i = 0; i < string1.length(); i++) {
			int temp = string1.charAt(i);
			if (temp >= 'A' && temp <= 'Z') {
				bigCount++;
			} else if (temp >= 'a' && temp <= 'z'){
				smallCount++;
			} else if (temp >= '0' && temp <= '9') {
				number++;
			} else {
				flag = false;
				break;
			}
		}
		if (flag) {
			System.out.println("大写字母有:" + bigCount + "个," + "小写字母有:" + smallCount + "个," + "数字有:" + number + "个");
		} else {
			System.out.println("含中文字符!!");
		}
		sc.close();
	}
}

字符串的拼接1

字符串操作


public class Test {

	public static String arrayToString(int array[]) {
		String string = "";
		string += "[";
		for (int i = 0; i < array.length; i++) {
			if (i == array.length - 1) {
				string += array[i];
			} else {
				string += array[i] + ",";
			}
		}
		string += "]";
		return string;
	}
	
	//案例:字符串的拼接
	public static void main(String args[]) {
		int array[] = {1,2,3,4,5};
		String string = arrayToString(array);
		System.out.println(string);
	}
}

字符串的拼接2

StringBuilder类


public class Test {
	public static String arrayToString(int array[]) {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("[");
		for (int i = 0; i < array.length; i++) {
			if (i == array.length - 1) {
				stringBuilder.append(array[i]);
			} else {
				stringBuilder.append(array[i]).append(",");
			}
		}
		stringBuilder.append("]");
		return stringBuilder.toString();
	}
	//案例:字符串的拼接2
	public static void main(String args[]) {
		int array[] = {1, 2, 3, 4, 5};
		String string = arrayToString(array);
		System.out.println(string);
	}
}

字符串反转1

字符串操作

import java.util.Scanner;

public class Test {
	public static String reverse(String string) {
		String str = "";
		for (int i = string.length() - 1; i >= 0; i--) {
			str += string.charAt(i);
		}
		return str;
	}
	//案例:字符串反转
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入一个字符串:");
		String string = sc.nextLine();
		String str = reverse(string);
		System.out.println(str);
		sc.close();
	}
}

字符串反转2

StringBuilder类

import java.util.Scanner;

public class Test {
	public static String myReverse(String string) {
		return new StringBuilder(string).reverse().toString();
	}
	//案例:字符串反转
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入一个字符串:");
		String string = sc.nextLine();
		String str = myReverse(string);
		System.out.println(str);
		sc.close();
	}
}

猫和狗1

多态的使用


public class Test{
	/**
	 * 	多态的弊端,不能使用子类的特有功能
	 * 	案例:猫和狗 1 
	 */
	public class Animal{
		private String name;
		private int age;
		
		public Animal() {};
		public Animal(String name,int age) {
			this.name = name;
			this.age = age;
		}
		
		public String getName() {
			return name;
		}
		
		public int getAge() {
			return age;
		}
		
		public void setName(String name) {
			this.name = name;
		}
		
		public void setAge(int age) {
			this.age = age;
		}
		
		public void eat() {
			System.out.println("动物吃东西");
		}
		
		public void show() {
			System.out.println(getName() + " , " + getAge());
		}
	}
	
	
	public class Pig extends Animal{
		public Pig() {}
		public Pig(String name, int age) {
			super(name, age);
		}
		
		public void eat() {
			System.out.println("猪吃白菜");
		}
	}
	
	
	public class Dog extends Animal{
		public Dog() {}
		public Dog(String name, int age) {
			super(name, age);
		}
		
		public void eat() {
			System.out.println("狗吃骨头");
		}
	}
	
	
	public class Cat extends Animal{
		public Cat() {}
		
		public Cat(String name, int age) {
			super(name, age);
		}
		
		public void eat() {
			System.out.println("猫吃鱼");
		}
		
		public void play() {
			System.out.println("猫抓老鼠");
		}
	}
	
	/**
	 * No enclosing instance of type Test is accessible.
	 * 非静态的内部类,要先实例化才能使用。
	 * @param args
	 */
	
	public static void main(String args[]) {
		Test t = new Test();	//非静态的内部类,要先实例化才能使用。
		Animal a = t.new Cat(); // 向上转型
		a.setName("托马斯");
		a.setAge(23);
		a.eat();
		a.show();
		
//		Cat c = (Cat)a;		//向下转型
		a = t.new Cat("加菲猫", 11);
		a.eat();
		a.show();
//		a.play(); 	//父类中并没有play方法
		
		a = t.new Dog("二哈", 23);
		a.eat();
		a.show();
		
		a = t.new Pig("皮猪", 13);
		a.eat();
		a.show();
	}
	
}

猫和狗2

抽象类和抽象方法


public class Test{
	/**
	 * 	抽象类和抽象方法
	 * 	案例:猫和狗 2 
	 */
	public abstract class Animal{
		private String name;
		private int age;
		
		public Animal() {};
		
		public Animal(String name, int age) {
			this.name = name;
			this.age = age;
		}
		
		public String getName() {
			return name;
		}
		
		public int getAge() {
			return age;
		}
		
		public void setName(String name) {
			this.name = name;
		}
		
		public void setAge(int age) {
			this.age = age;
		}
		
		public abstract void eat(); 
		public void show() {
			System.out.println(getName() + ", " + getAge());
		}
	}
	
	public class Dog extends Animal {
		public Dog() {}
		
		public Dog(String name, int age) {
			super(name, age);
		}
		
		public void eat() {
			System.out.println("狗吃骨头");
		}
	}
	
	public class Cat extends Animal {
		public Cat() {}
		
		public Cat(String name, int age) {
			super(name, age);
		}
		
		public void eat() {
			System.out.println("猫吃鱼");
		}
		
	}
	
	public class Pig extends Animal {
		public Pig() {}
		
		public Pig(String name, int age) {
			super(name, age);
		}
		
		public void eat() {
			System.out.println("猪吃白菜");
		}
	}
	
	public static void main(String[] args) {
		Animal a = new Test().new Cat("加菲猫", 5);
		a.eat();
		a.show();
		
		a = new Test().new Dog("藏獒", 10);
		a.eat();
		a.show();
		
		a = new Test().new Pig("皮猪", 3);
		a.eat();
		a.show();
	}
}

猫和狗3

接口使用


public class Test{
	/**
	 * 	接口
	 *  接口成员特点:成员变量只能是常量,默认public static final,无构造方法,成员方法只能是抽象方法,默认修饰public abstract
	 * 	案例:猫和狗 3
	 */
	public interface Jumpping {
		public abstract void jump();
	}
	
	public abstract class Animal implements Jumpping {
		private String name;
		private int age;
		
		public Animal() {};
		
		public Animal(String name, int age) {
			this.name = name;
			this.age = age;
		}
		
		public String getName() {
			return name;
		}
		
		public int getAge() {
			return age;
		}
		
		public void setName(String name) {
			this.name = name;
		}
		
		public void setAge(int age) {
			this.age = age;
		}
		
		public abstract void eat(); 
		
		public void show() {
			System.out.println(getName() + ", " + getAge());
		}
	}
	
	public class Dog extends Animal {
		public Dog() {}
		
		public Dog(String name, int age) {
			super(name, age);
		}
		
		public void eat() {
			System.out.println("藏獒吃骨头");
		}
		
		public void jump() {
			System.out.println("藏獒跳远了");
		}
	}
	
	public class Cat extends Animal {
		public Cat() {}
		
		public Cat(String name, int age) {
			super(name, age);
		}
		
		public void eat() {
			System.out.println("猫吃鱼");
		}
		
		public void jump() {
			System.out.println("猫可以跳高");
		}
		
	}
	
	public class Pig extends Animal {
		public Pig() {}
		
		public Pig(String name, int age) {
			super(name, age);
		}
		
		public void eat() {
			System.out.println("猪吃白菜");
		}
		
		public void jump() {
			System.out.println("猪跳高了");
		}
	}
	
	public static void main(String[] args) {
		Animal a = new Test().new Cat("加菲猫", 5);	//a为一个对象句柄。
		a.eat();
		a.show();
		a.jump();
		
		
		a = new Test().new Dog("藏獒", 10);
		a.eat();
		a.show();
		a.jump();
		
		a = new Test().new Pig("皮猪", 3);
		a.eat();
		a.show();
		a.jump();
	}
}

你可能感兴趣的:(Java,java,eclipse)