java的输入语句是什么啊,请详细点,还有是不是用不同的软件有不同的写法啊,比如用eclipse或者Netbeans怎么写,
我刚接触java,希望大家不要说这个问题菜,我真得不懂,我要写的一个题目:编写一个程序,接受用户输入的1~12之间的整数,若不符合则重输入,,利用swich语句输出对应月份的天数.刚刚学java,什么都不懂,请大家给我点信心,谢谢了!
我刚接触java,希望大家不要说这个问题菜,我真得不懂,我要写的一个题目:编写一个程序,接受用户输入的1~12之间的整数,若不符合则重输入,,利用swich语句输出对应月份的天数.刚刚学java,什么都不懂,请大家给我点信心,谢谢了!
String input = sc.next();或BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
String input = br.readLine();
String input = sc.next();
String input = sc.next();
以上这个是Jdk5.0以上版本才支持的新的属性,用起来还是蛮方便的
或BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
String input = br.readLine();
下面这个就是传统的输入,不需要背啊,只要理解了怎么做就好了啊
如果你的jdk版本支持的话可以使用前面的,当然前面的Scanner也是由BuffereReader封装的
注意Scanner是JDK 5.0中加入的.
Scanner要JDK5.0和以上版本 。
public class test1 { /**
* @param args
*/
public static void main(String[] args) {
do{
System.out.println("Input the month");
Scanner sc = new Scanner(System.in);
int month = sc.nextInt();
if(month < 1||month > 12)
System.out.println("Error,Input again");// TODO Auto-generated method stub
}while(month > 1&&month < 13);
switch (month){
case 1:System.out.println("31");break;
case 3:System.out.println("31");break;
case 5:System.out.println("31");break;
case 7:System.out.println("31");break;
case 8:System.out.println("31");break;
case 10:System.out.println("31");break;
case 12:System.out.println("31");break;
case 2:System.out.println("28");break;
case 4:System.out.println("30");break;
case 6:System.out.println("31");break;
case 9:System.out.println("31");break;
case 11:System.out.println("31");break;
} }}
这是我编的这个程序,运行时无法解析month,谁帮我看看,谢谢了
不管是Eclipse还是NetBeans,只要JDK的版本相同,
方法都是一样的
int month = 0;
month = sc.nextInt(); 这样
public class month { /**
* @param args
*/
public static void main(String[] args) {
int month=0;
System.out.println("Input the month");
do{
Scanner sc =new Scanner (System.in);
month = Integer.parseInt(sc.nextLine());
if(month>12 || month<1)
{
System.out.println("Error,Input again");
}
}while(month >12|| month <1);
switch (month)
{
case 1:System.out.println("31");break;
case 3:System.out.println("31");break;
case 5:System.out.println("31");break;
case 7:System.out.println("31");break;
case 8:System.out.println("31");break;
case 10:System.out.println("31");break;
case 12:System.out.println("31");break;
case 2:System.out.println("28");break;
case 4:System.out.println("30");break;
case 6:System.out.println("30");break;
case 9:System.out.println("30");break;
case 11:System.out.println("30");break;
}
}
}
Scanner scanner = new Scanner(System.in);
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int month = -1;
while(true) {
try {
System.out.print("请输入月份:");
month = scanner.nextInt();
if(month >= 1 && month <= 12) {
break;
}
System.out.println("** 请输入正确的月份 **");
} catch (Exception e) {
System.out.println("** 格式错误!请输入数字 **");
scanner.next();
}
}
System.out.println(month + " 月份有:" + days[month - 1] + " 天");
}
}
int day=s.nextInt();
String input = sc.next(); 或 BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
String input = br.readLine();
public class test1 { /**
* @param args
*/
public static void main(String[] args) {
int month = 0;
do{
System.out.println("Input the month");
Scanner sc = new Scanner(System.in);
month = sc.nextInt();
if(month < 1 || month > 12)
System.out.println("Error,Input again");// TODO Auto-generated method stub
}while(month > 1&&month < 13);
switch (month){
case 1:System.out.println("31");break;
case 3:System.out.println("31");break;
case 5:System.out.println("31");break;
case 7:System.out.println("31");break;
case 8:System.out.println("31");break;
case 10:System.out.println("31");break;
case 12:System.out.println("31");break;
case 2:System.out.println("28");break;
case 4:System.out.println("30");break;
case 6:System.out.println("31");break;
case 9:System.out.println("31");break;
case 11:System.out.println("31");break;
} } }
这是我的代码,我运行没问题啊。
你那里有什么问题
"do{"
"}while(month > 1&&month < 13);"
即可