我有一個問題,當我在 addLaptop() 方法中組合兩個陣列“laptops”和“arr2”并創建第三個陣列“newArray”以包含“laptops”和“arr2”的值然后設定“laptops”陣列等于“newArray”的值并在我的 addLaptop() 方法中列印“laptops”,“laptops”的值將等于“newArray”,就像我想要的那樣。
但是,當我嘗試從我的 printAllLaptops() 方法中從“筆記本電腦”中獲取資料時,“筆記本電腦”陣列中的值被設定回其原始值,而不是像我想要的那樣設定為“newArray”的值成為。
我的問題是什么我無法弄清楚為什么這些值不會更新?我已經在這個問題上停留了幾個小時,并嘗試將“筆記本電腦”陣列移動到我的不同方法中,并嘗試設定筆記本電腦 = newArray,還嘗試通過我的 addLaptop() 方法以幾種不同的方式回傳筆記本電腦。
呼叫我的方法的代碼:LaptopFinderApp.java
package docComments;
import java.util.Scanner;
public class LaptopFinderApp {
public static void main(String[] args) {
int loop = 0;
while (loop !=1) {
String userInput = null;
// Entering 1 is what calls the method I am having issues with
System.out.println("1. Show all laptops");
// Entering 2 is what calls the method that updates my 'laptops Array'
System.out.println("2. Add a laptop");
System.out.println("3. Find a laptop");
System.out.println("4. Delete a laptop");
System.out.println("5. Number of laptops");
System.out.println("6. Exit");
System.out.println("Enter your selection:" );
Scanner myObj = new Scanner(System.in);
userInput = myObj.nextLine(); // Read user input
System.out.println();
// Converts user input from a string to an integer
int convertedInput = Integer.parseInt(userInput);
// Handels user inputs
if (convertedInput > 6) {
System.out.println("Enter a selection 1 - 6");
} else if (convertedInput == 6) {
System.out.println("Goodbye");
break;
} else if (convertedInput == 5) {
} else if (convertedInput == 4) {
} else if (convertedInput == 3) {
} else if (convertedInput == 2) {
System.out.println("GPU:");
String cpu = myObj.nextLine();
System.out.println("CPU:");
String gpu = myObj.nextLine();
System.out.println("Battery Life:");
String batterylife = myObj.nextLine();
Laptops addLaptop = new Laptops(gpu, cpu, batterylife);
addLaptop.addLaptop();
} else if (convertedInput == 1) {
Laptops name = new Laptops(null, null, null);
name.printAllLaptops();
} else if (convertedInput < 1) {
System.out.println("Enter a selection 1 - 6");
} else {
System.out.println("Error please try again.");
}
System.out.println();
}
}
}
我的代碼就是問題所在:Laptops.java
package docComments;
import java.util.Arrays;
public class Laptops {
/**
* Needs to have GPU, CPU, Battery Life, unique id and static count as attributes.
*/
private String gpu;
private String cpu;
private String batterylife;
private int id;
private int counter;
public Laptops(String gpu, String cpu, String batterylife) {
this.gpu = gpu;
this.cpu = cpu;
this.batterylife = batterylife;
this.id = 1000003;
}
/**
* Returns the GPU of the Laptop.
* @return the GPU
*/
public String getGpu() {
return gpu;
}
/**
* Returns the CPU of the Laptop.
* @return the CPU
*/
public String getCpu() {
return cpu;
}
/**
* Returns the batterylife of the Laptop.
* @return the batterylife
*/
public String getBatteryLife() {
return batterylife;
}
/**
* Returns the user inputed id of the Laptop.
* @return the user inputed id
*/
public int getId() {
return id;
}
/**
* Returns the new id we created.
* @return the new id
*/
public int creatId() {
counter = counter 1;
id = id counter;
return id;
}
/**
* Array of laptops
*/
String[][] laptops = {
{"1000001", "RTX 3080", "Intel i7", "24h"},
{"1000002", "RTX 4090", "Intel i9", "16h"},
{"1000003", "GTX 1660", "Ryzen 5", "34h"}
};
/**
* Prints all of the laptops in our array
*/
public void printAllLaptops() {
System.out.println(Arrays.toString(laptops)); // only displays the three original laptops
for (int i = 0; i < laptops.length; i) {
System.out.println("Laptop " i ": " "ID:" laptops[i][0] " " laptops[i][1] " " laptops[i][2] " " laptops[i][3]);
}
}
/**
* Adds user created laptop to laptops array
*/
public String[][] addLaptop() {
if (gpu != null) {
String arr2[][] = {{String.valueOf(creatId()), gpu, cpu, batterylife}};
// create new array
String newArray[][] = new String[laptops.length arr2.length][];
// Copy laptops array to new array from 0 to laptops.length
System.arraycopy(laptops, 0, newArray, 0, laptops.length);
// copy second array to new array
System.arraycopy(arr2, 0, newArray, laptops.length, arr2.length);
// display all arrays
System.out.println("Array1 = " Arrays.toString(laptops[0]));
System.out.println("Array2 = " Arrays.toString(arr2[0]));
System.out.println("Merged Array = " Arrays.toString(newArray[3]));
// set old array equal to new array
laptops = newArray;
return newArray;
} else {
System.out.println("Error adding laptop to list.");
return laptops;
}
}
/**
* Prints out a string that contains GPU, CPU, battery life and id.
*/
@Override
public String toString() {
return "GPU: " gpu " CPU: " cpu " Battery Life: " batterylife " ID: " creatId();
}
}
uj5u.com熱心網友回復:
正如評論所暗示的,尚不清楚您是否希望單個筆記本電腦物件繼續更新,但如果是這種情況,那么您應該在回圈之前對其進行一次初始化。
Laptops addLaptop = new Laptops();
Scanner myObj = new Scanner(System.in);
int loop = 0;...
你應該完全洗掉這一行(回圈內的那一行,而不是回圈上方的第一行),因為它是罪魁禍首:
Laptops addLaptop = new Laptops(gpu, cpu, batterylife);
您不需要在回圈中重新初始化它,只需呼叫 addLaptop(),但將您的 3 個引數、gpu、cpu 和電池壽命傳遞給該函式,因此當您呼叫該方法時,它就像:
addLaptop.addLaptop(gpu, cpu, batterylife);
發生的事情是您不斷地用new關鍵字覆寫舊筆記本電腦。
還要注意,您的 input == 1 case 也應該移除new筆記本電腦,然后簡單地呼叫
addLaptop.printAllLaptops();
指的是您的原始筆記本電腦實體,而不是新實體。(可能想將其命名為與您的函式不同的名稱)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/481218.html
下一篇:生成非重復整數陣列
