英文:
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("\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]);
}
}
}
}
}
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];
在这里,您创建了一个新的数组
如果您想要调整数组的大小,您有两个选择
- 要么使用 java.util.ArrayList 或者 java.util.Vector 替代原生数组
- 要么创建一个新数组并将旧数据复制到新数组中
英文:
Main = new String[i][2];
Here you created a new Array
If you want to Resize the array you have 2 options
- Either use java.util.ArrayList or java.util.Vector instead of native array
- 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("\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[10][2];
//String[] Main1 = new String[2];
Main[0][0]="Create new Note";
Main[0][1]="View My Notes";
/**for( int j=0;j<2;j++ )
Main[j]= new String[2];*/
while(true) {
System.out.println("\nPlease select what to do: \n");
for (int n = 0; n < 2; n++) {
System.out.println((n + 1) + ") " + Main[0][n]);
}
System.out.print("\nPlease enter your response: ");
Answer = s.nextInt();
s.nextLine();
if (Answer == 1) {
i++;
System.out.print("\nTitle: ");
Main[i - 1][0] = s.nextLine();
System.out.print("Body: ");
Main[i - 1][1] = s.nextLine();
} else if (Answer == 2) {
for (int k = 1; k <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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论