设计一个方法,将一个小于2147483647的double类型变量以截断取整方式转化为int类型

输入描述:

随机double类型变量

输出描述:

转化后的int类型变量

代码:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double d = scanner.nextDouble();
        System.out.println(Main.typeConversion(d));
    }
    public static int typeConversion(double d){
         int n=(int)d;
         return n;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

计算:

描述

输入两个正整数a和b,输出这两个正整数的和,差,积,商,模(若a>b则输出a-b,a/b,a%b的值反之输出b-a,b/a,b%a的值,不考虑小数,请使用int类型)

输入描述:

两个正整数

输出描述:

它们的和,差,积,商,模。每个值之间用空格隔开

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();
        scanner.close();
        System.out.print(a+b+" ");
        if(a>b)
        System.out.print(a-b+" ");
        else
            System.out.print(b-a+" ");
        System.out.print(a*b+" ");
        if(a>b){
                System.out.print(a/b+" ");
                System.out.print(a%b);}
        else{
                System.out.print(b/a+" ");
                System.out.print(b%a);
        }
        
        

        //write your code here......
        

    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.

四舍五入:

定义一个int类型变量i,i为由浮点数变量d四舍五入后的整数类型,请将转换后的i进行输出

输入描述:

用户随机输入的浮点数

输出描述:

四舍五入之后的整数(小数点后一位>=5则进一,否则舍去)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double d= scanner.nextDouble();

        //write your code here......
        int i=(int)Math.round(d);

        System.out.println(i);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

交换变量值

描述

在不使用第三个变量的情况下交换两个int类型变量的值

输入描述:

a变量和b变量的值

输出描述:

交换后a变量和b变量的值,中间用空格隔开

代码:

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();
        int c;
        c=a;
        a=b;
        b=c;
        //write your code here.......
        

        System.out.println(a+" "+b);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

计算商场折扣:

牛牛商场促销活动:

满100全额打9折;

满500全额打8折;

满2000全额打7折;

满5000全额打6折;

且商场有抹零活动,不足一元的部分不需要付款(类型强制转换)

牛大姨算不清楚自己应该付多少钱,请你帮忙算一下

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        int price = console.nextInt();
        int cost = 0;
         if(price<100){
             cost=price;
         }
        else if(price>=100&&price<500){
            cost=(int)(price*0.9);
        }
        else if(price>=500&&price<2000){
            cost=(int)(price*0.8);
        }
        else if(price>=2000&&price<5000){
            cost=(int)(price*0.7);
        }
        else{
            cost=(int)(price*0.6);
        }
        

        System.out.println(cost);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

强制类型转换(int)(price*0.9)加两个括号


体重指数 = 体重 (kg) / ( 身高 (m) × 身高 (m) ),小于18.5属于偏瘦,介于18.5和20.9之间(左闭右开)属于苗条,介于20.9和24.9之间(左闭右闭)属于适中,超过24.9属于偏胖。下面由你来编写一段逻辑,算出输入了身高和体重的用户的体重指数,并返回他们的身材状态。(体重指数请使用double类型)

输入描述:

用户的身高(m)和用户的体重(kg)

输出描述:

体重指数代表的用户身材状态

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double height = scanner.nextDouble();
        double weight = scanner.nextDouble();
       double bmi=weight/(height*height);
        if(bmi<18.5){
            System.out.print("偏瘦");
        }
        else if(bmi>=18.5&&bmi<20.9){
            System.out.print("苗条");
        }
        else if(bmi>=20.9&&bmi<=24.9){
            System.out.print("适中");
        }
        else{
            System.out.print("偏胖");
        }
        //write your code here......
        

    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.

学生等级:

描述

现有如下成绩等级A,B,C,D。其中A代表优秀,B代表良好,C代表及格,D代表不及格。现在请你来为学生的成绩进行分级,如果用户输入错误则输出未知等级。

输入描述:

成绩等级对应的中文表述

输出描述:

优秀,良好,及格,不及格,未知等级

代码:

判断字符串相等就equals方法

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String grade =scanner.next();
        if(grade.equals("A")){
            System.out.print("优秀");
        }
        else if(grade.equals("B")){
             System.out.print("良好");
        }
        else if(grade.equals("C")){
             System.out.print("及格");
        }
        else if(grade.equals("D")){
             System.out.print("不及格");
        }
        else{
             System.out.print("未知等级");
        }
        //write your code here......
        

    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

正则表达式是否合法:

请根据给出的正则表达式来验证邮箱格式是否合法,如果用户输入的格式合法则输出「邮箱格式合法」,否则输出「邮箱格式不合法」。

输入描述:

任意字符串

输出描述:

根据输入的邮箱格式判断其合法于不合法,若输入字符串符合邮箱格式则输出邮箱格式合法,否则输出邮箱格式不合法

str.mtaches(“正则表达式”);str是否满足正则表达式

?:选择运算符:化简代码

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        String emailMatcher="[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+";
        System.out.println(str.matches(emailMatcher)?"邮箱格式合法":"邮箱格式不合法");
        //write your code here......
        

    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

求和:

有数列为:9,99,999,...,9999999999。要求使用程序计算此数列的和,并在控制台输出结果。(请尝试使用循环的方式生成这个数列并同时在循环中求和)

都用long类型

public class Main {
    public static void main(String[] args) {
        long sum=0;
        long item=0;
        for(int i=1;i<=10;i++){
            item=item*10+9;
            sum+=item;
        }
        System.out.println(sum);
        
        

    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

正数个数:

控制台输入整数,请设计一个死循环,当用户输入非正数时停止输入。请给出用户输入的正整数个数(默认输入个数不超过2147483647)

import java.util.*;

public class Main {
    public static void main(String[] args) {
        int count = 0;
        Scanner scanner = new Scanner(System.in);
        int d=scanner.nextInt();
        while(d>0){
            count++;
            d=scanner.nextInt();
        }
        System.out.println(count);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.