Java第六次作业

Posted A王洋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java第六次作业相关的知识,希望对你有一定的参考价值。

1、  给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。

package qwda;

 

import java.util.Arrays;

 

public class Test 

 

    public static void main(String[] args) 

        // TODO Auto-generated method stub

        int a[] = 1,6,2,3,9,4,5,7,8;

        System.out.println("Arrays.sort排序:");

        Arrays.sort(a);

        for (int i:a) 

            System.out.println(i);

        

        System.out.println("冒泡排序");

        for (int i =0;i<a.length-1;i++) 

            for (int j =0; j<a.length-1-i;j++) 

                if (a[j]>a[j+1]) 

                   int temp =a[j];

                   a[j]=a[j+1];

                   a[j+1] = temp;

                

            

        

    for (int i:a) 

        System.out.println(i);

    

    

   

 

 

 

 

 

 

2、  输出一个double型二维数组(长度分别为5、4,值自己设定)的值。

package qwda;

 

public class Test 

 

    public static void main(String[] args) 

        // TODO Auto-generated method stub

        double a[][]= new double [5][4];

        for (int i = 0;i<a.length;i++) 

            for (int j = 0; j < a[i].length;j++) 

                a[i][j] = i;

            

        

        for (int i = 0;i<a.length;i++) 

            for (int j = 0;j<a[i].length;j++) 

                System.out.print(a[i][j]+"");

            

        System.out.println();

        

       

    

 

 

3、  在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。

 

package qwda;

 

public class Test 

 

    public static void main(String[] args) 

        // TODO Auto-generated method stub

        int a[]=18,25,7,36,13,2,89,63;

        int max = a[0];

        int number =0;

        for (int i=1;i<a.length;i++) 

            if (a[i]>max) 

                max=a[i];

            

        number++;

        

        System.out.println("最大值为:"+max);

        System.out.println("下标为:"+number);

    

 

 

 

4、  将一个数组中的元素逆序存放

 

package qwda;

 

import java.util.Arrays;

 

public class Test 

 

    public static void main(String[] args) 

        // TODO Auto-generated method stub

        int a[]=10,20,30,40,50;

        int b;

        for (int i =0; i<a.length/2;i++) 

            b=a[i];

            a[i]=a[a.length-i-1];

            a[a.length-i-1]=b;

        

        System.out.println("倒序输出数值为:");

        for (int i=0;i<a.length;i++) 

            System.out.println(a[i]);

        

    

 

 

 

5. 将一个数组中的重复元素保留一个其他的清零。

 

package qwda;

 

import java.util.Arrays;

 

public class Test 

 

    public static void main(String[] args) 

        // TODO Auto-generated method stub

        int a[]=10,20,30,40,50,30,10,40;

        for (int i =0; i<a.length;i++) 

            for (int j=0; j<a.length;j++) 

                if (a[i]==a[j]&&i!=j) 

                   a[j]=0;

                

            

        

    for (int i=0;i<a.length;i++) 

        System.out.println(a[i]);

    

    

5、  给定一维数组 -10,2,3,246,-100,0,5,计算出数组中的平均值、最大值、最小值。

 

package qwda;

 

import java.util.Arrays;

 

public class Test 

 

    public static void main(String[] args) 

        // TODO Auto-generated method stub

        int a[]=-10,2,3,246,-100,0,5;

        int max=a[0];

        int min=a[0];

        double sum=0;

        for (int i = 0; i < a.length; i++) 

            if (max<a[i]) 

                max=a[i];

            

            if (min>a[i]) 

                min=a[i];

            

            sum+=a[i];

        

        sum=sum/7;

        System.out.println("最大值"+max);

        System.out.println("最小值"+min);

        System.out.println("平均值"+sum);

    

    

 

 

 

7、使用数组存放裴波那契数列的前20项 ,并输出

package qwda;

 

import java.util.Arrays;

 

public class Test 

 

    public static void main(String[] args) 

        // TODO Auto-generated method stub

        int[] a = new int[20];

        a[0] = 1;

        a[1] = 1;

        for (int i = 2; i < a.length; i++) 

            a[i] = a[i - 1] + a[i - 2];

        

        for (int i = 0; i < 8; i++) 

            System.out.print(a[i] + " ");

        



 

 

8、生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出

package qwda;

 

import java.util.Scanner;

 

import java.util.Arrays;

 

public class Test 

    public static void main(String[] args) 

        // TODO Auto-generated method stub

        int a[] = new int[10];

        System.out.println("随机生成长度为10的数组:");

        for (int i = 0; i < a.length; i++) 

            a[i] = (int) (Math.random() * 100);

        

        for (int i = 0; i < a.length; i++) 

            System.out.println(a[i]);

        

        System.out.println("排序后为:");

        Arrays.sort(a);

        for (int i = 0; i < a.length; i++) 

            System.out.println(a[i]);

        

    

 

 

 

 

 

 

9、做一个菜单切换程序。主菜单1.登陆 2.注册 3幸运抽奖 4 退出。每个菜单可以返回主菜单

package qwda;

 

import java.util.Scanner;

 

public class Test 

 

    public static void main(String[] args) 

        // TODO Auto-generated method stub

        System.out.println("1.登录");

        System.out.println("2.注册");

        System.out.println("3.幸运抽奖");

        System.out.println("4.退出");

        Scanner input = new Scanner(System.in);

        int i = input.nextInt();

        switch (i) 

        case 1:

            login();

            break;

        case 2:

            zhuce();

            break;

        

    

 

    public static void login() 

        System.out.println("輸入用戶名");

        Scanner input = new Scanner(System.in);

        String name = input.next();

        System.out.println("输入密码");

        String pwd = input.next();

        if (name.equals(pwd)) 

            System.out.println("登录成功");

         else 

            System.out.println("登录失败");

        

        System.out.println("是否返回主菜单?Y/N");

        if (input.next().equalsIgnoreCase("y")) 

            showMain();

         else 

            System.out.println("谢谢使用");

        

    

 

    public static void zhuce() 

        System.out.println("请输入用户名");

        Scanner input = new Scanner(System.in);

        String name = input.next();

        System.out.println("输入密码");

        String pwd = input.next();

        System.out.println("注册成功");

        System.out.println("是否返回主菜单?Y/N");

        if (input.next().equalsIgnoreCase("y")) 

            showMain();

         else 

            System.out.println("谢谢使用");

        

 

    

 

    public static void main(String[] args) 

        showMain();

    

 

 

 

java第六次作业

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.*;
public class choujiang {
    private ArrayList<Integer> cards;

    public void deal(int num){
        if (cards==null){
            cards=new ArrayList<Integer>();
            for(num=1;num<=100;num++){

                cards.add(num);
            }
        }
        Collections.shuffle(cards);
    }
    public void draw(){
       Random ra=new Random();
        int s =ra.nextInt(cards.size());
        System.out.println("一等奖:"+ cards.get(s));
        cards.remove(cards.get(s));
        Collections.shuffle(cards);
       
        for(int i=0;i<2;i++){
            int stwo=ra.nextInt(cards.size());
            System.out.println("二等奖:"+ cards.get(stwo));
            cards.remove(cards.get(stwo));
            }
        Collections.shuffle(cards);
            for(int j =0;j<3;j++){
                int sthree =ra.nextInt(cards.size());
                System.out.println("三等奖:"+cards.get(sthree));
                cards.remove(cards.get(sthree));
            }

         }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        choujiang c = new choujiang();
        c.deal(0);
        c.draw();
    }
}

技术分享

以上是关于Java第六次作业的主要内容,如果未能解决你的问题,请参考以下文章

Java第六次作业修改版

第六次java作业

java第六次作业

java第六次作业

Java第六次作业

Java 第六次作业