2024/9/27
登录系统(验证码):
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Registration {
public static void main(String[] args) {
ArrayList
Scanner sc = new Scanner(System.in);
int choice;
while (true) {
System.out.println("1.注册");
System.out.println("2.登录:");
choice = sc.nextInt();
switch (choice) {
case 1:
zhuce(list);
break;
case 2:
denglu(list);
break;
}
}
}
public static void zhuce(ArrayList<User> list) {
String username, password;
User user = new User();
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的用户名:");
username = sc.nextLine();
user.setUsername(username);
System.out.println("请输入您的密码:");
password = sc.nextLine();
user.setPassword(password);
list.add(new User(username, password));
}
public static void denglu(ArrayList<User> list) {
User user = list.get(0);
String username, password;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("用户名:");
username = sc.nextLine();
System.out.println("密码:");
password = sc.nextLine();
if (username.equals(user.getUsername()) && password.equals(user.getPassword())) {
Random r = new Random();
String code = "";
for (int i = 1; i <= 6; i++) {
int type = r.nextInt(3);
switch (type) {
case 0:
code += r.nextInt(10);
break;
case 1:
char ch1 = (char) ('a' + r.nextInt(26));
code += ch1;
break;
case 2:
char ch2 = (char) ('A' + r.nextInt(26));
code += ch2;
break;
}
}
System.out.println("密码正确,请等待验证码并输入");
System.out.println("验证码为:" + code);
System.out.println("请输入验证码:");
String yanzheng = sc.nextLine();
if (yanzheng.equals(code)) {
System.out.println("登录成功!");
break;
}
} else System.out.println("用户名或密码错误,请重新输入:");
}
}
}
public class User {
String username;
String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
四则运算:
include
include
include
include
include
using namespace std;
int main(){
for(int i=0;i<30;i++){
int a=rand()%100;
int b=rand()%100;
int c=rand()%4;
switch(c){
case 0:
cout<<a<<"+"<<b<<"="<<endl;
break;
case 1:
cout<<a<<"-"<<b<<"="<<endl;
break;
case 2:
cout<<a<<"*"<<b<<"="<<endl;
break;
case 3:
cout<<a<<"/"<<b<<"="<<endl;
break;
}
}
return 0;
}