Java初学者问题!!关于数组的查询

自学大魔王 2018-07-15 11:47:19
import java.util.Scanner;


public class day6a {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("请输入学生的人数:");
int p=sc.nextInt();
String a[]=new String[p];
for(int i=0;i<a.length;i++){
System.out.println("请输入学生的姓名:");
a[i]=sc.nextLine();
}
System.out.println("请输入你要查询的学生名字:");
String as=sc.nextLine();
for(int i=0;i<a.length;i++){
if(a[i].equals(as)){
System.out.println("第"+(i+1)+"位学生是"+as);
// break;
}
}
}


}
输入学生的人数后,循环连续输出两句话但是只能输入一个数,求大佬指教
...全文
142 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
charleyqian 2018-07-15
  • 打赏
  • 举报
回复
试一下,我主要是把nextLine()换成了next()
charleyqian 2018-07-15
  • 打赏
  • 举报
回复
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生的人数:");
int p = sc.nextInt();
String a[] = new String[p];
System.out.println("请输入学生的姓名:");
for (int i = 0; i < a.length; i++) {

a[i] = sc.next();
}
System.out.println("请输入你要查询的学生名字:");
String as = sc.next();
for (int i = 0; i < a.length; i++) {
if (a[i].equals(as)) {
System.out.println("第" + (i + 1) + "位学生是" + as);
// break;
}
}

}
verejava 2018-07-15
  • 打赏
  • 举报
回复
package test;

import java.util.Scanner;

public class day6a {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生的人数:");
int p = Integer.parseInt(sc.nextLine());
String a[] = new String[p];

for (int i = 0; i < a.length; i++) {
System.out.println("请输入学生的姓名:");
Scanner scanner =new Scanner(System.in);
a[i] = scanner.nextLine();
}

System.out.println("请输入你要查询的学生名字:");
Scanner scanner =new Scanner(System.in);
String as = scanner.nextLine();

for (int i = 0; i < a.length; i++) {
if (a[i].equals(as)) {
System.out.println("第" + (i + 1) + "位学生是" + as);
// break;
}
}
}
}
写代码的陈皮 2018-07-15
  • 打赏
  • 举报
回复
写代码的陈皮 2018-07-15
  • 打赏
  • 举报
回复
再加俩个Scanner 就行了.
package test;

import java.util.Scanner;

public class day6a {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生的人数:");
int p = sc.nextInt();
String a[] = new String[p];

for (int i = 0; i < a.length; i++) {
System.out.println("请输入学生的姓名:");
Scanner scanner =new Scanner(System.in);
a[i] = scanner.nextLine();
}

System.out.println("请输入你要查询的学生名字:");
Scanner scanner =new Scanner(System.in);
String as = scanner.nextLine();

for (int i = 0; i < a.length; i++) {
if (a[i].equals(as)) {
System.out.println("第" + (i + 1) + "位学生是" + as);
// break;
}
}
}
}

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧