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 ...
随机推荐
- Windows Server 2003修改远程桌面最大连接数的方法
Windows Server 2003远程桌面允许的终端连接数默认情况下只有2个线程,也就是2个用户,当我们需要多人同时登录服务器的时候,就需要适当增加远程连接同时在线的用户数. 修改远程桌面最大连接 ...
- Choose Concurrency-Friendly Data Structures
What is a high-performance data structure? To answer that question, we're used to applying normal co ...
- request获取请求头和请求数据
package cn.itcast.request; import java.io.IOException; import java.io.InputStream; import java.io.Pr ...
- JS中的特有语句-for in
<script> /* *js中特有语句for in *for(变量 in 对象)//对对象进行变量的语句 *{ *} */ var arr = [32,80,65]; for(i in ...
- LINUX VI 常用命令
vi 打开或新建 vi filename 打开或新建文件 并将光标置于第一行首 光标 ) 光标移至句尾 ( 光标移至句首 屏幕翻滚类命令 Ctrl+u 向文件首翻半屏 Ctrl+d 向文件尾翻半屏 ...
- mysql中engine=innodb和engine=myisam的区别
最开始用MySQL Administrator建数据库的时候,表缺省是InnoDB类型,也就没有在意.后来用Access2MySQL导数据的时候发现只能导成 MyISAM类型的表,不知道这两种类型有什 ...
- fiddler note
一晚上终于找到对的东西,作者很专业,这里作为笔记,想学习好东西还是到作者那里^_^ ---------------------------------------------------------- ...
- Android利用Java反射机制修改Android System Language
private void updateLanguage(Locale locale) { try { Object objIActMag, objActMagNative; Class clzIAct ...
- paper 107:图像的白平衡
所谓白平衡(White Balance):指在图像处理的过程中,对原本材质为白色的物体的图像进行色彩还原,去除外部光源色温的影响,使其在照片上也显示白色.也就是不管在任何光源下,都能将白色物体还原为白 ...
- Orchard part8
http://skywalkersoftwaredevelopment.net/blog/writing-an-orchard-webshop-module-from-scratch-part-8 定 ...