我是java的初学者。在做这个简单的程序时(程序很长,请不要介意)我发现:
这不起作用。
import java.util.*;
class store
{
store()
{
System.out.println("RESLUTS OF STUDENTS");
System.out.println();
}
void stu1 (String name,int marks)
{
System.out.println("The name of student is : "+ name+" and he got :"+marks+"marks");
}
void stu2 (String name,int marks)
{
System.out.println("The name of student is : "+ name+" and he got :"+marks+"marks");
}
void stu3 (String name,int marks)
{
System.out.println("The name of student is : "+ name+" and he got :"+marks+"marks");
}
void stu4 (String name,int marks)
{
System.out.println("The name of student is : "+ name+" and he got :"+marks+"marks");
}
void stu5 (String name,int marks)
{
System.out.println("The name of student is : "+ name+" and he got :"+marks+"marks");
}
}
class stu
{
public static void main(String[] args) {
Scanner v = new Scanner (System.in);
System.out.println("Enter the name of first student");
String p = v.nextLine();
System.out.println("Enter the marks for first student");
int q = v.nextInt();
System.out.println("Enter the name of second student");
String r = v.nextLine();
System.out.println("Enter the marks for second student");
int s = v.nextInt();
System.out.println("Enter the name of third student");
String t = v.nextLine();
System.out.println("Enter the marks for third student");
int u = v.nextInt();
System.out.println("Enter the name of fourth student");
String w = v.nextLine();
System.out.println("Enter the marks for fourth student");
int x = v.nextInt();
System.out.println("Enter the name of fifth student");
String y = v.nextLine();
System.out.println("Enter the marks for fifth student");
int z = v.nextInt();
store st = new store ();
st.stu1(p,q);
st.stu2(r,s);
st.stu3(t,u);
st.stu4(w,x);
st.stu5(y,z);
}
}
但是这样做.sextLine()和nextInt()与print语句分开。
import java.util.*;
class store
{
store()
{
System.out.println("RESLUTS OF STUDENTS");
System.out.println();
}
void stu1 (String name,int marks)
{
System.out.println("The name of student is : "+ name+" and he got :"+marks+"marks");
}
void stu2 (String name,int marks)
{
System.out.println("The name of student is : "+ name+" and he got :"+marks+"marks");
}
void stu3 (String name,int marks)
{
System.out.println("The name of student is : "+ name+" and he got :"+marks+"marks");
}
void stu4 (String name,int marks)
{
System.out.println("The name of student is : "+ name+" and he got :"+marks+"marks");
}
void stu5 (String name,int marks)
{
System.out.println("The name of student is : "+ name+" and he got :"+marks+"marks");
}
}
class stu
{
public static void main(String[] args) {
Scanner v = new Scanner (System.in);
System.out.println("Enter the name of first student");
String p = v.nextLine();
System.out.println("Enter the name of second student");
String r = v.nextLine();
System.out.println("Enter the name of third student");
String t = v.nextLine();
System.out.println("Enter the name of fourth student");
String w = v.nextLine();
System.out.println("Enter the name of fifth student");
String y = v.nextLine();
System.out.println("Enter the marks for first student");
int q = v.nextInt();
System.out.println("Enter the marks for second student");
int s = v.nextInt();
System.out.println("Enter the marks for third student");
int u = v.nextInt();
System.out.println("Enter the marks for fourth student");
int x = v.nextInt();
System.out.println("Enter the marks for fifth student");
int z = v.nextInt();
store st = new store ();
st.stu1(p,q);
st.stu2(r,s);
st.stu3(t,u);
st.stu4(w,x);
st.stu5(y,z);
}
}
这是为什么?是不是基本相同?或者还有其他方法吗?
答案 0 :(得分:0)
这是因为Scanner#nextInt方法不消耗输入的最后一个换行符,因此在下次调用Scanner#nextLine
时会消耗该换行符
您的问题已在此处得到解答:Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods