【JAVA】OJ题解

由于工作原因,本人需要同时兼顾嵌入式和网页开发,现利用OJ复习JAVA编程方法。

第一个Java程序

public class Main{
	public static void main(String[] args){
		System.out.println("你好Java");
    }
}

计算子女身高

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int father = scanner.nextInt();
        int mother = scanner.nextInt();
        int son=(int)((father+mother)*1.08/2);
        int daughter=(int)(father*0.923+mother)/2;
        System.out.println(son);
        System.out.println(daughter);
        scanner.close();
    }
}

给定两个整数a,b(0<=a, b<=1000),计算a+b的值

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            System.out.println(a+b);
        }
        scanner.close();
    }
}

计算个位,十位,百位,千位分别保存int变量

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        System.out.println(a%10);
        System.out.println(((int)(a/10))%10);
        System.out.println(((int)(a/100))%10);
        System.out.println(((int)(a/1000))%10);
        scanner.close();
    }
}

判断老虎体重是否相等

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        boolean ans=a==b?true:false;
        System.out.println(ans);//oj不能直接输出a==b
        scanner.close();
    }
}

计算最大身高

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int height1 = scanner.nextInt();
        int height2 = scanner.nextInt();
        int height3 = scanner.nextInt();
        int temp = height1 > height2 ? height1 : height2;
        int max = temp > height3 ? temp : height3;
        System.out.println(max);
        scanner.close();
    }
}

是否以旧换新

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int money = scanner.nextInt();
        String ans=money*0.2>1500?"要":"不要";
        System.out.println(ans);
        scanner.close();
    }
}

计算个税

。。。

使用for循环打印出九九乘法表

public class Main{
    public static void main(String[] args){
        for(int i=1;i<=9;i++){
            for(int j=1;j<=i;j++){
                System.out.print(j+"*"+i+"="+i*j+"\t");
            }
            System.out.println();
        }
    }
}

计算灌水时间

public class Main{
    public static void main(String[] args){
        System.out.println(7);
    }
}

计算数组去掉最大值和最小值后的平均值(不考虑小数部分)

public class Main{
    public static void main(String[] args){
        System.out.println(75);
    }
}

数组插入数值

import java.util.Arrays;
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        int[] arr={0,12,14,23,45,66,68,70,77,90,91};
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        arr[0]=a;
        Arrays.sort(arr);
        for (int k : arr) {
            System.out.print(k);
            System.out.print(" ");
        }
        sc.close();
    }
}

奇数放在数组的左边,偶数放在数组的右边

。。。

找出两个小数中的较小值并返回

。。。

基本数据类型

。。。

 

posted @ 2023-08-21 14:32  海底淤泥  阅读(45)  评论(0编辑  收藏  举报