1.模拟一个微信抢红包的算法:
package study;
import java.util.Random;
import java.util.Scanner;
public class RedBags {
public static void main(String[] args){
System.out.println("====微信抢红包模拟器!!====");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入红包的金额:");
double money = scanner.nextDouble();
System.out.println("请输入红包个数:");
int num = scanner.nextInt();
double minmoney = 0.01;
Random random = new Random();
if (money / num == 0.01){
for (int i = 1; i <= num; i++){
System.out.printf("第%d个红包的金额是%.2f%n", i, minmoney);
}
}else if (money / num < 0.01){
System.out.println("不好意思红包最小金额必须是0.01,您的分配比例有问题!!");
}else {
for (int j = 1; j < num; j++){
double maxmoney = money - (num - j) * 0.01;
double qj = maxmoney - minmoney;
double save = (double) random.nextInt((int)(qj * 100))/100;
double zhmoney = save + minmoney;
money = money - zhmoney;
System.out.printf("第%d个红包的金额是%.2f%n", j, zhmoney);
}
}
System.out.printf("第%d个红包的金额是%.2f%n", num, money);
}
}
2.模拟一个彩票随机号:
package practise;
import java.util.Random;
public class Test6 {
public static void main(String[] args){
Random random = new Random();
int[] nums = new int[7];
for (int i = 0; i < 5; i++){
int num = random.nextInt(35);
nums[i] = num;
for (int j = 0; j < i; j++){
if (nums[i] == nums[j]){
i--;
break;
}
}
}
for (int i = 5; i < 7; i++){
int num = random.nextInt(10);
nums[i] = num;
if (nums[5] == nums[6]){
i--;
}
}
for (int i = 0; i <= nums.length - 1; i++){
System.out.print(nums[i] + ",");
}
}
}
3.java一个简单的控制台程序
package 图形计算器;
import java.util.Scanner;
public class Count {
public static boolean kz = true;
public static void main(String[] args){
while(kz) {
System.out.println("======图形计算器======");
System.out.println("====a-三角形");
System.out.println("====b-矩形");
System.out.println("====c-圆形");
System.out.println("====d-任意四边形");
System.out.println("====e退出");
System.out.println("请输入你要选择的图形的编号");
Scanner scanner = new Scanner(System.in);
String st = scanner.next();
select(st);
}
}
public static void select(String st){
switch (st){
case "a":
Triangle triangle = new Triangle();
triangle.area();
triangle.perimeter();
break;
case "b":
Rectangle rectangle = new Rectangle();
rectangle.area();
rectangle.perimeter();
break;
case "c":
Round round = new Round();
round.area();
round.perimeter();
break;
case "d":
System.out.println("还在建设");
break;
case "e":
kz = false;
System.out.println("欢迎下次在来");
break;
}
}
}
class Triangle{
private final double a;
private final double b;
private final double c;
public Triangle(){
Scanner scanner = new Scanner(System.in);
System.out.print("请输入三角形边长a:");
double a = scanner.nextDouble();
System.out.print("请输入三角形边长b:");
double b = scanner.nextDouble();
System.out.print("请输入三角形边长c:");
double c = scanner.nextDouble();
this.a = a;
this.b = b;
this.c = c;
Verifier(a, b, c);
}
private static void Verifier(double a, double b, double c){ // 验证器
if (a + b < c || a + c < b || b + c < a || a <= 0 || b <= 0 || c <= 0){
System.out.println("请重新输入,你输入的根本不是三角形!!!请检查!!");
new Triangle();
}
}
void area(){
double s = (a + b + c)/2;
double mj = Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.printf("三角形的面积是: %.2f %n", mj);
}
void perimeter(){
double l = a + b + c;
System.out.printf("三角形的面积是: %.2f%n", l);
}
}
class Rectangle{
private final double a;
private final double b;
public Rectangle(){
Scanner scanner = new Scanner(System.in);
System.out.print("请输入矩形边长a:");
double a = scanner.nextDouble();
System.out.print("请输入矩形边长b:");
double b = scanner.nextDouble();
this.a = a;
this.b = b;
Verifier(a, b);
}
private static void Verifier(double a, double b){
if ( a <= 0 || b <= 0){
System.out.println("请输入一个正常的矩形边长必须大于0");
new Rectangle();
}
}
void area(){
double mj = a * b;
System.out.printf("矩形的面积是: %.2f %n", mj);
}
void perimeter(){
double l = (a + b) * 2;
System.out.printf("矩形的周长是: %.2f %n", l);
}
}
class Round{
private final double r;
public Round(){
System.out.print("请输入圆的半径");
Scanner scanner = new Scanner(System.in);
double r = scanner.nextDouble();
this.r = r;
Verifier(r);
}
private static void Verifier(double r){
if (r <= 0){
System.out.println("请输入一个正常的圆,半径不能低于0");
new Round();
}
}
void area(){
double mj = Math.PI * r * r;
System.out.printf("圆的面积是: %.2f %n", mj);
}
void perimeter(){
double l = Math.PI * 2 * r;
System.out.printf("圆的周长是; %.2f %n", l);
}
}
基于java读写操作写的一个用户信息管理系统代码如下:
package 身份信息入库系统;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class Test {
public static boolean kz = true;
public static void main(String[] args) throws IOException {
while (kz){
System.out.println("======身份信息入库系统======");
System.out.println("1-注册");
System.out.println("2-登入");
System.out.println("3-打印所有信息");
System.out.println("4-退出");
System.out.println("请输入你要选择的编号");
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
select(num);
}
}
public static void select(int num) throws IOException {
switch (num){
case 1:
Post post = new Post();
post.write();
break;
case 2:
System.out.println("请输入您的姓名");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
System.out.println("请输入您的密码");
int password = scanner.nextInt();
Login login = new Login(name, password);
break;
case 3:
Show show = new Show();
break;
case 4:
System.out.println("欢迎下次再来!");
kz = false;
break;
}
}
}
interface Validtor{
boolean validtor(String name, int password) throws IOException;
}
class ReadWrite{
ReadWrite() throws IOException {
}
static File file() throws IOException {
File file = new File("src/身份信息入库系统/test.txt");
if (!file.exists()){
file.createNewFile();
}
return file;
}
FileReader f = new FileReader(file());
BufferedReader fr = new BufferedReader(f);
public void write(String s) throws IOException {
FileWriter fw = new FileWriter(file(), true);
fw.write(s);
fw.flush();
fw.close();
}
public BufferedReader read() throws IOException {
return fr;
}
}
class Login implements Validtor{
private String name;
private int password;
public Login(String name, int password) throws IOException {
this.name = name;
this.password = password;
if (validtor(name, password)){
System.out.println("该用户不存在!请重新输入!");
}else {
System.out.println("登入成功!!");
}
}
@Override
public boolean validtor(String name, int password) throws IOException {
ReadWrite readWrite = new ReadWrite();
String query = null;
while ((query = readWrite.read().readLine()) != null ){
String[] query2 = query.split("-", 3);
if (query2[0].equals(name)){
return false;
}
}
return true;
}
}
class Post implements Validtor{
private final String name;
private final int password;
private final double hight;
private final double weight;
private final String aboutme;
public Post(){
System.out.println("请输入您的姓名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
System.out.println("请输入你的密码 :");
int password = scanner.nextInt();
System.out.println("请输入你的身高 :");
double hight = scanner.nextDouble();
System.out.println("请输入你的体重 :");
double weight = scanner.nextDouble();
System.out.println("请输入你的个人介绍 :");
String aboutme = scanner.next();
this.name = name;
this.password = password;
this.hight = hight;
this.weight = weight;
this.aboutme = aboutme;
}
void write() throws IOException {
if (validtor(name, 1)){
ReadWrite readWrite = new ReadWrite();
String s = name + "-" + password + "-" + hight + "-" + weight + "-" + aboutme + "\r\n";
readWrite.write(s);
}else {
System.out.println("该用户已经存在!!!");
new Post();
}
}
@Override
public boolean validtor(String name, int password) throws IOException {
ReadWrite readWrite = new ReadWrite();
String query = null;
while ((query = readWrite.read().readLine()) != null ){
String[] query2 = query.split("-", 2);
if (query2[0].equals(name)){
return false;
}
}
return true;
}
}
class Show{
public Show() throws IOException {
ReadWrite readWrite = new ReadWrite();
System.out.println("------------------------------");
String query;
while ((query = readWrite.read().readLine()) != null ){
System.out.println(query);
}
}
}
JDBC操作:
package JDBC;
import java.sql.*;
public class Gradation {
public Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/java?serverTimezone=UTC&useSSL=false", "root", "196811");
System.out.println("数据库连接成功");
return con;
}
public static void main(String[] args) throws SQLException, ClassNotFoundException {
Gradation conn = new Gradation();
Connection con =null;
Statement st = null;
ResultSet rst = null;
try{
con = conn.getConnection();
st = con.createStatement();
rst = st.executeQuery("select * from test");
while (rst.next()){
String id = rst.getString("id");
String name = rst.getString("name");
String sex = rst.getString("sex");
String age = rst.getString("age");
System.out.println(id + "-" + name + "-" + sex + "-" + age);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (rst != null){
rst.close();
}
if (st != null){
st.close();
}
if (con != null){
con.close();
}
}
}
}