使用int作为GregorianCalendar的输入,但我无法继续扫描JAVA
我试图编写一个代码来输入出生日期以返回那天的星期几,我使用输入流阅读器来获取我的输入使用int作为GregorianCalendar的输入,但我无法继续扫描JAVA,java,string,Java,String,我试图编写一个代码来输入出生日期以返回那天的星期几,我使用输入流阅读器来获取我的输入 package testing; import java.io.*; import java.text.DateFormat; import java.util.*; public class theDayYouBorn { public static void main(String[] args) throws IOException { InputStreamReader is
package testing;
import java.io.*;
import java.text.DateFormat;
import java.util.*;
public class theDayYouBorn {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("Please Input the Year You Born at : ");
int year1 = br.read();
System.out.println("Thank!, Please input the Month :");
int month1 = br.read();
System.out.println("Okay, last thing Please input the day : ");
int day1 = br.read();
GregorianCalendar gc = new GregorianCalendar(year1, month1, day1);
Date d1 = gc.getTime();
DateFormat df = DateFormat.getDateInstance();
String sd = df.format(d1);
String dayName = gc.getDisplayName(gc.DAY_OF_WEEK, gc.LONG,
Locale.getDefault());
System.out.println("The Day you born in was a " + sd
+ " and the day was " + dayName);
}
}
让我只输入第一个输入,然后运行它,并且是一个随机的日期,而不要求输入日期或月份
然后我尝试使用字符串作为输入,并将它们转换为整数,这就是工作。。。我改变这一点:
System.out.println("Please Input the Year You Born at : ");
String year = br.readLine();
System.out.println("Thank!, Please input the Month :");
String preMonth = br.readLine();
System.out.println("Okay, last thing Please input the day : ");
String day = br.readLine();
int day1 = Integer.parseInt(day);
int month2 = Integer.parseInt(preMonth);
int year1 = Integer.parseInt(year);
int month1 = month2 - 1;
我试图理解为什么我不能扫描整数。如果你看一下,你会发现:
读取单个字符
返回:读取的字符,范围为0到65535(0x00-0xffff)之间的整数,如果已到达流的末尾,则为-1 因此,每次调用
read
都将返回一个char
(或者更精确地说,它在Unicode表中的数字表示形式,如'a'
是97
,或者'1'
是49
)
例如,如果用于以下代码
System.out.println("Please Input the Year You Born at : ");
System.out.println(br.read());
System.out.println(br.read());
System.out.println(br.read());
System.out.println(br.read());
System.out.println(br.read());
System.out.println(br.read());
我们将提供输入1987
Please Input the Year You Born at :
1987[here we press enter]
在Windows操作系统上,我们将以
49
57
56
55
13
10
代表
int -> char
-----------
49 -> '1'
57 -> '9'
56 -> '8'
55 -> '7'
13 -> '\r'
10 -> '\n'
在br.readLine()的情况下不存在这样的问题因为它的目的是将数据作为文本而不是二进制数据读取。请阅读此::读取单个字符。