如何写Java程序来接受用户输入的一个1-12之间的整数,用switch语句输出对应月份的天数?
2个回答
展开全部
import java.util.Scanner;
public class QuestionOne {
private static boolean start;
public static void main(String[] args) {
boolean really=true;
Scanner sc=new Scanner(System.in);
while(really=true){
boolean temp=true;
int num=0;
start:
while(temp==true){
System.out.print("请输入要查询的月份:");
num=sc.nextInt();
if(num>12 ||num<1){
System.out.println("你输入的月份错误,请重新输入");
continue;
}
temp=false;
}
int dayOfMonth = 0;
switch (num) {
case 1:
dayOfMonth=31;
break;
case 2:
dayOfMonth=28;
break;
case 3:
dayOfMonth=31;
break;
case 4:
dayOfMonth=30;
break;
case 5:
dayOfMonth=31;
break;
case 6:
dayOfMonth=30;
break;
case 7:
dayOfMonth=31;
break;
case 8:
dayOfMonth=31;
break;
case 9:
dayOfMonth=30;
break;
case 10:
dayOfMonth=31;
break;
case 11:
dayOfMonth=30;
break;
case 12:
dayOfMonth=31;
break;
default:
break;
}
if(num==2){
System.out.println("'"+num+"'月对应的天数是:'"+dayOfMonth+"'或者29");
}
else{
System.out.println("'"+num+"'月对应的天数是:'"+dayOfMonth+"'");
}
System.out.println("C为继续,其余任意键退出!");
String t=sc.next();
if(t!="c"){
really=false;
}
}
}
}//(楼主如果需要判断闰年的话给发个悄悄话,这是为2月准备的)
public class QuestionOne {
private static boolean start;
public static void main(String[] args) {
boolean really=true;
Scanner sc=new Scanner(System.in);
while(really=true){
boolean temp=true;
int num=0;
start:
while(temp==true){
System.out.print("请输入要查询的月份:");
num=sc.nextInt();
if(num>12 ||num<1){
System.out.println("你输入的月份错误,请重新输入");
continue;
}
temp=false;
}
int dayOfMonth = 0;
switch (num) {
case 1:
dayOfMonth=31;
break;
case 2:
dayOfMonth=28;
break;
case 3:
dayOfMonth=31;
break;
case 4:
dayOfMonth=30;
break;
case 5:
dayOfMonth=31;
break;
case 6:
dayOfMonth=30;
break;
case 7:
dayOfMonth=31;
break;
case 8:
dayOfMonth=31;
break;
case 9:
dayOfMonth=30;
break;
case 10:
dayOfMonth=31;
break;
case 11:
dayOfMonth=30;
break;
case 12:
dayOfMonth=31;
break;
default:
break;
}
if(num==2){
System.out.println("'"+num+"'月对应的天数是:'"+dayOfMonth+"'或者29");
}
else{
System.out.println("'"+num+"'月对应的天数是:'"+dayOfMonth+"'");
}
System.out.println("C为继续,其余任意键退出!");
String t=sc.next();
if(t!="c"){
really=false;
}
}
}
}//(楼主如果需要判断闰年的话给发个悄悄话,这是为2月准备的)
展开全部
import java.util.Scanner;
class Switch{
public void contains(int temp){
switch(temp){
case 1:{
System.out.println(""+temp+"月为31天!!!");
break;
}
case 2:{
System.out.println(""+temp+"月为28天!!!");
break;
}
case 3:{
System.out.println(""+temp+"月为31天!!!");
break;
}
case 4:{
System.out.println(""+temp+"月为30天!!!");
break;
}
case 5:{
System.out.println(""+temp+"月为31天!!!");
break;
}
case 6:{
System.out.println(""+temp+"月为30天!!!");
break;
}
case 7:{
System.out.println(""+temp+"月为31天!!!");
break;
}
case 8:{
System.out.println(""+temp+"月为31天!!!");
break;
}
case 9:{
System.out.println(""+temp+"月为30天!!!");
break;
}
case 10:{
System.out.println(""+temp+"月为31天!!!");
break;
}
case 11:{
System.out.println(""+temp+"月为30天!!!");
break;
}
case 12:{
System.out.println(""+temp+"月为31天!!!");
break;
}
default:{
System.out.println("输入的月份有误!!!");
break;
}
}
}
}
public class SwitchDemo{
public static void main(String args[]){
int temp=0;
Switch sw=new Switch();
System.out.print("请输入月份:");
Scanner scan=new Scanner(System.in);
temp=scan.nextInt();
sw.contains(temp);
}
}
楼主,别忘了给分哦~~~
class Switch{
public void contains(int temp){
switch(temp){
case 1:{
System.out.println(""+temp+"月为31天!!!");
break;
}
case 2:{
System.out.println(""+temp+"月为28天!!!");
break;
}
case 3:{
System.out.println(""+temp+"月为31天!!!");
break;
}
case 4:{
System.out.println(""+temp+"月为30天!!!");
break;
}
case 5:{
System.out.println(""+temp+"月为31天!!!");
break;
}
case 6:{
System.out.println(""+temp+"月为30天!!!");
break;
}
case 7:{
System.out.println(""+temp+"月为31天!!!");
break;
}
case 8:{
System.out.println(""+temp+"月为31天!!!");
break;
}
case 9:{
System.out.println(""+temp+"月为30天!!!");
break;
}
case 10:{
System.out.println(""+temp+"月为31天!!!");
break;
}
case 11:{
System.out.println(""+temp+"月为30天!!!");
break;
}
case 12:{
System.out.println(""+temp+"月为31天!!!");
break;
}
default:{
System.out.println("输入的月份有误!!!");
break;
}
}
}
}
public class SwitchDemo{
public static void main(String args[]){
int temp=0;
Switch sw=new Switch();
System.out.print("请输入月份:");
Scanner scan=new Scanner(System.in);
temp=scan.nextInt();
sw.contains(temp);
}
}
楼主,别忘了给分哦~~~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询