public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 0
String ZodiacName = ""
Scanner sc = new Scanner(System.in)
System.out.println("请输入属相,输入值为1-12,1:鼠 ,2:牛,3:虎 ,4:兔 ,5:龙 ,6:蛇 ,7:马 ,8:羊 ,9:猴 ,10:鸡 ,11:狗 ,12:猪 ")
try {
a = sc.nextInt()
} catch (Exception e) {
// TODO: handle exception
System.out.println("对不起,请按照提示输入!")
}
switch (a) {
case 1:
ZodiacName = "鼠"
break
case 2:
ZodiacName = "牛"
break
case 3:
ZodiacName = "虎"
break
case 4:
ZodiacName = "兔"
break
case 5:
ZodiacName = "龙"
break
case 6:
ZodiacName = "蛇"
break
case 7:
ZodiacName = "马"
break
case 8:
ZodiacName = "羊"
break
case 9:
ZodiacName = "猴"
break
case 10:
ZodiacName = "鸡"
break
case 11:
ZodiacName = "狗"
break
case 12:
ZodiacName = "猪"
break
default:
ZodiacName = "没找到对应属相,请检查您是否按照提示输入!"
break
}
System.out.println("您的属相是:"+ZodiacName)
}
}
public class TTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
char a = 0
String ZodiacName = ""
Scanner sc = new Scanner(System.in)
System.out.println("请输入属相:")
a = sc.next().charAt(0)//接受String类型截取转换为char类型
switch (a) {
case '鼠':
ZodiacName = "鼠"
break
case '牛':
ZodiacName = "牛"
break
case '虎':
ZodiacName = "虎"
break
case '兔':
ZodiacName = "兔"
break
case '龙':
ZodiacName = "龙"
break
case '蛇':
ZodiacName = "蛇"
break
case '马':
ZodiacName = "马"
break
case '羊':
ZodiacName = "羊"
break
case '猴':
ZodiacName = "猴"
break
case '鸡':
ZodiacName = "鸡"
break
case '狗':
ZodiacName = "狗"
break
case '猪':
ZodiacName = "猪"
break
default:
ZodiacName = "没找到对应属相,请检查您是否按照提示输入!"
break
}
System.out.println("您的属相是:"+ZodiacName)
}
}
提起输入年份输出属相c语言,大家都知道,有人问关于c语言 已知1972年属鼠,输入一个四位的整数(1000-2999之间)代表年份,显示这一年属相是什么?另外,还有人想问C语言编程: 已知2015年是羊年,编程实现,输入任意年份,输出该年属相。(使用switch,你知道这是怎么回事?其实输入年份输出属相用c语言while语句,下面就一起来看看编写程序,输入一个年份,判断该年属相.(提示switch-case),希望能够帮助到大家!
输入年份输出属相c语言
int main(){
char p[12][4]={“鼠”,”牛”,”虎”,”兔”,”龙”,”蛇”,”马”,”羊”,”鸡”,”猴”,”狗”,”猪”}
int year0= //年是鼠年,作为基准
int year,n
printf(“请输入一个年份n”)
scanf(“%d”,&year)
n=year-year0
n=n%12
if (n<0) n+=12 //求余时,n可能是负数,如果是负数则加12
printf(“%d年属%sn”,year,p[n])
关于c语言 已知年属鼠,输入一个四位的整数(-之间)代表年份,显示这一年属相是什么?
//c++的
#include
#include
usingnamespacestd
intmain()//year用来记录输入的年份
intyear
cin>>year
//生肖相同的年份对于模12同余
//所以建立一个生肖数组
stringZodiacSigns[12]={“Rat”,”Ox”,”Tiger”,”Hare”,”Dragon”,”Snake”,”Horse”,”Sheep”,”Monkey”,”Cock”,”Dog”,”Boar”}
//(year-)对模12取余,即对应于数组中相应的生肖
//此处为防止(year-)为负数,给year加了
//其实加、等也都是可以的,只要是不小于的12的倍数即可
cout<<ZodiacSigns[((year+-)%12)]
return0
C语言编程: 已知年是羊年,编程实现,输入任意年份,输出该年属相。(使用switch
int main() int year
printf(“请输入年份:”)
scanf(“%d”,&year)
输入年份输出属相用c语言while语句year=(year-)%12
if(year<0) year+=12
switch(year)
{
case 0:printf(“今年是羊年!n”)break
case 1:printf(“今年是猴年!n”)break
case 2:printf(“今年年!n”)break
case 3:printf(“今年是狗年!n”)break
case 4:printf(“今年是猪年!n”)break
case 5:printf(“今年是鼠年!n”)break
case 6:printf(“今年是牛年!n”)break
case 7:printf(“今年是虎年!n”)break
case 8:printf(“今年是兔年!n”)break
case 9:printf(“今年是龙年!n”)break
case 10:printf(“今年是蛇年!n”)break
case 11:printf(“今年是马年!n”)break
}
return 0
以上就是与编写程序,输入一个年份,判断该年属相.(提示switch-case)相关内容,是关于关于c语言 已知1972年属鼠,输入一个四位的整数(1000-2999之间)代表年份,显示这一年属相是什么?的分享。看完输入年份输出属相c语言后,希望这对大家有所帮助!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)