Java基础(一)

27 阅读4分钟

 一、简单计算器实现

1.需求

设计一个可以进行加减乘除运算的计算器。用户输入两个数字和一个运算符,根据用户输入显示运算结果。

2.知识点

语法—输入输出、switch分支结构。

(1)语法输入输出

// 输入
// 1.导入Scanner类
import java.util.Scanner
// 2.创建Scanner对象
Scanner sc= new Scanner(System.in)
// 3.接收各种类型输入
sc.nextInt(); //接收一个int整数
sc.nextLong(); //接收一个long型整数
sc.nextFloat(); //接收float型小数
sc.nextDouble(); //接收double型小数
sc.next(); //接收读取不带空格的字符串
sc.nextLine(); //接收读取整段字符串

//输出
System.out.println(); //将括号内容转换成字符串输出到控制台,并自动换行

(2)switch分支结构

switch(expression){
    case x:
        //代码块
        break;
    case y:
        //代码块
        break;
    default:
        //代码块
}

3.具体代码实现

import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输如两个整数:");
        System.out.println("整数1:");
        double num1 = sc.nextDouble();

        System.out.println("整数2:");
        double num2 = sc.nextDouble();

        System.out.println("请输入运算符(+,-,*,/):");
        String operator = sc.next();

        double result = getResult(num1,num2,operator);
        System.out.println(num1 + operator + num2 + "=" + result);
    }
    public static double getResult(double num1,double num2,String operator){
        double result = 0;
        switch (operator){
            case "+":
                result = num1 + num2;
                break;
            case "-":
                result = num1 - num2;
                break;
            case "*":
                result = num1 * num2;
                break;
            case "/":
                result = num1 / num2;
                break;
            default:
                System.out.println("输入的运算符有误!");
        }
        return result;
    }
}

二、猜数字游戏

1.需求

随机生成一个1-100之间的数据,提示用户猜测,猜大提示过大,猜大提示过小,直到猜中结束游戏。

2.知识点

if分支结构、while循环、随机数生成。

(1)if分支结构

if(条件表达式1){
    代码块1;
}
else if(条件表达式2){
    代码块2;
}
else(条件表达式3){
    代码块3;
}

(2)while循环

while(循环条件){
    循环操作;
}

(3)随机数生成

// 1.Random类
import java.util.Random;
// 实例化Random对象
Random random = new Random();
int randomInt = random.nextInt();// 生成任意范围内的整数
int rangedInt = random.nextInt(10); // 0到9的整数
double randomDouble = random.nextDouble(); 0.01.0的浮点数

// 2.使用Math.random()
该方法返回0.0-1.0之间的双精度浮点数

3.具体代码实现

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

public class Test2 {
    public static void main(String[] args) {
        guess();
    }
    public static void guess(){
        // 1.生成随机数:1-100
        // 方式一:
        // Math.Random()返回[0,1)之间的随机整数
        // (int)Math.random()*100 ==> [0,100)的整数 ==> [0,99]+1 ==> [1,100]
        // int number = (int)(Math.random()*100)+1;

        // 方式二:
        Random r = new Random();
        int LuckNumber = r.nextInt(100)+1;

        Scanner sc = new Scanner(System.in);
        // 2.定义一个死循环让用户一直猜,直到猜中
        while (true) {
            System.out.println("请输入你要猜的数字:");
            int number = sc.nextInt();
            if (number == LuckNumber) {
                System.out.println("恭喜你猜对了!");
                break;
            } else if (number > LuckNumber) {
                System.out.println("你猜的数字太大了!");
            } else {
                System.out.println("你猜的数字太小了!");
            }
        }
    }
}

三、验证码

1.需求

开发一个程序,可以生成指定位数的验证码,可以是数字、大小写字母。

2.知识点

for循环、switch分支结构。

(1)for循环

for(初始化语句;循环条件;迭代语句){
    循环操作;
}

// 循环控制
continue:终止当前循环迭代,直接进入下一次循环
break:立即终止整个循环的执行

3.具体代码实现

public class Test3 {
    public static void main(String[] args) {
        System.out.println(getCode(5));
    }
    public static String getCode(int n){
        String code = "";
        for(int i = 0; i < n; i++){
            int type = (int)(Math.random() * 3);
            switch (type){
                case 0:
                    int num = (int)(Math.random() * 10);
                    code += num;
                    break;
                case 1:
                    // 'A' 65  'Z' 65+25
                    char ch1 = (char)(Math.random() * 26 + 'A');
                    code += ch1;
                    break;
                case 2:
                    // 'a' 97  'z' 97+25
                    char ch2 = (char)(Math.random() * 26 + 'a');
                    code += ch2;
                    break;
            }
        }
        return code;
    }
}

四、找素数

1.需求

判断101-200之间有多少个素数,并输出所有素数。说明:除了1和它本身外,不能被其他正整数整除,就叫素数。

2.知识点

素数理解、for循环、if分支结构。

3.具体代码实现

public class Test4 {
    public static void main(String[] args) {
        int count = 0
        for (int i = 101; i<=200; i++){
            if(isPrime(i)){
                System.out.println(i);
                count++;
            }
        }
        System.out.println("素数的个数:" + count);
    }
    public static boolean isPrime(int num) {
        for (int i = 2; i < Math.sqrt(num); i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }
}