我正在编写代码,通过获取输入,在java中使用字符串显示朋友的姓名。但索引0未获取条目 import java.util.Scanner; 公共类stringclass{ 公共静态void main(字符串[]args){ 扫描仪输入=新扫描仪(System.in); System.out.print(“输入好友数:”); int a=input.nextInt(); String[]friends=新字符串[a]; 整数计数=1; 对于(int i=0;ipublicstaticvoidmain(String[]args){ 扫描仪输入=新扫描仪(System.in); System.out.print(“输入好友数:”); inta=Integer.parseInt(input.nextLine()); String[]friends=新字符串[a]; for(int i=0;i
您需要使用包含输入整数的行尾,如注释中所述我正在编写代码,通过获取输入,在java中使用字符串显示朋友的姓名。但索引0未获取条目 import java.util.Scanner; 公共类stringclass{ 公共静态void main(字符串[]args){ 扫描仪输入=新扫描仪(System.in); System.out.print(“输入好友数:”); int a=input.nextInt(); String[]friends=新字符串[a]; 整数计数=1; 对于(int i=0;ipublicstaticvoidmain(String[]args){ 扫描仪输入=新扫描仪(System.in); System.out.print(“输入好友数:”); inta=Integer.parseInt(input.nextLine()); String[]friends=新字符串[a]; for(int i=0;i,java,string,input,Java,String,Input,您需要使用包含输入整数的行尾,如注释中所述 public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the number of friends: "); int a = Integer.parseInt(input.nextLine()); String[] friends = new String[a];
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of friends: ");
int a = Integer.parseInt(input.nextLine());
String[] friends = new String[a];
for (int i = 0; i < a; i++) {
System.out.print("Enter the name of your friend " + (i + 1) + ":");
friends[i] = input.nextLine();
}
for (int j = 0; j < a; j++) {
System.out.println("Name of your friend " + (j + 1) + "is " + friends[j].toUpperCase());
}
}
import java.util.Scanner;
公共类stringclass{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.print(“输入好友数:”);
int a=input.nextInt();
//这就是你想要的
input.nextLine();
String[]friends=新字符串[a];
整数计数=1;
这是一个非常常见的问题添加input.nextLine();
inta=input.nextLine()之后;
以使用包含输入整数的行尾。这是否回答了您的问题?如果您解释更改的内容以及更改的原因会更好。仅发布准备复制粘贴的代码并不是一种很好的回答技巧
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of friends: ");
int a = Integer.parseInt(input.nextLine());
String[] friends = new String[a];
for (int i = 0; i < a; i++) {
System.out.print("Enter the name of your friend " + (i + 1) + ":");
friends[i] = input.nextLine();
}
for (int j = 0; j < a; j++) {
System.out.println("Name of your friend " + (j + 1) + "is " + friends[j].toUpperCase());
}
}
import java.util.Scanner;
public class stringclass {
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of friends: ");
int a = input.nextInt();
// This is what you want
input.nextLine();
String [] friends = new String[a];
int count=1;
for(int i=0;i<a;i++){
System.out.println("Enter the name of your friend "+count +":");
friends[i] = input.nextLine();
count++;
}
int count1=1;
for (int j=0;j<a;j++){
System.out.println("Name of your friend "+count1+ "is "+friends[j].toUpperCase());
count1++;
}
}
}