我不明白为什么 array[0] 和 [1] 变成了 null。

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

I do not understand why array[0] and [1] became null

问题

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";

        while (true) {

            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("Body: ");
                Main[i - 1][1] = s.next();
            } else if (Answer == 2) {
                for (int k = 2; k < i; k++) {
                    System.out.println(k - 1 + Main[k][0]);
                }
            }
        }

    }
}

数组 Main 在第一个循环后为什么会变成空值?当我运行程序一次时,一切正常,但在第二个循环中,“创建新的笔记”和“查看你的笔记”都变为空值。

英文:
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;;
while(true) {
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;Body: &quot;);
Main[i - 1][1] = s.next();
} else if (Answer == 2) {
for (int k = 2; k &lt; i; k++) {
System.out.println(k - 1 + Main[k][0]);
}
}
}
}
}

why did array main become null after one while loop?? When I run the program once everything is perfect, but in the second loop "create new note" and "view your notes", both become null.

答案1

得分: 0

Main = new String[i][2];

在这里,您创建了一个新的数组

如果您想要调整数组的大小,您有两个选择

  1. 要么使用 java.util.ArrayList 或者 java.util.Vector 替代原生数组
  2. 要么创建一个新数组并将旧数据复制到新数组中
英文:
Main = new String[i][2];

Here you created a new Array

If you want to Resize the array you have 2 options

  1. Either use java.util.ArrayList or java.util.Vector instead of native array
  2. Or Create a new array and copy old data into new array

答案2

得分: 0

请尝试这段代码,我做了一些小的调整:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);
        int Answer;
        int i = 1;

        System.out.print("\n请输入您的姓名:");
        String Name = s.nextLine();
        System.out.println("欢迎来到Note-It," + Name + ",希望您会喜欢我们的应用。");

        String[][] Main = new String[10][2];
        Main[0][0] = "创建新笔记";
        Main[0][1] = "查看我的笔记";

        while (true) {

            System.out.println("\n请选择要进行的操作:\n");

            for (int n = 0; n < 2; n++) {
                System.out.println((n + 1) + ") " + Main[0][n]);
            }

            System.out.print("\n请输入您的选择:");
            Answer = s.nextInt();
            s.nextLine();

            if (Answer == 1) {
                i++;

                System.out.print("\n标题:");
                Main[i - 1][0] = s.nextLine();
                System.out.print("内容:");
                Main[i - 1][1] = s.nextLine();
            } else if (Answer == 2) {
                for (int k = 1; k < i; k++) {
                    System.out.println(k + ") " + Main[k][0]);
                }
            }
        }

    }
}

可能有一些元素不符合您的要求,请根据您的偏好进行更改。我还修复了内容跳过的问题。

英文:

Try this code made some minor adjustments

import java.util.Scanner;
public class Main {
public static void main(String[]args) {
Scanner s = new Scanner(System.in);
int Answer;
int i=1;
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[10][2];
//String[] Main1 = new String[2];
Main[0][0]=&quot;Create new Note&quot;;
Main[0][1]=&quot;View My Notes&quot;;
/**for( int j=0;j&lt;2;j++ )
Main[j]= new String[2];*/
while(true) {
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[0][n]);
}
System.out.print(&quot;\nPlease enter your response: &quot;);
Answer = s.nextInt();
s.nextLine();
if (Answer == 1) {
i++;
System.out.print(&quot;\nTitle: &quot;);
Main[i - 1][0] = s.nextLine();
System.out.print(&quot;Body: &quot;);
Main[i - 1][1] = s.nextLine();
} else if (Answer == 2) {
for (int k = 1; k &lt;i; k++) {
System.out.println(1 + Main[k][0]);
}
}
}
}
}

There might be some elements not upto your requirement please change them according to your preferences. Also have fixed the skipping of body.

答案3

得分: -1

首先,了解一下二维数组的工作原理。你已经声明了 new String[2][2]。然后它的工作方式是 [0][0],[0][1],[1][0],[1][1]

然后,检查一下赋值的部分。

英文:

First, check how the 2D array works. You have declared new String[2][2]. Then it works [0][0], [0][1],[1][0],[1][1].

Then check with you value assigning points.

huangapple
  • 本文由 发表于 2020年9月11日 12:14:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63840693.html
匿名

发表评论

匿名网友

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

确定