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

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

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=0){
//            System.out.println("所求下标为"+p);
//        }else if(p==-1){
//            System.out.println("该数字未在数组中。");
//        }
        while(i=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

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧

本文标题:Java程序设计---实验2-创新互联
当前网址:https://www.cdcxhl.com/article46/gsshg.html

成都网站建设公司_创新互联,为您提供网页设计公司App设计小程序开发微信公众号外贸建站网站导航

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联