Java入门在线编程题
#Java入门在线编程题
```java ##基本数据类型 ###设计一个方法,将一个小于2147483647的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){ return (int)d; } } ##运算符 ###输入两个正整数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(); if(a<=b){
int temp=a; a=b; b=temp; } System.out.printf("%d %d %d %d %d",a+b,a-b,a*b,a/b,a%b); }
} ###定义一个int类型变量i,i为由浮点数变量d四舍五入后的整数类型,请将转换后的i进行输出 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double d= scanner.nextDouble(); int i=(int)d; if((d-i)>0.5){ i++; } System.out.println(i); } } ###在不使用第三个变量的情况下交换两个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(); int temp = a; a = b; b = temp; System.out.println(a+" "+b); } } ##分支 ###牛牛商场促销活动: 满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>=5000){ cost=(int)(price*0.6); }else if(price>=2000){ cost = (int) (price * 0.7); }else if(price>=500){ cost = (int) (price * 0.8); }else if(price>=100){ cost=(int)(price*0.9); }else{ cost=(int)(price); } System.out.println(cost); } } ###判断体重指数 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 whh=weight/(height*height); if(whh>24.9){ System.out.println("偏胖"); }else if(whh>=20.9&&whh<=24.9){ System.out.println("适中"); }else if(whh>=18.5&&whh<20.9){ System.out.println("苗条"); }else{ System.out.println("偏瘦"); } } } ###判断学生成绩等级 import java.util.Scanner; //switch case 思路 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String grade =scanner.next(); switch (grade){ case "A": System.out.println("优秀"); break; case "B": System.out.println("良好"); break; case "C": System.out.println("及格"); break; case "D": System.out.println("不及格"); break; default: System.out.println("未知等级"); } } } ###邮箱验证 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]+"; if(str.matches(emailMatcher)){ System.out.println("邮箱格式合法"); }else{ System.out.println("邮箱格式不合法"); } } }