为什么我的java程序运行不出来,只能输入第一个学生成绩,不能输入第二个成绩
packagetest;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.Inpu...
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class one {
public static void main(String[] args) throws IOException {
int k,count = 5;
double score[]=new double [count];
double doubleSum = 0.0,doubleAver = 0.0, maxScore=0.0;
boolean contiGo = true ;
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
for(k = 0; k < count; k++){
while(contiGo){
System.out.print("请输入第" + (k + 1) + "个学生的成绩:");
String str = buf.readLine();
}
doubleSum += score[k];
if (score[k]>maxScore) maxScore = score [k];
}
doubleAver = doubleSum / count;
System.out.println("这"+count+"个同学的平均成绩是:"+doubleAver);
// TODO Auto-generated method stub
}
} 展开
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class one {
public static void main(String[] args) throws IOException {
int k,count = 5;
double score[]=new double [count];
double doubleSum = 0.0,doubleAver = 0.0, maxScore=0.0;
boolean contiGo = true ;
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
for(k = 0; k < count; k++){
while(contiGo){
System.out.print("请输入第" + (k + 1) + "个学生的成绩:");
String str = buf.readLine();
}
doubleSum += score[k];
if (score[k]>maxScore) maxScore = score [k];
}
doubleAver = doubleSum / count;
System.out.println("这"+count+"个同学的平均成绩是:"+doubleAver);
// TODO Auto-generated method stub
}
} 展开
1个回答
展开全部
您好,这样的:
import java.util.Scanner;
public class Student{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生的人数....");
int num = sc.nextInt();
int[] arr = new int[num];
double[] chengji_arr = new double[num]; //存放成绩的
String[] String_arr = new String[num]; //存放姓名的
String chengjis = "";
String names = "";
for(int i = 0; i < arr.length && i < String_arr.length && i < chengji_arr.length; i++){
arr[i] = i;
int s = 0;
Students st = new Students();
System.out.println("请输入第"+(arr[i]+1)+"个学生的序号!");
st.setId(sc.nextInt());
System.out.println("请输入第"+(arr[i]+1)+"个学生的姓名!");
st.setName(sc.next());
System.out.println("请输入第"+(arr[i]+1)+"个学生的成绩!");
st.setChengji(sc.nextDouble());
String_arr[i] = st.getName(); //保存成绩
chengji_arr[i] = st.getChengji();
}
int max = 0;
int tmp = 0;
for (int i = 0; i < chengji_arr.length; i++) {
max = i;
for (int j = i + 1; j < chengji_arr.length; j++) {
if (chengji_arr[max] < chengji_arr[j])
max = j;// 记下较大数位置,再次比较,直到最大
}
if (i != max) {
tmp = (int)chengji_arr[i];
chengji_arr[i] = chengji_arr[max];
chengji_arr[max] = tmp;
}
}
for (int i = 0; i < chengji_arr.length; i++)
System.out.print("成绩为:"+chengji_arr[i] + " ");
}
}
class Students{
private int id;
private String name;
private double chengji;
public void setId(int id){
this.id = id;
}
public int getId(){
return id;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setChengji(double chengji){
this.chengji = chengji;
}
public double getChengji(){
return chengji;
}
}
//代码没有交换学生数组下标。
import java.util.Scanner;
public class Student{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生的人数....");
int num = sc.nextInt();
int[] arr = new int[num];
double[] chengji_arr = new double[num]; //存放成绩的
String[] String_arr = new String[num]; //存放姓名的
String chengjis = "";
String names = "";
for(int i = 0; i < arr.length && i < String_arr.length && i < chengji_arr.length; i++){
arr[i] = i;
int s = 0;
Students st = new Students();
System.out.println("请输入第"+(arr[i]+1)+"个学生的序号!");
st.setId(sc.nextInt());
System.out.println("请输入第"+(arr[i]+1)+"个学生的姓名!");
st.setName(sc.next());
System.out.println("请输入第"+(arr[i]+1)+"个学生的成绩!");
st.setChengji(sc.nextDouble());
String_arr[i] = st.getName(); //保存成绩
chengji_arr[i] = st.getChengji();
}
int max = 0;
int tmp = 0;
for (int i = 0; i < chengji_arr.length; i++) {
max = i;
for (int j = i + 1; j < chengji_arr.length; j++) {
if (chengji_arr[max] < chengji_arr[j])
max = j;// 记下较大数位置,再次比较,直到最大
}
if (i != max) {
tmp = (int)chengji_arr[i];
chengji_arr[i] = chengji_arr[max];
chengji_arr[max] = tmp;
}
}
for (int i = 0; i < chengji_arr.length; i++)
System.out.print("成绩为:"+chengji_arr[i] + " ");
}
}
class Students{
private int id;
private String name;
private double chengji;
public void setId(int id){
this.id = id;
}
public int getId(){
return id;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setChengji(double chengji){
this.chengji = chengji;
}
public double getChengji(){
return chengji;
}
}
//代码没有交换学生数组下标。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询