In Class Programming 3

In today’s class we have learnt about comparisons and sorting. The following practice help me go through the knowledge I absorbed. And this set of exercise reinforce my knowledge in dealing with nested loops and control embedded in loops.

Code 1

The first code is about finding the maximum and the minimum in an input array. It can be used in finding the most remarkable value for different data. Trace Table on Class Website.

import java.util.*;
public class Practice4 {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int[] myArray=new int [1000];
        System.out.print("Please tell me how much number you 
        are going to put in:");
        int n;
        n=input.nextInt();
        for(int i=0;i<n;i++){
            myArray[i]=input.nextInt();
        }
        int max=myArray[0];
        int min=max;
        for(int i=0;i<n;i++){
            if(myArray[i]>max){
                max=myArray[i];
            }
            if(myArray[i]<min){
                min=myArray[i];
            }
        }
        System.out.println("The maximum number is "+max+".");
        System.out.println("The mainimum number is "+min+".");
    }
}

Input & Output
这里写图片描述

Flowchart
这里写图片描述

Code 2

The second code is used to calculate the average for a group of numbers. It can be used to find out the average for a large amount of data. Trace Table on Class Website.

import java.util.*;
public class Practice5 {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int num=0,total=0,count=0;      
        for(;num>=0;){
            System.out.println("Please enter a number which is 
            greater or equal to 0 in order to calculate the 
            average:");
            num=input.nextInt();
            if(num>=0){
                total+=num;
                count++;
            }
        }
        System.out.println("The average is "+total/count+".");
    }
}

Input & Output
这里写图片描述

Flowchart
这里写图片描述

Code 3

The third code is about sorting an array. It can be used to arrange a group of data from the largest to the smallest or the smallest to the largest. Trace Table on Class Website.

import java.util.*;
public class Practice6 {
    public static void main(String[] args){
        int num[] ={15,30,25,85,40,90,50,65,20,60};
        int temp;
        System.out.println("Before sorting:");
        for(int i=0;i<=9;i++){
            System.out.print(num[i]+" ");
        }
        for(int i=0;i<=8;i++){
            for(int j=0;j<=8;j++){
                if(num[j]<num[j+1]){
                    temp=num[j];
                    num[j]=num[j+1];
                    num[j+1]=temp;
                }
            }
        }
        System.out.println();
        System.out.println("After sorting:");
        for(int i=0;i<=9;i++){
            System.out.print(num[i]+" ");
        }
    }
}

Input & Output
这里写图片描述

Flowchart
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值