跳过部分基础,写三个小练习
This commit is contained in:
29
src/class12/test1.java
Normal file
29
src/class12/test1.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package class12;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class test1 {
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("请输入第一个数字:");
|
||||
double num1 = sc.nextDouble();
|
||||
System.out.println("请输入第二个数字:");
|
||||
double num2 = sc.nextDouble();
|
||||
System.out.println("请输入运算符:(+,-,*,/)");
|
||||
String str = sc.next();
|
||||
double result = getResult(num1,num2,str);
|
||||
System.out.println("结果是:"+result);
|
||||
}
|
||||
public static double getResult(double num1,double num2,String str){
|
||||
return switch (str) {
|
||||
case "+" -> num1 + num2;
|
||||
case "-" -> num1 - num2;
|
||||
case "*" -> num1 * num2;
|
||||
case "/" -> num1 / num2;
|
||||
default -> {
|
||||
System.out.println("输入的运算符有误");
|
||||
yield 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
31
src/class12/test2.java
Normal file
31
src/class12/test2.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package class12;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class test2 {
|
||||
public static void main(String[] args) {
|
||||
//guess
|
||||
int a = (int)(Math.random()*100);
|
||||
Scanner sc = new Scanner(System.in);
|
||||
while(true){
|
||||
int b = sc.nextInt();
|
||||
if(big_or_small(a,b)){
|
||||
break;
|
||||
}
|
||||
}
|
||||
System.out.println("猜对了!");
|
||||
|
||||
}
|
||||
public static boolean big_or_small(int a,int b){
|
||||
// 错误返0,正确返回1
|
||||
if(b>a){
|
||||
System.out.println("猜大了");
|
||||
return false;
|
||||
}else if(b<a){
|
||||
System.out.println("猜小了");
|
||||
return false;
|
||||
}else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
39
src/class12/test3.java
Normal file
39
src/class12/test3.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package class12;
|
||||
|
||||
public class test3 {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("验证码已生成:");
|
||||
System.out.println(getCode(4));
|
||||
System.out.println(getCode(6));
|
||||
System.out.println(getCode2(4));
|
||||
System.out.println(getCode2(6));
|
||||
|
||||
}
|
||||
public static String getCode(int len){
|
||||
StringBuilder code= new StringBuilder();
|
||||
for (int i = 0; i < len; i++) {
|
||||
int type = (int)(Math.random()*3);
|
||||
switch (type) {
|
||||
case 0:
|
||||
code.append((char) (Math.random() * 26 + 'a'));
|
||||
break;
|
||||
case 1:
|
||||
code.append((char) (Math.random() * 26 + 'A'));
|
||||
break;
|
||||
case 2:
|
||||
code.append((int) (Math.random() * 10));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return code.toString();
|
||||
}
|
||||
public static String getCode2(int len) {
|
||||
String dict = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
StringBuilder code = new StringBuilder();
|
||||
for (int i = 0; i < len; i++) {
|
||||
int index = (int)(Math.random() * dict.length());
|
||||
code.append(dict.charAt(index));
|
||||
}
|
||||
return code.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user