一、实验目的
1、掌握变量的命名是否符合Java关于标识符的命名规则。
2、掌握8种基本数据类型和3种类型转换的方法。
3、熟悉各种运算符的优先级和结合方向。
4、掌握控制结构的3种语句。
5、掌握Java输入数据的基本方法;Scanner。
6、掌握方法的定义、调用格式,理解方法调用和参数传递的原理,掌握方法的重载,理解方法的递归调用。
7、掌握数组的定义、动态和静态初始化格式,数组的遍历和应用,Array工具类的使用。
二、实验内容
1、实验题目:基本语法
(1)求任意输入的10个数的和。
import java.util.Scanner;
public class S2_1_1 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
double sum=0;
System.out.println("请输入10个数,每输入一个数回车确认");
for (int i=0;i<10;i++){
double a=sc.nextDouble();
sum+=a;
}
System.out.println("这十个数的和为:"+sum);
}
}
(2)实验题目:获取三个整数中的最大值(用三元运算符)。
import java.util.Scanner;
public class S2_1_2 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入三个整数:");
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
int max;
max=a>b?a:b;
max=max>c?max:c;
System.out.println("最大数为:"+max);
}
}
2、实验题目:选择结构的应用
(1)键盘录入月份的值,输出对应的季节。
import java.util.Scanner;
public class S2_2_1 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入月份:");
int month=sc.nextInt();
if(month>=3&&month<=5)
System.out.println("此时为春天");
else if(month>=6&&month<=8)
System.out.println("此时为夏天");
else if(month>=9&&month<=11)
System.out.println("此时为秋天");
else if(month>=1&&month<=2||month==12)
System.out.println("此时为冬天");
else
System.out.println("月份输入错误!!");
}
}
(2)输入年份和月份,输出该年月的天数。
import java.util.Scanner;
public class S2_2_2 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入年份:");
int year=sc.nextInt();
System.out.println("请输入月份:");
int month=sc.nextInt();
int day;
switch (month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("该年月的天数为31天");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println("该年月的天数为30天");
break;
case 2:
if(year%4==0&&year%100!=0||year%400==0)
System.out.println("该年月的天数为29天");
else
System.out.println("该年月的天数为28天");
}
}
}
(3)出租车计费问题。
• 开封市的出租车计费方式为:起步2公里内5元,2公里以上每公里收费1.3元,9公里以上每公里收费2元,燃油附加费1元。
• 编写程序,输入公里数,计算出所需的出租车费用。
import java.util.Scanner;
public class S2_2_3 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
double charge=0;
System.out.println("请输入公里数:");
double kilometer=sc.nextDouble();
while(kilometer<0){
System.out.println("输入错误,请重新输入!!");
System.out.println("请输入公里数:");
kilometer=sc.nextDouble();
}
if(kilometer==0){
charge=0;
}
else if (kilometer<=2) {
charge=5;
}
else if (kilometer<=9) {
charge=5+1.3*(kilometer-2);
}
else if(kilometer>9){
charge=5+1.3*7+(kilometer-9)*2+1;
}
System.out.println("总费用为:"+charge+"元");
}
}
3、实验题目:循环结构的应用
(1)分别用do-while和for循环计算1+1/2!-1/3!+1/4!-1/5!…的前20项之和。
public class S2_3_1 {
public static void main(String[] args){
int i=1,x=2;//i是分子,x是循环次数
double sum=1;
do {
double n=x;
for(double j=n-1;j>=1;j--){
n*=j;
}
sum+=i/n;
i=-i;
x++;
}while (x<21);
System.out.println("和为:"+sum);
}
}
(2)求1000以内的完全数(一个数等于它的因子之和称为完全数)。
public class S2_3_2 {
public static void main(String[] args){
System.out.println("1000以内的完全数为:");
for(int i=1;i<=1000;i++){
int sum=1;
for (int j=2;j<i;j++){
if(i%j==0){
sum+=j;
}
}
if(sum==i){
System.out.println(i);
}
}
}
}
(3)微信中的一个问题:一筐鸡蛋,1个1个拿,正好拿完。
2个2个拿,还剩1个。
3个3个拿,正好拿完。
4个4个拿,还剩1个。
5个5个拿,还差1个。
6个6个拿,还剩3个。
7个7个拿,正好拿完。
8个8个拿,还剩1个。
9个9个拿,正好拿完。
问筐里最少有多少鸡蛋?
public class S2_3_3 {
public static void main(String[] args){
for(int i=1;i<10000;i++){
if(i%1==0&&i%2==1&&i%3==0&&i%4==1&&i%5==4&&i%6==3&&i%7==0&&i%8==1&&i%9==0){
System.out.println("鸡蛋的个数可能为:"+i);
}
}
}
}
5、实验题目:课后作业题
(1)求六边形面积,六边形面积可以通过下面公式计算(s是边长):
注:使用Math类中的方法计算tan值。
import java.util.Scanner;
public class S2_5_1 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入六边形的边长:");
double length=sc.nextByte();
double area;
area=6*length*length/(4*Math.tan(Math.PI/6));
System.out.println("该六边形的面积为:"+area);
}
}
(2)实现会员注册,要求用户名长度不小于3,密码长度不小于6,若不满足需有提示信息,提示输入有误;注册时两次输入密码必须相同(字符串)。
import java.util.Scanner;
public class S2_5_2 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String name,password1,password2;
while(true){
System.out.println("请输入用户名(长度不小于3):");
name=sc.next();
if(name.length()<3){
System.out.println("长度小于3,请重新输入!");
}
else{
break;
}
}
while(true){
System.out.println("请输入密码(长度不小于6)");
password1=sc.next();
if(password1.length()<6){
System.out.println("长度小于6,请重新输入!");
}
else {
break;
}
}
while(true){
System.out.println("请确认密码:");
password2=sc.next();
if(!password1.equals(password2)){
System.out.println("两次输入不一致,请重新输入!");
}
else {
break;
}
}
System.out.println("注册成功!");
}
}
(3)找出两个分数最高的学生)编写程序,提示输入学生的个数、每个学生的名字及其分数,最后显示获得最高分的学生和第二高分的学生。