Java开发中经典的小实例-(比较输入数值的最大值、最小值和平均值)
//输入数字个数来产生数字并且比较大小
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要输入的数字个数:");
int num = sc.nextInt();
int[] array = new int[num];
for (int i = 0; i < array.length; i++) {
System.out.println("请输入第" + (i + 1) + "个数。");
array[i] = sc.nextInt();
}
System.out.println("最大值:" + getArrayMax(array));
System.out.println("最小值:" + getArrayMin(array));
}
public static int getArrayMax(int[] array) {
int max = array[0];
for (int i = 1; i < array.length; i++) {
max = (max < array[i]) ? array[i] : max;
}
return max;
}
public static int getArrayMin(int[] array) {
int min = 0;
for (int i = 1; i < array.length; i++) {
min = (array[min] > array[i]) ? i : min;
}
return array[min];
}
}
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
* int num=123; int sum;
*
* int a=num/100%10; int b=num/10%10; int c=num%10;
*
* sum=a+b+c;
*
* System.out.println(sum);
*/
Scanner input = new Scanner(System.in);
int max = 0;
int min = 0;
for (int i = 0; i < 5; i++) {
System.out.println("请输入第" + (i + 1) + "个数");
int number = input.nextInt();
if (i == 0) {
max = number;
min = number;
} else {
if (max < number) {
max = number;
}
if (min > number) {
min = number;
}
}
}
System.out.println("最大值为:" + max);
System.out.println("最小值为:" + min);
}
}
-----------------------------------------------------------------------------------------
import java.util.Scanner;
public class Test22 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入十个成绩 :");
int[] score = new int[10];
int max = 0;
double avg = 0;
int sum = 0;
int min = score[0];
for (int i = 0; i < score.length; i++) {
System.out.println("请输入第" + (i + 1) + "个成绩 :");
score[i] = input.nextInt();
if (i == 0) {
max = score[i];
min = score[i];
} else {
if (score[i] > max) {
max = score[i];
}
if (score[i] < min) {
min = score[i];
}
}
sum = sum + score[i];
}
avg = sum / 10;
System.out.println("最高分是:" + max);
System.out.println("最低分是:" + min);
System.out.println("平均分是:" + avg);
}
}
Java开发中经典的小实例-(比较输入数值的最大值、最小值和平均值)的更多相关文章
- Java开发中经典的小实例-(输入三个数字判断三角形类型)
import java.util.Scanner;public class threeTest { public static void main(String[] args) { ...
- Java开发中经典的小实例-(打印输入重复的值)
import java.util.ArrayList;import java.util.Scanner;public class Test8 { public static void main( ...
- Java开发中经典的小实例-(二分法)
public int binarySearch(int[] data,int aim){//以int数组为例,aim为需要查找的数 int start = 0; int end = data.leng ...
- Java开发中经典的小实例-(while(参数){})
import java.util.Scanner;public class Test_while { public static void main(String[] args) { ...
- Java开发中经典的小实例-(swich(){case:参数break;default: break;})
import java.util.Scanner;public class Test6 { public static void main(String[] args) { // ...
- Java开发中经典的小实例-(能被3整除,并且十个数换一行)
import java.util.Scanner;public class Test15 { public static void main(String[] args) { // ...
- Java开发中经典的小实例-(do{}while())
import java.util.Scanner;public class Test13 { public static void main(String[] args) { // ...
- Java开发中经典的小实例-(随机产生验证码)
import java.util.Scanner;public class Test10 { public static void main(String[] args) { // ...
- Java开发中经典的小实例-(随机数)
import java.util.Random;//输出小于33的7个不相同的随机数public class probability { static Random random = new R ...
随机推荐
- HashMap实现原理分析
1. HashMap的数据结构 数据结构中有数组和链表来实现对数据的存储,但这两者基本上是两个极端. 数组 数组存储区间是连续的,占用内存严重,故空间复杂的很大.但数组的二分查找时间复杂度小,为O(1 ...
- html5:地理信息 LBS基于地理的服务和百度地图API的使用
地理位置请求 单次定位请求getCurrentPosition(请求成功函数,请求失败函数,数据收集方式) 多次定位请求watchPosition(请求成功函数,请求失败函数,数据收集方式) 关闭更新 ...
- rabbitmq之消息转储vm_memory_high_watermark_paging
rabbitmq为了预防内存达到上限,会在内存使用到了一定比例后,将消息转储到磁盘去.
- Android 自定义RecyclerView 实现真正的Gallery效果
http://blog.csdn.net/lmj623565791/article/details/38173061
- [curator] Netflix Curator 使用
curator简介 Netflix curator 是Netflix公司开源的一个Zookeeper client library,用于简化zookeeper客户端编程,包含一下几个模块: curat ...
- kernel update 2.6.18-2.6.38
细致某种程度上可以体现作者的开放精神 copy for build a note needed when config the kernel 主要的问题出在了menu.lst 和 grub.conf文 ...
- Cursor use
Ref:http://www.cnblogs.com/Gavinzhao/archive/2010/07/14/1777644.html declare @Id varchar(100),@name ...
- 酶设计软件rosetta安装
1.Rosetta uses SCons as a compile assistant. You will likely need to download and install this first ...
- Dijkstar算法的数学原理
Dijkstar算法是荷兰数学家迪克斯屈拉(or迪杰斯特拉?)在1959年发现的一个算法.是现有的几个求带权图中两个顶点之间最短通路的算法之一.算是一个相当经典的算法了. 迪克斯屈拉算法应用于无向连通 ...
- [学习Vulkan之一] 初识Vulkan
Vulkan是Khronos组织制定的"下一代"开放的图形显示API,是与DirectX12可以匹敌的GPU API标准.Vulkan是基于AMD的Mantle API演化而来,目 ...