RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
输入性别的java代码 性别的代码怎么编写

java输入男输出0,输入女输出1

public static void main(String[] args){

成都创新互联专注于富川网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供富川营销型网站建设,富川网站制作、富川网页设计、富川网站官网定制、重庆小程序开发公司服务,打造富川网络公司原创品牌,更为您提供富川网站排名全网营销落地服务。

Scanner input = new Scanner(System.in);

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

String inputsex = input.nextLine();

int number = getSexNumber(inputsex); /////////////看这

System.out.println(number);

}

}

用 java编译段代码,写人体每天摄人热量的计算,输入身高体重性别年龄来计算,公式百度里有,求

import java.util.Scanner;

public class Tt {

/** 计算公式:

 *  男:[66 + 1.38 x 体重(kg) + 5 x 高度(cm) - 6.8 x 年龄] x 活动量 

 * 女:[65.5 + 9.6 x 体重(kg) + l.9 x 高度(cm) - 4.7 x 年龄] x 活动量 

 */

private static double actRadio = 1.2;//活动量 

public static void main (String[] args){

System.out.println("计算人体每天摄入热量");

System.out.println("请输入性别/体重(KG)/身高(CM)/年龄,例如:男/60/170/25");

Scanner sca = new Scanner(System.in);

String  input = sca.nextLine();

while(!"exit".equalsIgnoreCase(input)){

double heat = calcHeat(input);

if(heat==-1){

System.out.println("输入格式不正确,请重新输入!");

Scanner sc = new Scanner(System.in);

input = sc.nextLine();

continue;

}else{

System.out.println("所需热量为:"+heat+"(Kcal)");

System.out.println("请继续输入:");

Scanner sc = new Scanner(System.in);

input = sc.nextLine();

}

}

private static double calcHeat(String input){

double heat = -1;

try {

String[] ss = input.split("/");

if(ss[0].equals("男")){

heat = (66+1.38*Integer.parseInt(ss[1])+ 5*Integer.parseInt(ss[2])+6.8*Integer.parseInt(ss[3]))*actRadio;

}else if(ss[0].equals("女")){

heat = (65.5+9.6*Integer.parseInt(ss[1])+ 1.9*Integer.parseInt(ss[2])+4.7*Integer.parseInt(ss[3]))*actRadio;

}else {

throw new Exception();

}

}catch (Exception e){

return -1;

}

return heat;

}

}

求java代码 。题目是,创建一个Person类,成员变量为姓名,性别,年龄,使用键盘赋值,并显

public class Person {

String name;

String sex;

int age;

public static void main(String[]args) throws IOException{

Person p1=new Person();

Person p2=new Person();

//输入p1的信息

System.out.println("请输入姓名:");

p1.name=System.in.read();

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

while(1)

{

p1.sex=System.in.read();

if(p1.sex=="man")

break;

else

System.out.println("性别输入有误,请重新输入!");

}

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

while(1)

{

p1.age=System.in.read();

if(p1.age=0p1.age=120)

break;

else

System.out.println("年龄输入有误,请重新输入!");

}

//输出第一个人的信息

System.out.println("第一个人的姓名:"+p1.name+"\n性别"+p1.sex+"\n年龄:"+p1.age);

//输入p2的信息

System.out.println("请输入姓名:");

p2.name=System.in.read();

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

while(1)

{

p2.sex=System.in.read();

if(p2.sex=="female")

break;

else

System.out.println("性别输入有误,请重新输入!");

}

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

while(1)

{

p2.age=System.in.read();

if(p2.age=0p2.age=120)

break;

else

System.out.println("年龄输入有误,请重新输入!");

}

//输出第二个人的信息

System.out.println("第二个人的姓名:"+p2.name+"\n性别"+p2.sex+"\n年龄:"+p2.age);

}

}

java程序,列出性别菜单项,根据用户的输入值判断用户性别(1代表男,2代表女)输出对应提示信息,

public class Example{

public static void main(String[] args){

Scanner input=new Scanner(System.in);

//显示菜单

System.out.println("请选择性别:1-男  2-女");

try{

int sex=input.nextInt();

if(sex==1){

System.out.println("您选择了男!");

}else if(sex==2){

System.out.println("您选择了女!");

}else{

System.out.println("只能选择1和2!");

}

}catch(Exception e){

System.out.println("菜单选择有误,只能输入数字:"+e.getMessage());

}

}

}

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

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

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用switch语句实现下述功能

按照你的要求编写的Java程序如下(注意要用JDK1.8及以上的支持switch变量为字符串的JDK编译)

import java.util.Scanner;

public class AA {

public static void main(String[] args) {

System.out.print("请输入性别代码(m or f):");

Scanner sc=new Scanner(System.in);

String input=sc.next();

switch(input){

case "m":System.out.println("男性");break;

case "f":System.out.println("女性");break;

default:System.out.println("未知");break;

}

}

}

运行结果

请输入性别代码(m or f):m

男性


名称栏目:输入性别的java代码 性别的代码怎么编写
转载来源:http://cqwzjz.cn/article/docpsci.html