4
回答
java 问题,再次晕了,求教高手。
华为云4核8G,高性能云服务器,免费试用   
有个问题,见代码的问号处!,为什么要两行 name = input.nextLine();才能实现姓名的输入
import java.util.*;
class Student{
	protected String name;																					
	protected String studentNumber;																			
	protected float englishScore;																			
	protected float mathScore;																				
	protected float computerScore;																			
	protected float totalScore;																				
	
	public Student(){																						
	}
	
	public Student(String name,String studentNumber,float englishScore,float mathScore,float computerScore){
		this.name = name;
		this.studentNumber = studentNumber;
		this.englishScore = englishScore;
		this.mathScore = mathScore;
		this.computerScore = computerScore;
		totalScore = this.englishScore+this.mathScore+this.computerScore;
	}
	
	public String getName(){																				
		return name;
	}
	
	public void setName(String aName){																	
		name=aName;
	}
	
	public String getStudentNumber(){																		
		return studentNumber;
	}
	
	public void setStudentNumber(String studentNum){
														
		studentNumber=studentNum;
	}
	
	public float getEnglishScore(){																			
		return englishScore;
	}
	
	public void setEnglishScore(float aEnglishScore){	
													
		englishScore = aEnglishScore;
		totalScore = sum();
	}
	
	public float getMathScore(){																			
		return mathScore;
	}
	
	public void setMathScore(float aMathScore){																
		mathScore = aMathScore;
		totalScore = sum();
	}
	
	public float getComputerScore(){																		
		return computerScore;
	}
	
	public void setComputerScore(float aComputerScore){	
													
		computerScore=aComputerScore;
		totalScore = sum();
	}
	
	public float getTotalScore(){																		
		return totalScore;
	}
	
	public float sum(){																						
		totalScore = englishScore+mathScore+computerScore;
		return totalScore;
		
	}
	
	public float testScore(){																				
		float averageScore = sum()/(float)3;				
		return averageScore;
	}
	
	@Override
	public boolean equals(Object x){//重写equals方法														
		if(this.getClass()!=x.getClass())
			return false;
		Student b=(Student) x;
		return ((this.getName().equals(b.getName()))&&
				(this.getStudentNumber().equals(b.getStudentNumber()))&&
				(this.getEnglishScore()==(b.getEnglishScore()))&&
				(this.getMathScore()==b.getMathScore())&&
				(this.getComputerScore()==b.getComputerScore())&&
				(this.getTotalScore()==b.getTotalScore())&&
				(this.getTotalScore()==b.getTotalScore())&&
				(this.testScore()==b.testScore()));
	}
	
	//比较两个学生的总成绩
	public static void compare(Student a,Student b){														
		if(a.totalScore>b.totalScore)
			System.out.print("大于");
		else if(a.totalScore==b.totalScore)
			System.out.print("等于");
		else
			System.out.print("小于");
	}
	
	//修改toString方法
	public String toString(){																	
		return ("\n姓名:"+name+"\n学号:"+studentNumber+"\n英语成绩:"+englishScore+"\n数学成绩:"+
				mathScore+"\n计算机成绩:"+computerScore+"\n总成绩:"+totalScore+"\n测评成绩:"+testScore()+"\n");
	}
}


class StudentXW extends Student{//Student派生的StudentXW类																			
	private String liability;																				
	
	public StudentXW(){																						
	}
	
	public StudentXW(String name,String studentNumber,float englishScore,float mathScore,float computerScoreString ,String liability){		
		super(name,studentNumber,englishScore,mathScore,computerScoreString);
		this.liability=liability;
	}
	
	public String getLiability(){																			
		return liability;
	}
	
	public void setLiability(String liability){																
		this.liability= liability;	
	}

	//修改testScore方法,计算学委测评成绩
	public float testScore(){																				
		float averageScore=sum()/3+3;				
		return averageScore;
	}

	//修改toString方法
	public String toString(){																				
		return ("\n姓名:"+name+"\n学号:"+studentNumber+"\n责任"+liability+"\n英语成绩:"+englishScore+"\n数学成绩:"+
				mathScore+"\n计算机成绩:"+computerScore+"\n总成绩:"+totalScore+"\n测评成绩:"+testScore()+"\n");
	}
	
}


class StudentBZ extends Student{//Student派生的StudentBZ类																		
	private String liability;																
	
	public StudentBZ(){																						
		super();
	}
	
	public StudentBZ(String name,String studentNumber,float englishScore,float mathScore,float computerScoreString ,String liability){																		
		super(name,studentNumber,englishScore,mathScore,computerScoreString);
		this.liability=liability;
	}
	
	public String getLiability(){																			
		return liability;
	}
	
	public void setLiability(String liability){																
		this.liability=liability;
	}

	//修改testScore方法,计算班长测评成绩
	public float testScore(){																	
		float averageScore=sum()/3+5;				
		return averageScore;
	}

	//修改toString方法
	public String toString(){																	
		return ("\n姓名:"+name+"\n学号:"+studentNumber+"\n责任"+liability+"\n英语成绩:"+englishScore+"\n数学成绩:"+
				mathScore+"\n计算机成绩:"+computerScore+"\n总成绩:"+totalScore+"\n测评成绩:"+testScore()+"\n");
	}
}


public class PerformanceEvaluationTester {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 Scanner input=new Scanner(System.in);
		 
		 float englishScore,mathScore,computerScore;
		 String name,liability;
		 String studentNumber;	
		 
		 System.out.println("请输入一个学生信息\n姓名:");
		 name = input.nextLine();
		 System.out.println("学号:");
		 studentNumber = input.nextLine();
		 System.out.println("英语成绩:");
		 englishScore = input.nextFloat();
		 System.out.println("数学成绩:");
		 mathScore = input.nextFloat();
		 System.out.println("计算机成绩:");
		 computerScore = input.nextFloat();
		 Student student = new Student(name,studentNumber,englishScore,mathScore,computerScore);
		 
		 System.out.println("请输入一个学习委员信息\n姓名:");
		 name = input.nextLine();//??????????????????????????此处为什么要两行才能实现姓名的输入??
		 name = input.nextLine();//
		 System.out.println("学号:");
		 studentNumber = input.nextLine();
		 System.out.println("责任:");
		 liability = input.nextLine();
		 System.out.println("英语成绩:");
		 englishScore = input.nextFloat();
		 System.out.println("数学成绩:");
		 mathScore = input.nextFloat();
		 System.out.println("计算机成绩:");
		 computerScore = input.nextFloat();
		 StudentXW studentxw = new StudentXW(name,studentNumber,englishScore,mathScore,computerScore,liability);
		 
		 System.out.println("请输入一个班长信息\n姓名:");
		 name = input.nextLine();// ????????????????????????同上,此处为什么要两行才能实现姓名的输入??
		 name = input.nextLine();// 
		 System.out.println("学号:");
		 studentNumber = input.nextLine();
		 System.out.println("责任:");
		 liability = input.nextLine();
		 System.out.println("英语成绩:");
		 englishScore = input.nextFloat();
		 System.out.println("数学成绩:");
		 mathScore = input.nextFloat();
		 System.out.println("计算机成绩:");
		 computerScore = input.nextFloat();
		 StudentBZ studentbz = new StudentBZ(name,studentNumber,englishScore,mathScore,computerScore,liability);
		 
		 System.out.println(student);
		 System.out.println(studentxw);
		 System.out.println(studentbz);
		 
		 System.out.print("学委总成绩");
		 Student.compare(studentxw, studentbz);
		 System.out.println("学委总成绩");
		 
		 System.out.print("学委和班长");
		 if(student.equals(studentxw))
			 System.out.println("同一");
		 else
			 System.out.println("不同一");
	}
}

举报
共有4个答案 最后回答: 7年前

第二个是接受回车的,nextLine()方法的结束符是Enter键,即nextLine()方法返回的是Enter键之前的所有字符。

你可以换成next()试试

不会啊,我测试过都没事,如果真需要两个的话,估计像一楼说的一样,一个是来接受回车的,你可以Debug看看。

不过建议使用next()就行了,而且最好在使用next()之前都加上hasNext()作判断。

public class PerformanceEvaluationTester {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner input = new Scanner(System.in);

  float englishScore, mathScore, computerScore;
  String name, liability;
  String studentNumber;

  System.out.println("请输入一个学生信息\n姓名:");
  name = input.nextLine();
  System.out.println("学号:");
  studentNumber = input.nextLine();
  System.out.println("英语成绩:");
  englishScore = input.nextFloat();
  System.out.println("数学成绩:");
  mathScore = input.nextFloat();
  System.out.println("计算机成绩:");
  computerScore = input.nextFloat();
  Student student = new Student(name, studentNumber, englishScore, mathScore, computerScore);

  //System.out.println("请输入一个学习委员信息姓名:");
  System.out.println("请输入一个学习委员信息\n姓名:");
  name = input.nextLine();//=====>注意行

  System.out.println("注意name==="+name.length);

}

把代码的class PerformanceEvaluationTester主体变成上面的样子后(其它的代码不变)运行后,注意行好像没起到要求输入的作用,而是直接跳过了!下面是我的一个运行示例!

请输入一个学生信息
姓名:
1
学号:
1
英语成绩:
1
数学成绩:
1
计算机成绩:
1
请输入一个学习委员信息
姓名:
注意name===0

前面单个1是我输入的!不知道这个怎么解释,到了那一行没让我输入,而程序就结束了(输入最后一个1后回车程序就结束了!!!而String name长度为0???

顶部