public static void main(String[] args)
创新互联建站主要从事网页设计、PC网站建设(电脑版网站建设)、wap网站建设(手机版网站建设)、响应式网站开发、程序开发、网站优化、微网站、成都微信小程序等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了丰富的成都网站制作、网站设计、网站设计、网络营销经验,集策划、开发、设计、营销、管理等多方位专业化运作于一体。
{
int a[]=new int[10];
Scanner sc = new Scanner(System.in);
System.out.println("请输入十个数字0~100");
for(int i=0;i10;i++)
{
System.out.println("输入第"+(i+1)+"数:");
a[i]=sc.nextInt();
}
int b[]=new int[11];
for(int i=0;i11;i++)
{
b[i]=0;
}
for(int i=0;i10;i++)
{
if(a[i]=0a[i]=9)
{
b[0]++;
}
else if(a[i]=10a[i]=19)
{
b[1]++;
}
else if(a[i]=20a[i]=29)
{
b[2]++;
}
else if(a[i]=30a[i]=39)
{
b[3]++;
}
else if(a[i]=40a[i]=49)
{
b[4]++;
}
else if(a[i]=50a[i]=59)
{
b[5]++;
}
else if(a[i]=60a[i]=69)
{
b[6]++;
}
else if(a[i]=70a[i]=79)
{
b[7]++;
}
else if(a[i]=80a[i]=89)
{
b[8]++;
}
else if(a[i]=90a[i]=99)
{
b[9]++;
}
else if(a[i]==100)
{
b[10]++;
}
}
System.out.println("0~9人数:"+b[0]+";10~19人数:"+b[1]+";20~29人数:"+b[2]+";30~39人数:"+b[3]+";40~49人数:"+b[4]+";50~59人数:"+b[5]+";60~69人数:"+b[6]+";70~79人数:"+b[7]+";80~89人数:"+b[8]+";90~99人数:"+b[9]+";100人数:"+b[10]);
for(int i=0;i11;i++)
{
System.out.println("第"+(i+1)+"段人数为:"+b[i]);
}
}
简单的方法是:
public static void main(String[] args)
{
int a[]=new int[11];
Scanner sc = new Scanner(System.in);
System.out.println("请输入十个数字0~100");
for(int i=0;i10;i++)
{
System.out.println("输入第"+(i+1)+"数:");
a[sc.nextInt()/10]++;//对于这种根据读取数据分段的数据利用int型数据整除可以很好的分出每个段的个数;
}
for(int i=0;i11;i++)
{
System.out.println("第"+(i+1)+"段人数为:"+a[i]);
}
}
int[] grade = {65,76,87,98,100,.........};//定义一个数组保存学生成绩
int[] count = {0,0,0,0,0};//定义一个数组代表各分数段的人数
for(int i=0;igrade.lenth;i++){
int k = grade[i]/10 - 6; //判断此成绩属于哪一个分数段
count[k] += 1; //此分数段的人数加一
}
import java.util.Scanner;
public class test {
static int s;
public static void main(String[] argv) {
int [] num=new int[20];
Scanner scanner=new Scanner(System.in);
Integer a;
int i;
for(i=1;i=20;i++){
System.out.println("请输入第"+i+"一个分数(结束请输入520嘿嘿):");
a=scanner.nextInt();
num[i-1]=a;
if(a==520){
break;
}
}
for(int m=0;mi-1;m++)
{
s=s+num[m];
}
System.out.println("总共录入了"+(i-1)+"个分数");
System.out.println("总成绩是"+s+"分");
System.out.println("平均分是"+s/(i-1)+"分");
for(Integer m=0;mnum.length-1;m++)
{ if(num[m]0num[m]!=520){
if(num[m]=90)
System.out.println(num[m]+"优秀");
else if(num[m]=80)
System.out.println(num[m]+"良好");
else if(num[m]=70)
System.out.println(num[m]+"中等");
else if(num[m]=60)
System.out.println(num[m]+"及格");
else
System.out.println(num[m]+"不及格");
}else break;
}
}
}
我的代码很简洁,采纳我呗。。。
import java.util.Scanner;
public class Test {
protected static boolean end_flag = true;
public static void main(String[] args) {
Students student = new Students();
student.InputData();// 输入学生数据
String order;
while (end_flag) {
System.out.println("请输入数字1-5:");
Scanner s = new Scanner(System.in);
order = s.nextLine();
student.Operator(order);
}
}
}
Students类:
import java.util.ArrayList;
import java.util.Scanner;
public class Students {
private int STUDENTS_NUM = 5; // 学生的人数
private int SUBJECTS_NUM = 3; // 课程数
private String[][] data = new String[STUDENTS_NUM][SUBJECTS_NUM + 2];
private String name;
private int Chinese_score;
private int Math_score;
private int English_score;
public void SearchHighestScore(String[][] original) { // 查找最高成绩
int flag = Integer.parseInt(original[0][4]), position = 0;
for (int i = 1; i original.length; i++) {
if (Integer.parseInt(original[i][4]) flag) {
flag = Integer.parseInt(original[i][4]);
position = i;
}
}
System.out.print("最高分同学的姓名和总分为:");
System.out.println(original[position][0] + " " + original[position][4]);
}
public void SearchLowestScore(String[][] original) {
int flag = Integer.parseInt(original[0][4]), position = 0;
for (int i = 1; i original.length; i++) {
if (Integer.parseInt(original[i][4]) flag) {
flag = Integer.parseInt(original[i][4]);
position = i;
}
}
System.out.print("最低分同学的姓名和总分为:");
System.out.println(original[position][0] + " " + original[position][4]);
}
public void OrderStudents(String[][] original) {
String[][] result = original;
String temp;
for (int i = 1; i result.length; i++) {
for (int j = 0; j result.length - i; j++) {
if (Integer.parseInt(result[j][4]) Integer
.parseInt(result[j + 1][4])) {
temp = result[j][4];
result[j][4] = result[j + 1][4];
result[j + 1][4] = temp;
temp = result[j][0];
result[j][0] = result[j + 1][0];
result[j + 1][0] = temp;
}
}
}
System.out.println("排名\t" + "姓名\t" + "总分\t");
for (int i = 0; i STUDENTS_NUM; i++) {
System.out.println(i + 1 + "\t" + result[i][0] + "\t"
+ result[i][4]);
}
}
public void ScoreUnder60(String[][] original) {
System.out.println("平均分在60以下的学生的姓名为:");
ArrayList temp = new ArrayList();
for (int i = 0; i original.length; i++) {
if (Integer.parseInt(original[i][4]) / SUBJECTS_NUM 60) {
temp.add(original[i][0]);
}
}
System.out.print(temp + " ");
System.out.println("\n" + "平均分在60以下的学生的人数为:"
+ temp.size());
}
public Integer SearchScore(String student_name) {
int position = -1;
for (int i = 0; i data.length; i++) {
if (student_name.equals(data[i][0])) {
position = i;
}
}
if (position != -1)
return Integer.valueOf(data[position][4]);
else
return null;
}
public void Operator(String order) {
if ("1".equals(order)) {
SearchHighestScore(data);
SearchLowestScore(data);
} else if ("2".equals(order)) {
OrderStudents(data);
} else if ("3".equals(order)) {
ScoreUnder60(data);
} else if ("4".equals(order)) {
String name;
System.out.println("请输入学生的姓名:");
Scanner s = new Scanner(System.in);
name = s.nextLine();
if (SearchScore(name) != null)
System.out.println(name + "的总分为:" + SearchScore(name));
else
System.out.println("查无此人");
} else if ("5".equals(order)) {
Test obj = new Test();
obj.end_flag = false;
System.out.println("退出");
} else {
System.out.println("输入有误!");
}
}
public void InputData() {
for (int i = 1; i = STUDENTS_NUM; i++) {
System.out.print("请输入第" + i + "个人的姓名" + ":");
Scanner s1 = new Scanner(System.in);
name = s1.nextLine();
data[i - 1][0] = name;
System.out.print("请输入第" + i + "个人的语文成绩" + ":");
Scanner s2 = new Scanner(System.in);
Chinese_score = s2.nextInt();
data[i - 1][1] = String.valueOf(Chinese_score);
System.out.print("请输入第" + i + "个人的数学成绩" + ":");
Scanner s3 = new Scanner(System.in);
Math_score = s3.nextInt();
data[i - 1][2] = String.valueOf(Math_score);
System.out.print("请输入第" + i + "个人的英语成绩" + ":");
Scanner s4 = new Scanner(System.in);
English_score = s4.nextInt();
data[i - 1][3] = String.valueOf(English_score);
data[i - 1][4] = String.valueOf(Chinese_score + Math_score
+ English_score);
System.out.println(data[i - 1][4]);
}
}
}
Java 8 以上(包含java8) Lambda 方式,这种方式比较简洁,代码如下:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class App20 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ListFloat scores = new ArrayList();
System.out.println("请输入10个人的成绩:");
for (int i = 0; i 10; i++) {
scores.add(scanner.nextFloat());
}
float max = scores.stream().max((x, y) - Float.compare(x, y)).get();
System.out.println("最高分:" + max);
float min = scores.stream().min((x, y) - Float.compare(x, y)).get();
System.out.println("最低分:" + min);
float average = scores.stream().reduce(0.0f, Float::sum) / scores.size();
System.out.println("平均分:" + average);
scores.sort((x, y) - Float.compare(x, y));
System.out.println("从小到大分数排序:");
scores.forEach(s - System.out.print(s + " "));
long n = scores.stream().filter(r - r average).count();
System.out.println("\n高于平均分的有" + n + "个");
}
}
传统方式,如下:
import java.util.Arrays;
public class App21 {
public static void main(String[] args) {
float[] scores = new float[] { 10f, 9f, 8f, 7f, 6f, 5f, 4f, 3f, 2f, 1f };
float max = Float.MIN_VALUE;
for(float score : scores) {
if (score max) {
max = score;
}
}
System.out.println("最高分:" + max);
float min = Float.MAX_VALUE;
for(float score : scores) {
if (score min) {
min = score;
}
}
System.out.println("最低分:" + min);
float sum = 0;
for(float score : scores) {
sum += score;
}
float average = sum / scores.length;
System.out.println("平均分:" + average);
// 从小到大排序
Arrays.sort(scores);
System.out.println("从小到大分数排序:");
for(float score : scores) {
System.out.print(score + " ");
}
int n = 0;
for(float score : scores) {
if (score average)
n++;
}
System.out.println("\n高于平均分的有" + n + "个");
}
}
public static void main(String[] args) {
double scores[] = new double[5];
double total = 0;
double avg = 0;
double max = 0;
double min = 0;
int count=0;
String inputStr=null;
System.out.println("请输入5名学生的成绩:");
Scanner input = new Scanner(System.in);
while(count5){
try{
if(count 5){
System.out.println("请输入第"+(count+1)+"个分数:");
}
inputStr=input.nextLine();
scores[count++]=Double.valueOf(inputStr.trim());
}catch(Exception e){
if(inputStr!=null "exit".equals(inputStr.trim())){
System.out.println("您已成功结束程序");
System.exit(0);
}
System.out.println("若想结束请输入:exit");
System.out.print("您输入的分数不是数值类型,");
count--;
}
}
input.close();
Arrays.sort(scores);
min=scores[0];
max=scores[scores.length-1];
for(double score :scores){
total += score;
}
avg=total/scores.length;
System.out.println("总成绩是" + total);
System.out.println("最高分是" + max);
System.out.println("最低分是" + min);
System.out.println("平均分是" + avg);
}
//-------------------------------------------------------------------------
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while(true){
Double[] scores = null;
double total = 0;
double avg = 0;
double max = 0;
double min = 0;
int count=1;
ListDouble inputScores=new ArrayListDouble();
String inputStr=null;
System.out.println("请输入要统计学生的成绩(理论上可以输入无限个,前提是你有那么大的内存):");
while(true){
try{
System.out.println("请输入第"+count+++"个分数,或输入ok进行计算,离开请输入exit");
inputStr=input.nextLine();
inputScores.add((double)Double.valueOf(inputStr.trim()));
}catch(Exception e){
if(inputStr!=null "exit".equals(inputStr.trim().toLowerCase())){
System.out.println("您已成功结束程序");
input.close();
System.exit(0);
}
if(inputStr!=null "ok".equals(inputStr.trim().toLowerCase())){
break;
}
System.out.println("您输入的分数不是数值类型,");
System.out.println("若想结束请输入exit ,若想计算结果请输入ok");
count--;
}
}
if(inputScores.size()==0){
System.out.println("您没有输入学生成绩,无数据可统计,程序结束。");
return ;
}
scores=inputScores.toArray(new Double[inputScores.size()]);
Arrays.sort(scores);
min=scores[0];
max=scores[scores.length-1];
for(double score :scores){
total += score;
}
avg=total/scores.length;
System.out.println("总成绩是" + total);
System.out.println("最高分是" + max);
System.out.println("最低分是" + min);
System.out.println("平均分是" + avg);
}
}