编写一个程序,根据用户输入(1~7) 输出英文星期几

2024-11-17 00:51:11
推荐回答(3个)
回答1:

C语言的:

#include

void main()

{

int num;

while(1)

{

printf("输入1-7\n");

scanf("%d",&num);

while(1)

if (1<=num<=7) break;

else printf("重新输入\n");

scanf("%d",&num);

}

switch (num)

case 1: printf("monday");break;

case 2: printf("tuesday"); break;

case 3: printf("wednesday");break;

case 4: printf("thursday"); break;

case 5: printf("friday");break;

case 6: printf("saturday"); break;

case 7: printf("sunday"); break;

}

}

}


回答2:

public String getWeek(int weekInt){
if(weekInt==1) return "星期一";
if(weekInt==2) return "星期二";
if(weekInt==3) return "星期三";
//下面依次类推,一直到星期天
return "不是1~7中的数";
}
//上面是一个函数,传入整型的数。当然在用户输入的时候你还要判断是否输入的是数字

回答3:

java 语言的

System.out.println("请输入数字1-7,然后回车...");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
switch(num)
{
case 1:
System.out.println(" Today is Monday!");
break;
case 2:
System.out.println("Today is tuesday!");
break;
case 3:
System.out.println("Today is Wensday!");
break;
case 4:
System.out.println("Today is Thursday!");
break;
case 5:
System.out.println("Today is Friday!");
break;
case 6:
System.out.println("Today is Saturday!");
break;
case 7:
System.out.println("Today is Sunday!");
break;
}