我正在嘗試在 for 回圈中初始化一個物件陣列,每個物件都具有來自控制臺的不同值,我有一個 for 回圈來執行此操作,一切正常,直到我看到物件,所有物件都有第二個輸入的值,我嘗試了很多不同的東西,但我看不出有什么問題,我今年 17 歲,完全是初學者,我不是以英語為母語的人,如果難以理解,請見諒,抱歉,如果解決方案很明顯。請幫幫我,謝謝。
這是我的主要課程代碼。
import java.util.Scanner;
public class StockDriver {
static Stocks stockObj[];
static int NumStocks;
static String Symbol;
static String ComName;
static int Shares;
public static void main(String[] args) throws Exception {
Scanner cin = new Scanner(System.in);
System.out.println("How many stocks do you want to create?");
//Stocks input
try { //Try-Catch to handle Imput Mismatch Exeption
NumStocks = cin.nextInt();
cin.nextLine();
System.out.println("You entered: " NumStocks);
} catch (Exception e) {
System.out.println("That is an improper value. The program has been stoped.");
cin.close();
System.err.println(e "in Stock");
}
System.out.println("You will now enter in the company symbol, name and their share quantity. The price will be randomly generated.");
//Initialitation of stock objects
stockObj = new Stocks[NumStocks];
//For loop to ask for the imput of all stock, forced to a maximum of 10 stocks
for(int i = 0; i < NumStocks && i < 10; i ){
stockObj[i] = new Stocks(Symbol,ComName,Shares);
//Symbol input
System.out.println("Please enter the three character symbol: ");
try { //Try-Catch to handle Imput Mismatch Exeption
Symbol = cin.nextLine();
} catch (Exception e) {
System.out.println("That is an improper value. The program has been stoped.");
cin.close();
System.err.println(e "in Symbol");
}
//Company Name input
System.out.println("Please enter the conpany name:");
try { //Try-Catch to handle Imput Mismatch Exeption
ComName = cin.nextLine();
} catch (Exception e) {
System.out.println("That is an improper value. The program has been stoped.");
cin.close();
System.err.println(e "in Name");
}
//Number of Shares input
System.out.println("Please enter the total shares:");
try { //Try-Catch to handle Input Mismatch Exeption
Shares = cin.nextInt();
cin.nextLine();
} catch (Exception e) {
System.out.println("That is an improper value. The program has been stoped.");
cin.close();
System.err.println(e "in Shares");
}
// stockObj[i] = new Stocks(Symbol,ComName,Shares);
}
for( int i = 0; i < NumStocks;i ){
System.out.println("this is " i);
System.out.println(stockObj[i].getSymbol());
}
}
}
這是我的課程。
public class Stocks {
private static String Symbol;
private static String ComName;
private static int Shares;
private static double price;
Stocks(String symbol, String comName, int shares) {
Symbol = symbol;
ComName = comName;
Shares = shares;
}
//Method to get symbol
public String getSymbol(){
return Symbol;
}
//Method to get Company Name
public String getComName(){
return ComName;
}
//Method to get the random price between $1.00 and $200.99
public double randomPrice(){
double price = (Math.random() * (200.99 - 1.00)) 1.00;
Stocks.price = price;
return price;
}
//Method to get Shares
public int getShares(){
return Shares;
}
//Method to get the total value of the stock
public double totalValue(){
return price * Shares;
}
}
在下一個 for 回圈中,我要求三個輸入之前輸入的次數,我試圖做的是在回圈的第一次存盤輸入,并在回圈再次請求輸入之前創建一個具有此輸入的物件并且輸入發生變化。我已經驗證并且程式確實存盤了不同的輸入,所以這不是問題。問題是,當查看每個物件具有相同的值時,盡管有不同的輸入,存盤在所有物件中的輸入是最后一個或最后一個之前的一個。我想了解正在發生的事情并知道一種將不同輸入存盤在物件中的方法,非常感謝您,很抱歉長時間閱讀。
for(int i = 0; i < NumStocks && i < 10; i ){
//stockObj[i] = new Stocks(Symbol,ComName,Shares);
//Symbol input
System.out.println("Please enter the three character symbol: ");
try { //Try-Catch to handle Imput Mismatch Exeption
Symbol = cin.nextLine();
} catch (Exception e) {
System.out.println("That is an improper value. The program has been stoped.");
cin.close();
System.err.println(e "in Symbol");
}
//Company Name input
System.out.println("Please enter the conpany name:");
try { //Try-Catch to handle Imput Mismatch Exeption
ComName = cin.nextLine();
} catch (Exception e) {
System.out.println("That is an improper value. The program has been stoped.");
cin.close();
System.err.println(e "in Name");
}
//Number of Shares input
System.out.println("Please enter the total shares:");
try { //Try-Catch to handle Input Mismatch Exeption
Shares = cin.nextInt();
cin.nextLine();
} catch (Exception e) {
System.out.println("That is an improper value. The program has been stoped.");
cin.close();
System.err.println(e "in Shares");
}
stockObj[i] = new Stocks(Symbol,ComName,Shares);
}
uj5u.com熱心網友回復:
class 中的所有欄位Stocks都是static,也就是說,它們存在于每個類中——而不是每個實體,因此在創建它們時,所有實體都共享相同的值。
要解決此問題,只需洗掉static修飾符,并將欄位名稱更改camelCase為符合 Java 命名約定:
public class Stocks {
private String symbol;
private String comName;
private int shares;
private double price;
// ...
// update getters/setters/constructor according to the updated field names
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/411943.html
標籤:
上一篇:根據級別列印出物件陣列
下一篇:記憶體泄漏,我不知道為什么
