//用Scanner讀入字符串,加個(gè)判斷就可以了
成都創(chuàng)新互聯(lián)主營(yíng)尚志網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶APP開發(fā)公司,尚志h5微信小程序搭建,尚志網(wǎng)站營(yíng)銷推廣歡迎尚志等地區(qū)企業(yè)咨詢
public?static?void?main(String[]?args)?{
Scanner?s=new?Scanner(System.in);
String?sex=s.next();
if(sex.toLowerCase().equals("m"))
System.out.println("男性");
else?if(sex.toLowerCase().equals("n"))
System.out.println("女性");
else
System.out.println("未知");
}
package?test;
import?java.util.Scanner;??//?Import?the?Scanner?class
public?class?Test{
public?static?void?main(String[]?args)?{
Scanner?myObj?=?new?Scanner(System.in);??//?Create?a?Scanner?object
System.out.print("輸入年齡:");
String?age=?myObj.nextLine();??//?Read?user?input
System.out.print("輸入姓名:");
String?name=?myObj.nextLine();??//?Read?user?input
//再寫?地點(diǎn)。。。
System.out.println(name+"?你好,今年"?+?age+"歲");??//?Output?user?input
}}
實(shí)現(xiàn)代碼如下:
Student類:
public class Student {
private String name;
private String sex;
private int age;
private double chinese;
private double math;
private double english;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getChinese() {
return chinese;
}
public void setChinese(double chinese) {
this.chinese = chinese;
}
public double getMath() {
return math;
}
public void setMath(double math) {
this.math = math;
}
public double getEnglish() {
return english;
}
public void setEnglish(double english) {
this.english = english;
}
}
-----------------------------------------------------------------
StudentTest類:(測(cè)試類)
import java.util.Scanner;
public class StudentTest {
public static void main(String[] args) {
Student student = new Student();
Scanner sc = new Scanner(System.in);
System.out.println("請(qǐng)輸入姓名:");
student.setName(sc.next());
System.out.println("請(qǐng)輸入性別:");
student.setSex(sc.next());
System.out.println("請(qǐng)輸入年齡:");
student.setAge(sc.nextInt());
System.out.println("請(qǐng)輸入語文成績(jī)、數(shù)學(xué)成績(jī)、英語成績(jī):");
student.setChinese(sc.nextDouble());
student.setMath(sc.nextDouble());
student.setEnglish(sc.nextDouble());
Double count = student.getChinese()+ student.getMath()+student.getEnglish();
System.out.println("姓名:"+student.getName()+" 性別:"+student.getSex()+" 年齡:"+student.getAge());
System.out.println("總分:"+count+" 平均分:"+count/3);
}
}
運(yùn)行結(jié)果為: