英文:
Java is not taking user input for the first variable
问题
import java.util.Scanner;
public class NoteIt {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int Answer;
int i = 2;
System.out.print("\nPlease Enter your Name: ");
String Name = s.nextLine();
System.out.println("Welcome to Note-It " + Name + ", We hope you'll enjoy our application.");
String[][] Main = new String[2][2];
Main[0][0] = "Create new Note";
Main[1][0] = "View My Notes";
System.out.println("\nPlease select what to do: \n");
for (int n = 0; n < 2; n++) {
System.out.println((n + 1) + ") " + Main[n][0]);
}
System.out.print("\nPlease enter your response: ");
Answer = s.nextInt();
s.nextLine(); // Consume the newline character left by nextInt()
if (Answer == 1) {
i++;
Main = new String[i][2];
System.out.print("\nTitle: ");
Main[i - 1][0] = s.nextLine();
System.out.print("\nBody: ");
Main[i - 1][1] = s.nextLine();
}
}
}
英文:
import java.util.Scanner;
public class NoteIt {
public static void main(String[]args) {
Scanner s = new Scanner(System.in);
int Answer;
int i=2;
System.out.print("\nPlease Enter your Name: ");
String Name = s.nextLine();
System.out.println("Welcome to Note-It "+Name+", We hope you'll enjoy our application. ");
String[][] Main = new String[2][2];
Main[0][0]="Create new Note";
Main[1][0]="View My Notes";
System.out.println("\nPlease select what to do: \n");
for(int n=0; n<2; n++){
System.out.println((n+1)+") "+Main[n][0]);
}
System.out.print("\nPlease enter your response: ");
Answer = s.nextInt();
if(Answer == 1){
i++;
Main = new String[i][2];
System.out.print("\nTitle: ");
Main[i-1][0]=s.nextLine();
System.out.print("\nBody: ");
Main[i-1][1]=s.nextLine();
}
}
}
I don't know why it is not asking for Title?
答案1
得分: 0
根据 @Rohit Jain 所说,这是因为 Scanner.nextInt 方法不会读取通过按下“Enter”键创建的输入中的换行符,所以调用 Scanner.nextLine 在读取该换行符后返回。
描述在这里:也可以查看这个问题
尝试一下,可能会有帮助...
Scanner s = new Scanner(System.in);
int Answer;
int i=2;
System.out.print("\n请输入您的姓名:");
String Name = s.nextLine();
System.out.println("欢迎来到Note-It," + Name + ",希望您会喜欢我们的应用。");
String[][] Main = new String[2][2];
Main[0][0]="创建新笔记";
Main[1][0]="查看我的笔记";
System.out.println("\n请选择要做什么:\n");
for(int n=0; n<2; n++){
System.out.println((n+1) + ") " + Main[n][0]);
}
System.out.print("\n请输入您的选择:");
Answer = s.nextInt();
if(Answer == 1){
i++;
Main = new String[i][2];
System.out.print("\n标题:");
Main[i-1][0] = s.next();
System.out.print("\n正文:");
Main[i-1][1] = s.next();
}
英文:
According to @Rohit Jain, That's because the Scanner.nextInt method does not read the newline character in your input created by hitting "Enter," and so the call to Scanner.nextLine returns after reading that newline.
Description here: check this question too
Try this it may help...
Scanner s = new Scanner(System.in);
int Answer;
int i=2;
System.out.print("\nPlease Enter your Name: ");
String Name = s.nextLine();
System.out.println("Welcome to Note-It "+Name+", We hope you'll enjoy our application. ");
String[][] Main = new String[2][2];
Main[0][0]="Create new Note";
Main[1][0]="View My Notes";
System.out.println("\nPlease select what to do: \n");
for(int n=0; n<2; n++){
System.out.println((n+1)+") "+Main[n][0]);
}
System.out.print("\nPlease enter your response: ");
Answer = s.nextInt();
if(Answer == 1){
i++;
Main = new String[i][2];
System.out.print("\nTitle: ");
Main[i-1][0]=s.next();
System.out.print("\nBody: ");
Main[i-1][1]=s.next();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论