java 练习题 输入输出 请问第二题怎么写?
2个回答
展开全部
```今天好多人问同样的问题
import java.util.Scanner;
public class StudentDemo {
public static void main(String[] args) {
float tmp=0;
Scanner scanner = new Scanner(System.in);
Student student = new Student("WSNDNMSL","250");
System.out.print("输入语文成绩:");
student.setChinese(scanner(scanner));
System.out.print("输入数学成绩:");
student.setMath(scanner(scanner));
System.out.print("输入英语成绩:");
student.setEnglish(scanner(scanner));
print(student);
student.score();
student.average();
student.goodstudent();
scanner.close();
}
public static float scanner(Scanner scanner) {
float tmp =0;
do{
tmp = scanner.nextFloat();
}while (tmp<1 || tmp>100);
return tmp;
}
public static void print(Student student) {
System.out.println("姓名:"+student.getName());
System.out.println("学号:"+student.getId());
System.out.println("语文:"+student.getChinese());
System.out.println("数学:"+student.getMath());
System.out.println("英语:"+student.getEnglish());
}
}
public class Student {
private String name;
private String id;
private float chinese;
private float math;
private float english;
public Student() {
this.name = "张三";
this.id = "111111";
}
public Student(String name, String id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public float getChinese() {
return chinese;
}
public void setChinese(float chinese) {
this.chinese = chinese;
}
public float getMath() {
return math;
}
public void setMath(float math) {
this.math = math;
}
public float getEnglish() {
return english;
}
public void setEnglish(float english) {
this.english = english;
}
//总成绩
public void score(){
System.out.println("总成绩为:"+(this.chinese+this.math+this.english));
}
//平均成绩
public void average(){
System.out.println("平均成绩为:"+(this.chinese+this.math+this.english)/3);
}
//是否三好学生
public void goodstudent(){
if((this.chinese+this.math+this.english)/3>=90)
System.out.println("是三好学生!");
else
System.out.println("不是三好学生!");
}
}
追问
你好 我的是通过对话框输入
你还在吗????
展开全部
按照你的要求编写的Java程序如下(语文,数学和英语成绩通过输入对话框输入)
import javax.swing.JOptionPane;
class Student {
private String name;
private String id;
private float chinese;
private float math;
private float english;
public Student(){
this.name="张三";
this.id="111111";
}
public Student(String name,String id){
this.name=name;
this.id=id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public float getChinese() {
return chinese;
}
public void setChinese(float chinese) {
this.chinese = chinese;
}
public float getMath() {
return math;
}
public void setMath(float math) {
this.math = math;
}
public float getEnglish() {
return english;
}
public void setEnglish(float english) {
this.english = english;
}
public void score(){
System.out.println("总成绩:"+(this.chinese+this.math+this.english));
}
public void average(){
System.out.println("平均成绩:"+(this.chinese+this.math+this.english)/3);
}
public void goodstudent(){
if((this.chinese+this.math+this.english)/3>90){
System.out.println(this.name+"是三好学生");
}else{
System.out.println(this.name+"不是三好学生");
}
}
}
public class studentDemo {
public static void main(String[] args) {
float chinese,math,english;
String[] message={"语文","数学","英语"};
Student s=new Student("李四","111112");
chinese=input(message[0]);
s.setChinese(chinese);
math=input(message[1]);
s.setMath(math);
english=input(message[2]);
s.setEnglish(english);
System.out.println("姓名:"+s.getName()+" 学号:"+s.getId()+" 语文成绩:"+s.getChinese()+" 数学成绩:"+s.getMath()+" 英语成绩:"+s.getEnglish());
s.score();
s.average();
s.goodstudent();
}
public static float input(String message) {
float score=0;
while(score<1 || score>100){
String sScore=JOptionPane.showInputDialog(null, "请输入"+message+"成绩[1-100]:");
if(sScore==null){
}else{
score=Float.parseFloat(sScore);
}
}
return score;
}
}
运行结果
姓名:李四 学号:111112 语文成绩:88.0 数学成绩:96.0 英语成绩:92.0
总成绩:276.0
平均成绩:92.0
李四是三好学生
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询