0
我是java的初學者。雖然做這個簡單的程序(該程序很長,請不要介意)我發現:將nextLIne()與nextInt()一起使用
這是行不通的。
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);
}
}
但是這樣做。nextLine()和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);
}
}
這是爲什麼?它不是基本相同嗎?還是有其他方法可以做到這一點?