Java不会为第一个变量接受用户输入。

huangapple go评论90阅读模式
英文:

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(&quot;\nPlease Enter your Name: &quot;);
String Name = s.nextLine();
System.out.println(&quot;Welcome to Note-It &quot;+Name+&quot;, We hope you&#39;ll enjoy our application. &quot;);
String[][] Main = new String[2][2];
Main[0][0]=&quot;Create new Note&quot;;
Main[1][0]=&quot;View My Notes&quot;;
System.out.println(&quot;\nPlease select what to do: \n&quot;);
for(int n=0; n&lt;2; n++){
System.out.println((n+1)+&quot;) &quot;+Main[n][0]);
}
System.out.print(&quot;\nPlease enter your response: &quot;);
Answer = s.nextInt();
if(Answer == 1){
i++;
Main = new String[i][2];
System.out.print(&quot;\nTitle: &quot;);
Main[i-1][0]=s.nextLine();
System.out.print(&quot;\nBody: &quot;);
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(&quot;\nPlease Enter your Name: &quot;);
String Name = s.nextLine();
System.out.println(&quot;Welcome to Note-It &quot;+Name+&quot;, We hope you&#39;ll enjoy our application. &quot;);
String[][] Main = new String[2][2];
Main[0][0]=&quot;Create new Note&quot;;
Main[1][0]=&quot;View My Notes&quot;;
System.out.println(&quot;\nPlease select what to do: \n&quot;);
for(int n=0; n&lt;2; n++){
System.out.println((n+1)+&quot;) &quot;+Main[n][0]);
}
System.out.print(&quot;\nPlease enter your response: &quot;);
Answer = s.nextInt();
if(Answer == 1){
i++;
Main = new String[i][2];
System.out.print(&quot;\nTitle: &quot;);
Main[i-1][0]=s.next();
System.out.print(&quot;\nBody: &quot;);
Main[i-1][1]=s.next();
}

huangapple
  • 本文由 发表于 2020年9月10日 17:27:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63826735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定