前五节上机实验

1.
public class Test01 {
    public static void main(String[] args) {
        double i=1.0;
        double m=i/2.0+i/3.0+i/5.0;
        System.out.println(m);
    }
}

import java.util.Scanner;
public class Test01 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int m=sc.nextInt();
        int n= sc.nextInt();
        int temp=m;
        m=n;
        n=temp;
        System.out.println("m="+m+" n="+n);
    }
}

import java.util.Scanner;
import java.lang.Math;
public class Test01 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        double r=sc.nextDouble();
        double ballV=(4.0/3.0)*Math.PI*Math.pow(r,3);
        System.out.printf("球的体积是:%.2f", ballV);
    }
}

import java.util.Scanner;
import java.lang.Math;
public class Test01 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        double m=sc.nextDouble();
        double c=(5.0/9.0)*(m-32);
//        System.out.println("摄氏温度为: "+c);
        System.out.printf("摄氏温度为:%.2f",c);
    }
}

import java.util.Scanner;
import java.lang.Math;
public class Test01 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int m=sc.nextInt();
        int n=sc.nextInt();
        int c=0;
        c=m+n;
        System.out.println("这两个整数的和为"+c);
        c=m-n;
        System.out.println("这两个整数的差为"+c);
        c=m*n;
        System.out.println("这两个整数的积为"+c);
        c=m/n;
        System.out.println("这两个整数的商为"+c);
    }
}

import java.util.Scanner;
public class Test01 {
    public static void main(String[] args) {
        System.out.print("Enter a dollar amount:");
        Scanner sc=new Scanner(System.in);
        int dollar=sc.nextInt();
        int count01=0; int count02=0; int count03=0; int count04=0;
        while(dollar>20){
            count01++;
            dollar-= 20;
        }
        while(dollar>10){
            count02++;
            dollar-= 10;
        }
        while(dollar>5){
            count03++;
            dollar -= 5;
        }
        if(dollar>0) {
            count04 += dollar;
        }
        System.out.println("$20 bills:"+count01);
        System.out.println("$10 bills:"+count02);
        System.out.println("$5 bills:"+count03);
        System.out.println("$1 bills:"+count04);
    }
}

import java.util.Scanner;
public class Test01 {
    public static void main(String[] args) {
        System.out.print("Enter total seconds:");
        Scanner sc=new Scanner(System.in);
        int totalSeconds = sc.nextInt();
        int hours = totalSeconds / 3600;
        int remainingSeconds = totalSeconds % 3600;
        int minutes = remainingSeconds / 60;
        int seconds = remainingSeconds % 60;
        System.out.printf("Time:%d:%d:%d\n", hours, minutes, seconds);
    }
}


import java.util.Scanner;
public class Test01 {
    public static void main(String[] args) {
        System.out.print("Enter item number:");
        Scanner sc=new Scanner(System.in);
        int itemNumber = sc.nextInt();
        System.out.print("Enter unit price:");
        double unitPrice = sc.nextDouble();
        System.out.print("Enter sales profit:");
        double salesProfit = sc.nextDouble();
        System.out.print("Enter purchase date (yyyy mm dd): ");
        int year = sc.nextInt();
        int month = sc.nextInt();
        int day = sc.nextInt();
        System.out.println("Item\t"+"Unit Price\t"+"Sales profit\t"+"Purchase date\t");
        System.out.printf("%d\t\t$%.1f\t\t%.1f",itemNumber,unitPrice,salesProfit);
        System.out.print("%\t\t");
        System.out.printf("\t%d/%d/%d",month,day,year);
    }
}

import java.util.Scanner;
public class Test01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("第一个学生的成绩为:");
        int m = sc.nextInt();
        System.out.print("第二个学生的成绩为");
        int n = sc.nextInt();
        int highernum = 0;
        highernum = m > n ? m : n;
        System.out.println("更高的成绩为:" + highernum);
        }
    }


import java.util.Scanner;
public class Test01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number:");
        double num=sc.nextDouble();
        if(num%3==0&&num%7!=0){
            System.out.println("Print:YES");
        }else{
            System.out.println("Print:NO");
        }
    }
}


import java.util.Scanner;
public class Test01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入小孩的年龄:");
        int age=sc.nextInt();
        switch (age) {
            case 2:
            case 3:
                System.out.println("应编入小班(small class)");
                break;
            case 4:
                System.out.println("应编入中班(middle class)");
                break;
            case 5:
            case 6:
                System.out.println("应编入大班(top class)");
                break;
            default:
                System.out.println("年龄不符合幼儿园的招生范围");
                break;
        }
    }
}

import java.util.Scanner;
public class Test01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number:");
        char character=sc.next().charAt(0);
        if(character>='a'&&character<='z'||character>='A'&&character<='Z'){
            System.out.println("Print:YES");
        }else{
            System.out.println("Print:NO");
        }
    }
}

import java.util.Scanner;
public class Test01 {
    public static void main(String[] args) {
        int num = 123; // 设定的数字
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入你猜的数字:");
        int guessNum = scanner.nextInt();
        if (guessNum == num) {
            System.out.println("Right");
        } else if (guessNum > num) {
            System.out.println("Wrong, Too high");
        } else {
            System.out.println("Wrong, Too low");
        }
    }
}

import java.util.Scanner;
public class Test01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number:");
        int number = scanner.nextInt();
        int digits=0; int tempNumber=number;
        while(tempNumber>0){
            digits++;
            tempNumber/=10;
        }
        System.out.printf("The number %d has %d digits",number,digits);
    }
}


public class Test01 {
    public static void main(String[] args) {
        int addNum=0;
        for(int i=1;i<=200;i++){
            if(i%2!=0){
                addNum+=i;
            }
        }
        System.out.println("和="+addNum);
    }
}

public class Test01 {
    public static void main(String[] args) {
       for(int i=100;i<200;i++){
           if(i%3==0&&i%7!=0){
               System.out.print(i+" ");
           }
       }
    }
}

import java.util.Scanner;
public class Test01 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入n的值:");
        int n = sc.nextInt();
        long factorialAdd= 0;
        long factorial= 1;
        for (int i = 1; i <= n; i++) {
            factorial *= i; // 计算i的阶乘
            factorialAdd += factorial; // 将阶乘累加到总和中
        }
        System.out.println("1!+2!+3!+...+" + n + "! 的和是:" + factorialAdd);
    }
}

public class Test01 {
    public static void main(String[] args) {
        System.out.println("水仙花数为:");
        for (int i = 100; i <= 999; i++) {
            if (fun(i)) {
                System.out.println(i);
            }
        }
    }
    public static boolean fun(int number) {
        int Number = number;
        int sum = 0;
        while (number > 0) {
            int digit = number % 10;
            sum += Math.pow(digit, 3);
            number /= 10;
        }
        return sum == Number;
    }
}

import java.util.Scanner;
public class Test01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number:");
        int number = scanner.nextInt();
        int reverseNumber = 0; int count=0;
        while (number != 0) {
            int digit = number % 10;
            reverseNumber = reverseNumber * 10 + digit;
            number /= 10;
            count++;
        }
        System.out.println("The reversal number:" + reverseNumber);
        System.out.println("The number has " + count + " digits.");
//        System.out.println("The number has " + Integer.toString(reverseNumber).length() + " digits.");
        //先转换成字符串数组
    }
}


import java.util.Scanner;
public class Test01 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        double numberMax=0.0;
        int count=1;
        while(true){
            System.out.printf("Enter "+ count+ " number:");
            double number = sc.nextDouble();
            if (number<=0) {
                break;
            }
            if (number>numberMax) {
                numberMax=number;
            }
            count++;
        }
        System.out.println("The largest number  is "+numberMax);
    }
}


import java.util.Scanner;

public class Test01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入n的值:");
        int n = scanner.nextInt();
        double num = 0.0;
        for (int i = 0; i < n; i++) {
            num += 1.0 / factorial(i);
        }
        System.out.printf("e的近似值是:%.10f\n", num);
    }
    public static long factorial(int number) {
        long result = 1;
        for (int i = 1; i <= number; i++) {
            result *= i;
        }
        return result;
    }
}

public class Test01 {
    public static void main(String[] args) {
                System.out.println("2到1000之间的完数有:");
                for (int number = 2; number <= 1000; number++) {
                    if (isPerfect(number)) {
                        System.out.println(number);
                    }
                }
            }
            public static boolean isPerfect(int number) {
                int sum = 0;
                for (int i = 1; i < number; i++) {
                    if (number % i == 0) {
                        sum += i;
                    }
                }
                return sum == number;
            }
}



public class Test01 {
    public static void main(String[] args) {
                System.out.println("1到200之间的素数有:");
                for (int number = 2; number <= 200; number++) {
                    if (isPrime(number)) {
                        System.out.println(number);
                    }
                }
            }public static boolean isPrime(int number) {
                if (number <= 1) {
                    return false;
                }
                for (int i = 2; i <= Math.sqrt(number); i++) {
                    if (number % i == 0) {
                        return false;
                    }
                }
                return true;
            }
}


public class Test01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入第一个非零正整数:");
        int num1 = scanner.nextInt();
        System.out.print("请输入第二个非零正整数:");
        int num2 = scanner.nextInt();
        int gcd = fun(num1, num2);
        int lcm = (num1 * num2) / gcd;
        System.out.println("最大公约数是:" + gcd);
        System.out.println("最小公倍数是:" + lcm);
    }
    public static int fun(int a, int b) {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }
}


public class Test01{
    public static void main(String[] args) {
        int sum = 1;
        int n = 1;
        while (sum <= 9999) {
            n++;
            sum += factorial(n);
        }
        n--;
        System.out.println("满足条件的最大整数n是:" + n);
    }
    public static long factorial(int number) {
        long result = 1;
        for (int i = 1; i <= number; i++) {
            result *= i;
        }
        return result;
    }
}


import java.util.Scanner;

public class Test01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数组的长度:");
        int length = scanner.nextInt();
        int[] array = new int[length];
        System.out.println("请输入数组元素:");
        for (int i = 0; i < length; i++) {
            array[i] = scanner.nextInt();
        }
        int maxIndex = 0;
        int maxValue = array[0];
        for (int i = 1; i < length; i++) {
            if (array[i] > maxValue) {
                maxValue = array[i];
                maxIndex = i;
            }
        }
        System.out.println("最大元素的下标为:" + maxIndex);
        System.out.println("最大元素的值为:" + maxValue);
    }
}

import java.util.Scanner;

public class Test01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数组的长度:");
        int length = scanner.nextInt();
        int[] array = new int[length]; int addNum=0;
        System.out.println("请输入数组元素:");
        for (int i = 0; i < length; i++) {
            array[i] = scanner.nextInt();
            addNum+=array[i];
        }
        double avgNum=addNum/length;
        System.out.println(avgNum);
        for (int i = 0; i < length; i++) {
            if(array[i]>avgNum){
                System.out.println(array[i]);
            }
        }
    }
}


import java.util.Scanner;

public class Test01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] array = new int[10];
        System.out.println("请输入10个整数:");
        for (int i = 0; i < array.length; i++) {
            array[i] = scanner.nextInt();
        }
        for (int i = 0; i < array.length / 2; i++) {
            int temp = array[i];
            array[i] = array[array.length - 1 - i];
            array[array.length - 1 - i] = temp;
        }
        System.out.println("逆置后的数组为:");
        for(int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        scanner.close();
    }
}

import java.util.Scanner;

public class Test01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] array = new int[10];
        System.out.println("请输入10个整数:");
        for (int i = 0; i < array.length; i++) {
            array[i] = scanner.nextInt();
        }
        int low=0; int high=array.length-1;
        while(low<high){
            int temp=array[low];
            array[low]=array[high];
            array[high]=temp;
            low++;
            high--;
        }
        System.out.println("逆置后的数组为:");
        for(int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        scanner.close();
    }
}


import java.util.Scanner;

public class Test01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数组的长度:");
        int length = scanner.nextInt();
        int[] array = new int[length + 1];
        System.out.println("请输入有序数组的元素:");
        for (int i = 0; i < length; i++) {
            array[i] = scanner.nextInt();
        }
        System.out.println("请输入要插入的新数值:");
        int newNum = scanner.nextInt();
        int insertIndex = 0;
        while (insertIndex < length && array[insertIndex] < newNum) {
            insertIndex++;
        }
        for (int i = length; i > insertIndex; i--) {
            array[i] = array[i - 1];
        }
        array[insertIndex] = newNum;
        length++;
        System.out.println("插入新元素后的有序数组为:");
        for (int i = 0; i < length; i++) {
            System.out.print(array[i] + " ");
        }
    }
}


import java.util.Scanner;

public class Test01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[][] array=new int[3][4]; int min=Integer.MIN_VALUE;
        for(int i=0;i<array.length;i++){
            for(int j=0;j<array[0].length;j++){
                array[i][j]=scanner.nextInt();
                min=Math.max(array[i][j],min);
            }
        }
        System.out.println("二维数组的最大值为"+min);
    }
}


import java.util.Scanner;
import java.util.Random;
public class Test01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要生成的整数个数n:");
        int n = scanner.nextInt();
        int[] array = new int[n];
        Random random = new Random(); // 创建Random对象用于生成随机数
        // 生成随机整数并存储在数组中
        System.out.println("生成的随机整数数组为:");
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(100); // 生成[0, 100)内的随机整数
            System.out.print(array[i] + " ");
        }
        bubbleSort(array);
        // 输出排序后的数组
        System.out.println("排序后的数组为:");
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }
    public static void bubbleSort(int[] array){
        for(int i=0;i<array.length-1;i++){
            for(int j=0;j< array.length-1-i;j++){
                if(array[j]>array[j+1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }

}


选择排序
import java.util.Random;
import java.util.Scanner;

public class SelectionSort {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要生成的整数个数n:");
        int n = scanner.nextInt();
        int[] array = new int[n]; // 创建一个长度为n的数组

        Random random = new Random(); // 创建Random对象用于生成随机数

        // 生成随机整数并存储在数组中
        System.out.println("生成的随机整数数组为:");
        for (int i = 0; i < array.length; i++) {
            array[i] = 10 + random.nextInt(20); // 生成[10, 30)内的随机整数
            System.out.print(array[i] + " ");
        }
        System.out.println(); // 换行

        // 使用选择排序法对数组进行排序
        selectionSort(array);

        // 输出排序后的数组
        System.out.println("排序后的数组为:");
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        
        scanner.close(); // 关闭scanner
    }

    // 选择排序法
    public static void selectionSort(int[] array) {
        int n = array.length;
        for (int i = 0; i < n - 1; i++) {
            int minIndex = i; // 假设当前位置是最小值
            for (int j = i + 1; j < n; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j; // 更新最小值的位置
                }
            }
            // 交换当前位置和最小值的位置
            if (minIndex != i) {
                int temp = array[i];
                array[i] = array[minIndex];
                array[minIndex] = temp;
            }
        }
    }
}

8.
import java.util.Scanner;

public class Test01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[][] matrix = new int[3][3];
        int[] rowSums = new int[3];
        int[] colSums = new int[3];
        for (int i = 0; i < matrix.length; i++) {
            System.out.println("Enter row " + (i + 1) + ":");
            for (int j = 0; j < matrix[i].length; j++) {
                matrix[i][j] = scanner.nextInt();
                rowSums[i] += matrix[i][j];
                colSums[j] += matrix[i][j];
            }
        }
        System.out.print("Row totals:");
        for (int i = 0; i < rowSums.length; i++) {
            System.out.print(rowSums[i] + " ");
        }
        System.out.println();
        System.out.print("Column totals:");
        for (int j = 0; j < colSums.length; j++) {
            System.out.print(colSums[j] + " ");
        }
        System.out.println();
        scanner.close(); // 关闭scanner
    }
}


9.
import java.util.Scanner;

public class Test02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入杨辉三角的行数:");
        int numRows = scanner.nextInt();
        int[][] triangle = new int[numRows][];
        // 构建杨辉三角
        for (int i = 0; i < numRows; i++) {
            triangle[i] = new int[i + 1]; // 初始化每一行的数组
            triangle[i][0] = 1; // 每行的第一个数字
            triangle[i][i] = 1; // 每行的最后一个数字
            for (int j = 1; j < i; j++) { // 计算中间的值
                triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
            }
        }
        // 打印杨辉三角
        for (int i = 0; i < numRows; i++) {
            // 打印空格以使三角形居中
            for (int k = 0; k < numRows - i - 1; k++) {
                System.out.print("  ");
            }
            for (int j = 0; j < triangle[i].length; j++) {
                System.out.print(triangle[i][j] + "   ");
            }
            System.out.println();
        }
        scanner.close(); // 关闭scanner
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值