【发布时间】:2020-11-02 23:47:03
【问题描述】:
我正在创建一个名为 MySize 的双精度数组,用于保存用户输入。
如何通过使用 if 语句将数组全部填充为零来清除数组?
我尝试创建一个名为 ClearMySize 的函数,以允许用户清除他们创建的整个数组并将其填充为零
package trashmysize;
import java.util.Arrays;
import java.util.Scanner;
public class TrashMySize {
static double [][] MySize;
static int b1 = 0;
static int b2 = 0;
static Scanner scanSize = new Scanner(System.in);
static int columns = 0;
static int rows = 0;
static int NumberOfElements;
static void PrintMySize(){
for (int r = 0; r < rows; r++) {
for (int c = 0; c < columns; c++) {
System.out.print(MySize[r][c] + " ");
}
System.out.println();
}
}
static void BuildMySize(){
System.out.println("Enter the number of columns: ");
rows = Integer.parseInt(scanSize.nextLine());
System.out.println("Enter the number of rows: ");
columns = Integer.parseInt(scanSize.nextLine());
MySize = new double[rows][columns];
for (int r = 0; r < rows; r++) {
for (int c = 0; c < columns; c++) {
System.out.print(MySize[r][c] + " ");
}
System.out.println();
}
Menu();
}
static void UserEntryMySize(){
int blue = 0;
double value = 0;
while(blue < 1){
System.out.println("Enter value: ");
value = Double.parseDouble(scanSize.nextLine());
System.out.println("What row: ");
b1 = Integer.parseInt(scanSize.nextLine()) -1;
System.out.println("What column: ");
b2 = Integer.parseInt(scanSize.nextLine()) -1;
try{
if(b1 <= rows-1 && b2 <= columns-1){
System.out.println("Good position");
}
blue++;
}
catch(Exception e){
System.out.println(e.getMessage());
System.out.println("Not a good position");
blue++;
BuildMySize();
}
}
MySize[b1][b2] = value;
PrintMySize();
Menu();
}
static void ClearMySize(){
if(MySize != null){
//for (int i = 0; i < MySize.length; i++)
//MySize[i] = 0;
Arrays.fill(MySize, 0);
} else {
System.out.println("Array is empty");
}
System.out.println("\nArray has been cleared: " + Arrays.toString(MySize));
PrintMySize();
Menu();
}
static public void Menu(){
int choice = 0;
Scanner myScan = new Scanner(System.in);
System.out.println("\nMain Menu\n"
+ "\n1. Build MySize"
+ "\n2. Add to MySize"
+ "\n3. Clear MySize"
+ "\nMake a choice: ");
choice = Integer.parseInt(myScan.nextLine());
switch(choice){
case 1 -> BuildMySize();
case 2 -> UserEntryMySize();
case 3 -> ClearMySize();
case 4 -> Menu();
case 5 -> {
System.out.println("Thank you for playing! Have a good day!");
System.exit(0);
}
}
}
public static void main(String[] args) {
Menu();
}
}
但这不起作用。它似乎使我的代码崩溃。 我不断收到以下错误消息:
Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer
at java.base/java.util.Arrays.fill(Arrays.java:3429)
at trashmysize.TrashMySize.ClearMySize(TrashMySize.java:107)
at trashmysize.TrashMySize.Menu(TrashMySize.java:136)
at trashmysize.TrashMySize.UserEntryMySize(TrashMySize.java:91)
at trashmysize.TrashMySize.Menu(TrashMySize.java:135)
at trashmysize.TrashMySize.BuildMySize(TrashMySize.java:51)
at trashmysize.TrashMySize.Menu(TrashMySize.java:134)
at trashmysize.TrashMySize.main(TrashMySize.java:145)
C:\Users\Amanda\Documents\NetBeansProjects\trashMySize\nbproject\build-impl.xml:1330: The following error occurred while executing this line:
C:\Users\Amanda\Documents\NetBeansProjects\trashMySize\nbproject\build-impl.xml:936: Java returned: 1
BUILD FAILED (total time: 15 seconds)
除了我的清晰功能之外,一切都按原样工作。
【问题讨论】:
-
什么是MySize,一个数组或数组的大小?
-
java.util.Arrays.fill( array, 0 );docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/… -
“它不起作用。它似乎让我的代码崩溃了。” – 那么,你得到了什么错误信息?
-
MySize 是数组的名称。我正在尝试设置一个名为 ClearMySize 的函数,该函数将清除用户输入到数组中的数据。 clear 函数必须在 if 语句中。
标签: java arrays if-statement