如何将一维字符串数组放入循环中?
How to put a 1d string Array into a loop?
我在第一个、第二个、第三个...条目中输入名称时遇到问题.. 例如,我的代码输出结果如下:
Ouput:
Input Name 1 : Input student hw score 1 : 2
Input student test score 1 : 3
Input finaltest score 1 : 2
所以问题是我无法在循环中输入任何名称,所以我该如何解决这个问题?
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Contoh : ");
int noOfRows;
int noOfColumns;
int[][] noOfArrays;
String names[];
System.out.print("Input no of students : ");
noOfRows = sc.nextInt();
noOfColumns = noOfRows;
//////////////////////////////////
names = new String[noOfRows];
///////////////////////
noOfArrays = new int[noOfRows][noOfColumns];
System.out.println("====================");
for(int i = 0 ; i<noOfRows ;i++)
{
System.out.print("Name "+(i+1)+" : ");
names[i] = sc.nextLine();
System.out.print("Input students hw score "+(i+1)+" : ");
noOfArrays[i][0] = sc.nextInt();
System.out.print("Input student test score "+(i+1)+" : ");
noOfArrays[i][1] = sc.nextInt();
System.out.print("Input finaltest score "+(i+1)+" : ");
noOfArrays[i][2] = sc.nextInt();
}
}
改变names[i] = sc.nextLine();
---->names[i] = sc.next();
这会起作用。
此外,
您的程序仅适用于 3 人以上的学生。
我没有这么深的 java 知识,这是我的(可能不是很好的)工作示例。只需添加 sc.nextLine()
,省略 eol。
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Contoh : ");
int noOfRows;
int noOfColumns;
int[][] noOfArrays;
System.out.print("Input no of students : ");
noOfRows = sc.nextInt();
noOfColumns = noOfRows;
//////////////////////////////////
String names[] = new String[noOfRows];
;
///////////////////////
noOfArrays = new int[noOfRows][noOfColumns];
System.out.println("====================");
for(int i = 0 ; i<noOfRows ;i++)
{
sc.nextLine();
System.out.print("Name "+(i+1)+" :");
names[i] = sc.nextLine();
System.out.print("Input students hw score "+(i+1)+" : ");
noOfArrays[i][0] = sc.nextInt();
System.out.print("Input student test score "+(i+1)+" : ");
noOfArrays[i][1] = sc.nextInt();
System.out.print("Input finaltest score "+(i+1)+" : ");
noOfArrays[i][2] = sc.nextInt();
}
}
我在第一个、第二个、第三个...条目中输入名称时遇到问题.. 例如,我的代码输出结果如下:
Ouput:
Input Name 1 : Input student hw score 1 : 2
Input student test score 1 : 3
Input finaltest score 1 : 2
所以问题是我无法在循环中输入任何名称,所以我该如何解决这个问题?
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Contoh : ");
int noOfRows;
int noOfColumns;
int[][] noOfArrays;
String names[];
System.out.print("Input no of students : ");
noOfRows = sc.nextInt();
noOfColumns = noOfRows;
//////////////////////////////////
names = new String[noOfRows];
///////////////////////
noOfArrays = new int[noOfRows][noOfColumns];
System.out.println("====================");
for(int i = 0 ; i<noOfRows ;i++)
{
System.out.print("Name "+(i+1)+" : ");
names[i] = sc.nextLine();
System.out.print("Input students hw score "+(i+1)+" : ");
noOfArrays[i][0] = sc.nextInt();
System.out.print("Input student test score "+(i+1)+" : ");
noOfArrays[i][1] = sc.nextInt();
System.out.print("Input finaltest score "+(i+1)+" : ");
noOfArrays[i][2] = sc.nextInt();
}
}
改变names[i] = sc.nextLine();
---->names[i] = sc.next();
这会起作用。
此外,
您的程序仅适用于 3 人以上的学生。
我没有这么深的 java 知识,这是我的(可能不是很好的)工作示例。只需添加 sc.nextLine()
,省略 eol。
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Contoh : ");
int noOfRows;
int noOfColumns;
int[][] noOfArrays;
System.out.print("Input no of students : ");
noOfRows = sc.nextInt();
noOfColumns = noOfRows;
//////////////////////////////////
String names[] = new String[noOfRows];
;
///////////////////////
noOfArrays = new int[noOfRows][noOfColumns];
System.out.println("====================");
for(int i = 0 ; i<noOfRows ;i++)
{
sc.nextLine();
System.out.print("Name "+(i+1)+" :");
names[i] = sc.nextLine();
System.out.print("Input students hw score "+(i+1)+" : ");
noOfArrays[i][0] = sc.nextInt();
System.out.print("Input student test score "+(i+1)+" : ");
noOfArrays[i][1] = sc.nextInt();
System.out.print("Input finaltest score "+(i+1)+" : ");
noOfArrays[i][2] = sc.nextInt();
}
}