在不添加额外的 readLine 的情况下读取字符后无法进行下一个输入
Can't take next inputs after reading a character without adding an extra readLine
必须在读取字符 "sec=(char)in.read();" 后添加额外的行 "String x=in.readLine();" 否则程序不会继续进行以获取更多输入,请参阅下面代码中的注释。请注意我不想使用扫描仪 class。
import java.io.*;
class marks
{
public static void main(String args[])
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int rl,m1,m2,m3,tot=0;
String nm,cl;
nm=cl="";
char sec='A';
double avg=0.0d;
try
{
System.out.println("Enter the information of the student");
System.out.print("roll no:");
rl=Integer.parseInt(in.readLine());
System.out.print("class:");
cl=in.readLine();
System.out.print("section:");
sec=(char)in.read();
String x=in.readLine(); /* have to add this line only then marks of 3 subject can be inputed */
System.out.println("marks of three subjects "+x);
m1=Integer.parseInt(in.readLine());
m2=Integer.parseInt(in.readLine());
m3=Integer.parseInt(in.readLine());
tot=m1+m2+m3;
avg=tot/3.0d;
System.out.println("total marks of the students = "+tot);
System.out.println("avg marks of the students = "+avg);
}
catch (Exception e)
{};
}
}
如何替换:
sec=(char)in.read();
与:
sec = in.readLine().charAt(0);
已解决
import java.io.BufferedReader;
import java.io.InputStreamReader;
class Marks {
public static void main(String args[]) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in))) {
int rl, m1, m2, m3, tot = 0;
String nm, cl;
nm = cl = "";
char sec = 'A';
double avg = 0.0d;
System.out.println("Enter the information of the student");
System.out.print("roll no:");
rl = Integer.parseInt(in.readLine());
System.out.print("class:");
cl = in.readLine();
System.out.print("section:");
sec = in.readLine().charAt(0); //changes are here, instead of
// String x = in.readLine(); /* have to add this line only then marks of 3
// subject can be inputed */
System.out.println("marks of three subjects ");
m1 = Integer.parseInt(in.readLine());
m2 = Integer.parseInt(in.readLine());
m3 = Integer.parseInt(in.readLine());
tot = m1 + m2 + m3;
avg = tot / 3.0d;
System.out.println("total marks of the students = " + tot);
System.out.println("avg marks of the students = " + avg);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output
Enter the information of the student
roll no:21
class:10
section:c
marks of three subjects
56
65
56
total marks of the students = 177
avg marks of the students = 59.0
问题是当您根据文档使用 in.read() 时:"Reads a single character," 但您实际上输入的是 'two' 个字符:一个字符和一个存储的 '\n'在 InputStreamReader 的缓冲区中,当您使用 in.readLine();
时将再次读取
必须在读取字符 "sec=(char)in.read();" 后添加额外的行 "String x=in.readLine();" 否则程序不会继续进行以获取更多输入,请参阅下面代码中的注释。请注意我不想使用扫描仪 class。
import java.io.*;
class marks
{
public static void main(String args[])
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int rl,m1,m2,m3,tot=0;
String nm,cl;
nm=cl="";
char sec='A';
double avg=0.0d;
try
{
System.out.println("Enter the information of the student");
System.out.print("roll no:");
rl=Integer.parseInt(in.readLine());
System.out.print("class:");
cl=in.readLine();
System.out.print("section:");
sec=(char)in.read();
String x=in.readLine(); /* have to add this line only then marks of 3 subject can be inputed */
System.out.println("marks of three subjects "+x);
m1=Integer.parseInt(in.readLine());
m2=Integer.parseInt(in.readLine());
m3=Integer.parseInt(in.readLine());
tot=m1+m2+m3;
avg=tot/3.0d;
System.out.println("total marks of the students = "+tot);
System.out.println("avg marks of the students = "+avg);
}
catch (Exception e)
{};
}
}
如何替换:
sec=(char)in.read();
与:
sec = in.readLine().charAt(0);
已解决
import java.io.BufferedReader;
import java.io.InputStreamReader;
class Marks {
public static void main(String args[]) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in))) {
int rl, m1, m2, m3, tot = 0;
String nm, cl;
nm = cl = "";
char sec = 'A';
double avg = 0.0d;
System.out.println("Enter the information of the student");
System.out.print("roll no:");
rl = Integer.parseInt(in.readLine());
System.out.print("class:");
cl = in.readLine();
System.out.print("section:");
sec = in.readLine().charAt(0); //changes are here, instead of
// String x = in.readLine(); /* have to add this line only then marks of 3
// subject can be inputed */
System.out.println("marks of three subjects ");
m1 = Integer.parseInt(in.readLine());
m2 = Integer.parseInt(in.readLine());
m3 = Integer.parseInt(in.readLine());
tot = m1 + m2 + m3;
avg = tot / 3.0d;
System.out.println("total marks of the students = " + tot);
System.out.println("avg marks of the students = " + avg);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output
Enter the information of the student
roll no:21
class:10
section:c
marks of three subjects
56
65
56
total marks of the students = 177
avg marks of the students = 59.0
问题是当您根据文档使用 in.read() 时:"Reads a single character," 但您实际上输入的是 'two' 个字符:一个字符和一个存储的 '\n'在 InputStreamReader 的缓冲区中,当您使用 in.readLine();
时将再次读取