请输入性别的Java代码 运行查询时提示请输入性别

java如何通过键盘输入性别代号

//用Scanner读入字符串,加个判断就可以了

成都创新互联主营尚志网站建设的网络公司,主营网站建设方案,重庆APP开发公司,尚志h5微信小程序搭建,尚志网站营销推广欢迎尚志等地区企业咨询

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("未知");

}

java程序设计,用scanner编写,输入姓名,性别,年龄,住址,婚否,可以输出相应的数据?

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

//再写 地点。。。

System.out.println(name+" 你好,今年" + age+"岁");  // Output user input

}}

用JAVA编写一个程序,要求如下:

实现代码如下:

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类:(测试类)

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("请输入姓名:");

student.setName(sc.next());

System.out.println("请输入性别:");

student.setSex(sc.next());

System.out.println("请输入年龄:");

student.setAge(sc.nextInt());

System.out.println("请输入语文成绩、数学成绩、英语成绩:");

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);

}

}

运行结果为:


本文名称:请输入性别的Java代码 运行查询时提示请输入性别
网页地址:http://pwwzsj.com/article/ddcpsdp.html