9)输入一个整数1~7,调用一个自己定义的函数,输出星期一~星期日;(java语言)
- 提问者网友:自食苦果
- 2021-05-18 03:57
- 五星知识达人网友:孤独的牧羊人
- 2021-05-18 05:07
运行过了:
import java.util.Scanner;
public class Test
{
public void print(int date)
{
switch(date)
{
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
default:
System.out.println("星期日");
}
}
public static void main(String[] args)
{
Scanner ss = new Scanner(System.in);
try
{
int dd = Integer.parseInt(ss.next());
if(dd>7||dd<1)
{
System.out.println("您输入的数字有错误,请输入1到7的数字");
}
else
{
new Test().print(dd);
}
}
catch(Exception e)
{
System.out.println("您的输入有误,请输入1到7的数字");
}
}
}
- 1楼网友:洎扰庸人
- 2021-05-18 06:05
package test;
import java.io.*;
public class Test { public static void print() { System.out.print("Enter a Number:"); char c='0'; String day; try { c = (char) System.in.read(); } catch (IOException ex) { ex.printStackTrace(); } switch (c) { case '1': day = "星期一"; break; case '2': day = "星期二"; break; case '3': day = "星期三"; break; case '4': day = "星期四"; break; case '5': day = "星期五"; break; case '6': day = "星期六"; break; case '7': day = "星期日"; break; default: day = "没有这一天"; } System.out.println(day); }
public static void main(String[] args) { Test.print(); } }
- 2楼网友:酒者煙囻
- 2021-05-18 05:54
package test; import java.util.*;
public class test3 {
public static void main(String[] args) { // TODO 自动生成方法存根 Scanner input=new Scanner(System.in); System.out.print("请输入1-7的数字:"); int day=input.nextInt(); System.out.print("星期"+Week(day)); } public static char Week(int day) { switch(day) { case 1: return '一'; case 2: return '二'; case 3: return '三'; case 4: return '四'; case 5: return '五'; case 6: return '六'; case 7: return '日'; default: return ' '; } }
}