Java程序设计---实验2

1.求任意输入的10个数的和。 

import java.util.Scanner;
public class Test1 {
    public static void main(String[] args){
        Scanner sca=new Scanner(System.in);
        double sum=0;//定义一个双精度浮点数作为结果,并设初值为0
        int input=1;//定义一个整形input设初始值为1,防止循环直接结束
        System.out.println("请输入数字:");
       while(input!=0){//while实现元素的循环输入
           input=sca.nextInt();
           sum+=input;//求和
       }
        System.out.println("sum="+sum);
    }
}

2.实验题目:获取三个整数中的最大值(用三元运算符)。

import java.util.Scanner;
public class Test2 {
    public static void main(String[] args){
        Scanner sca=new Scanner(System.in);
        int a,b,c;
        System.out.println("请输入要比较的三个整数:");
        System.out.print("请输入第一个数:");
        a=sca.nextInt();
        System.out.print("请输入第二个数:");
        b=sca.nextInt();
        System.out.print("请输入第三个数:");
        c=sca.nextInt();
        int max= a>b?(a>c?a:c):(b>c?b:c);
        System.out.println("最大值为:"+max);
    }
}

3.键盘录入月份的值,输出对应的季节。

import java.util.*;
 public class Test3{
    public static void main(String[] args){
        System.out.print("请输入月份:");
        Scanner sca=new Scanner(System.in);
        int mouth=sca.nextInt();
//        if (mouth == 2 || mouth == 3 || mouth == 4) {
//            System.out.println(mouth + "月是春季");
//        } else if (mouth == 5 || mouth == 6 || mouth == 7) {
//            System.out.println(mouth + "月是夏季");
//        } else if (mouth == 8 || mouth == 9 || mouth == 10) {
//            System.out.println(mouth + "月是秋季");
//        } else if (mouth == 11 || mouth == 12 || mouth == 1){
//            System.out.println(mouth + "月是冬季");
//        }else{
//            System.out.println(mouth + "月不存在");
//        }
        switch(mouth){
            case 2:
            case 3:
            case 4:
                System.out.println(mouth + "月是春季");
                break;
            case 5:
            case 6:
            case 7:
                System.out.println(mouth + "月是夏季");
                break;
            case 8:
            case 9:
            case 10:
                System.out.println(mouth + "月是秋季");
                break;
            case 11:
            case 12:
            case 1:
                System.out.println(mouth + "月是冬季");
                break;
            default:
                System.out.println(mouth + "月不存在");
        }
    }
}

4.输入年份和月份,输出该年月的天数。

import java.util.Scanner;
public class Test4 {
    public static void main(String[] args){
        Scanner scan=new Scanner(System.in);
        System.out.print("请输入年份和月份:");
        int year,month;
        int days=0;
        year=scan.nextInt();
        month=scan.nextInt();
        boolean isLeapYear;
        isLeapYear = (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
        switch(month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days=31;
                break;
            case 2:
                if(isLeapYear){
                    days=29;
                }else{
                    days=28;
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days=30;
                break;
            default:
                System.out.println("error!!!");
        }
        System.out.println(year+"年"+month+"月有"+days+"天");
    }
}

5.出租车计费问题。

• 开封市的出租车计费方式为:起步2公里内5元,2公里以上每公里收费1.3元,9公里以上每公里收费2元,燃油附加费1元。

• 编写程序,输入公里数,计算出所需的出租车费用。

public class Test5 {
    public static void main(String[] args){
        Scanner sca=new Scanner(System.in);
        System.out.print("请输入公里数:");
        int km=sca.nextInt();
        double money;
        if(km<=2){
            money=5;
        }else if(km>2&&km<9){
            money=5+(km-2)*1.3;
        }else{
            money=5+7*1.3+(km-9)*3;
        }
        System.out.println("你所需的出租车费用是"+money);
    }
}

6.分别用do-while和for循环计算1+1/2!-1/3!+1/4!-1/5!…的前20项之和。

 public class Test6 {
    public static void main(String[] args) {
        double a = 1, b = 1;
        double sum = 0;
        System.out.print("do-while循环:");
        do {
            sum += b;
            a++;
            b *= 1.0 / a;
        } while (a <= 20);
        System.out.println(sum);
//       int i,j;
//        for(i=1;i<=20;i++){
//            float t=1;
//            for(j=1;j<=i;j++){
//                t=t*j;
//            }
//            sum+=(1/t);
//        }
        System.out.print("for循环:");
        for (a = 1; a <= 20; a++) {
            b *= (1.0 / a);
            sum += b;
        }
        System.out.println(sum);
    }
}                     

7.求1000以内的完全数(一个数等于它的因子之和称为完全数)。

public class Test7 {
    public static void main(String[] args) {
        int i;
        int k=0;
        for(i=1;i<=1000;i++){
            int sum=0;
            for(int j=1;j<i;j++){
                if(i%j==0){
                    sum=sum+j;
                }
            }
            if(sum==i){
                System.out.println(i+"是完全数");
                k++;
            }
        }
        System.out.println("完全数的个数是"+k);
    }
}       

8.微信中的一个问题:一筐鸡蛋,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 Test8 {
    public static void main(String[] args) {
        int i=0;
        while (true){
            i++;
            if(i%2==1&&i%4==1&&i%5==4&&i%8==1){
                if(i%3==0&&i%7==0&&i%9==0){
                    if(i%6==3){
                        break;
                    }
                }
            }
        }
        System.out.println("筐里最少有"+i+"个鸡蛋");
    }

9.求六边形面积,六边形面积可以通过下面公式计算(s是边长):

注:使用Math类中的方法计算tan值。

import java.util.Scanner;
public class Test9 {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("请输入边长:");
        double s = input.nextDouble();//用户输入数据
        //判断数据的合法性
        if(s<0){
            System.out.println("输入的数字不合法");
        }
        //进行面积运算
        double area = (6 * s * s) / (4 * Math.tan(Math.PI / 6));
        //输出面积
        System.out.printf("六边形的面积是:"+area);
    }
}

10.实现会员注册,要求用户名长度不小于3,密码长度不小于6,若不满足需有提示信息,提示输入有误;注册时两次输入密码必须相同(字符串)。

import java.util.Scanner;
public class Test10 {
    public static void main(String[] args){
        Scanner sca=new Scanner(System.in);
        String name;
        String password;
        String newpassword;
        System.out.println("用户注册");
            System.out.print("用户名:");
            name=sca.next();
            System.out.print("设置密码:");
           password=sca.next();
            System.out.print("确认密码:");;
           newpassword=sca.next();
            while(name.length()< 3||(password.length())<6||(password.equals(newpassword)==false)){
                if(name.length()<3) {
                    System.out.println("用户名长度不得小于3");
                }
                    if(password.length()<6){
                        System.out.println("密码长度不得小于6");
                    }
                    if(password.equals(newpassword)==false){
                        System.out.println("两次密码不一致");
                    }
                    System.out.print("请重新输入用户名:");
                    name = sca.next();
                    System.out.print("请重新输入密码:");
                    password = sca.next();
                    System.out.print("请再次输入密码:");
                    newpassword = sca.next();
            }
        System.out.println("用户名:"+name);
        System.out.println("密码:"+password);
        System.out.println("注册成功!");
    }
}

 11.找出两个分教最高的学生)编写程序,提示输入学生的个数、每个学生的名字及其分数,最后显示获得最高分的学生和第二高分的学生。

import java.util.Scanner;
public class Test11 {
    public static void main(String[] args){
        Scanner sca=new Scanner(System.in);
        System.out.print("请输入学生的数量:");
        int n=sca.nextInt();
        if(n<2)
        {
            System.out.println("输入学生的个数过少");
            System.exit(0);//结束程序
        }
        String[] stuname=new String[n];
        int[] score=new int[n];
        for(int i=0;i<n;i++){
            System.out.print("请输入第"+(i+1)+"个学生的姓名:");
            stuname[i]=sca.next();
            System.out.print("请输入第"+(i+1)+"个学生的分数:");
            score[i]=sca.nextInt();
        }
        for(int x=0;x<n-1;x++){
            for(int y=0;y<n-x-1;y++){
                if(score[y]<score[y+1]) {
                    int temp1 = score[y];
                    score[y] = score[y + 1];
                    score[y + 1] = temp1;
                    String temp2 = stuname[y];
                stuname[y]=stuname[y+1];
                stuname[y+1]=temp2;
                }
            }
        }
        if(score[0]==score[1]) {
        System.out.println(stuname[0]+"和"+stuname[1]+"的成绩都为"+score[0]);
        }else{
            System.out.println("第一名同学的名字是"+stuname[0]+",他的成绩是"+score[0]);
            System.out.println("第二名同学的名字是"+stuname[1]+",他的成绩是"+score[1]);
        }
        }
    }

12.定义一维数组并初始化,通过键盘任意输入一个数,查找该数是否存在(结果返回下标值)。

import java.util.Scanner;
public class Test12 {
    public static void main(String[] args){
        Scanner sca=new Scanner(System.in);
        int[] arr=new int[]{10,20,30,40,50};
        System.out.print("请输入你要查找的数字:");
        int n=sca.nextInt();
        int i=0,p=-1;

//        for(i=0;i<arr.length;i++){
//            if(arr[i]==n){
//                p=i;
//                break;
//            }
//        }
//        if(p>=0){
//            System.out.println("所求下标为"+p);
//        }else if(p==-1){
//            System.out.println("该数字未在数组中。");
//        }
        while(i<arr.length){
            if(arr[i]==n){
                p=i;
                break;
            }
            i++;
        }
        if(p>=0){
            System.out.println("所求下标为"+p);
        }else if(p==-1){
            System.out.println("该数字未在数组中。");
        }
    }
}              

13.编写一个程序,将二维数组a转置后存入数组b(所谓转置就是行列互换)例如:

1 2 3

4 5 6

7 8 9   

的转置就是:

public class Test13 {
    public static void main(String[] args) {
    int[][] arr1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int[][] arr2=new int[3][3];
      for(int i = 0; i<arr1.length;i++){
        for (int j = 0; j < arr1[i].length; j++) {
            System.out.print(arr1[i][j] +"\t");
        }
        System.out.println();
      }
      System.out.println("转置后的数组:");
        for(int i = 0; i<arr2.length;i++){
            for (int j = 0; j < arr2[i].length; j++) {
                arr2[i][j]=arr1[j][i];
                System.out.print(arr2[i][j] +"\t");
            }
            System.out.println();
        }
    }
}
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

用户1234567890

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值