我製作了一個名爲Car的類,該類應該類似於一輛真實汽車的一些功能。我試圖讓最重的汽車和最小的汽車發動機的號碼。爲什麼我使用「我」作爲我的數組索引後得到1?
正如我在下面的代碼所示,你可以看到我已經使用索引進行驗證,然後將其與「i」進行比較。但是,無論輸入什麼號碼,在兩次驗證中都會得到「1」。
什麼可能是錯的?我應該得到汽車的數量而不是「1」。
這是我代碼來獲得最重的車:
int weight = x[i].getweight();
if(weight > max) {
maxweight = weight;
maxIndex = i;
}
我車類:
public class Car
{
private String color;
private int weight;
private int state;
private int fuel;
private int Maxspeed ;
private int engine;
public Car() {
this.color = "White";
this.weight = 1000;
this.state = 0;
this.fuel =0;
this.Maxspeed = 0;
this.engine = 0;
}
public Car (String color, int weight,
int state, int fuel, int Maxspeed
) {
this.color = color;
this.weight = weight;
this.state = state;
this.fuel = fuel;
this.Maxspeed = Maxspeed;
}
public String getColor() { return this.color; }
public int getweight() { return this.weight; }
public int getstate() { return this.state; }
public int getfuel() { return this.fuel; }
public int getMaxspeed() { return this.Maxspeed; }
public int getengine() { return this.engine; }
public void setColor(String color){
this.color = color;
}
public void setweight(int weight){
this.weight = weight;
}
public void setstate(int state){
this.state = state;
}
public void setfuel(int fuel){
this.fuel = fuel;
}
public void setMaxspeed(int Maxspeed){
this.Maxspeed = Maxspeed;
}
public void setengine(int engine){
this.engine = engine;
}
public void showdata() {
System.out.println("\nCar's color is: " + this.getColor());
System.out.println("Car's weight is: " + this.getweight());
System.out.println("State: " + this.getstate());
System.out.println("Fuel: " + this.getfuel());
System.out.println("Max speed: " + this.getMaxspeed());
}
public void accelerate(int speed){
if(this.getstate() == 0 || this.getstate() == 3 ||
this.getstate() == 4 || this.getMaxspeed() < speed)
{
System.out.println("\nCar cannot accelerate...");
}
else {
System.out.println("\nCar is accelerating...");
this.setfuel(this.getfuel()-2);
this.setstate(2);
if(this.getfuel() <= 0) {
this.setstate(4);
}
}
}
public void crash() {
this.setstate(3);
System.out.println("\nCrash!!!");
}
public void stop() {
this.setstate(1);
System.out.println("\nCar has stopped.");
}
public void addfuel(int fuel) {
if(this.getstate() == 0 || this.getstate() == 4){
this.setfuel(this.getfuel()+ fuel);
}
else {
System.out.println("You can't add fuel.");
}
}
public void repair() {
if(this.getstate() == 3){
this.setstate(1);
System.out.println("The car has been repaired");
}
else{
System.out.println("The car is not broken");
}
}
}
我主要:
import java.util.Scanner;
public class aaa {
public static void main (String args []) {
Car x[] = new Car[2];
int keep=1;
int counter = 0;
int counter_stopped = 0;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int maxIndex = 0;
int maxweight = 0;
int index_engine = 0;
int min_engine = 0;
Scanner input = new Scanner(System.in);
for(int i = 0; i < x.length; i++) {
String color;
int weight;
int fuel;
int Maxspeed;
int engine;
x[i] = new Car();
System.out.print("\nEnter car color " + (i + 1) + ": ");
color = input.next();
System.out.print("Enter car weight " + (i + 1) + ": ");
weight = input.nextInt();
System.out.print("Enter car fuel " + (i + 1) + ": ");
fuel = input.nextInt();
System.out.print("Enter car max speed " + (i + 1) + ": ");
Maxspeed = input.nextInt();
System.out.print("Enter car engine weight " + (i + 1) + ": ");
engine = input.nextInt();
x[i].setColor(color);
x[i].setweight(weight);
x[i].getstate();
x[i].setfuel(fuel);
x[i].setMaxspeed(Maxspeed);
x[i].setengine(engine);
}
for(int i = 0; i < x.length; i++) {
int state;
System.out.print("\nEnter car state " + (i + 1) + ": ");
state = input.nextInt();
x[i].setstate(state);
while(state > 4 || state < 0){
System.out.print("state not valid.\nTry again: ");
state = input.nextInt();
x[i].setstate(state);
}
do {
keep = menu();
switch(keep) {
case 1:
accelerate(x[i]);
break;
case 2:
stop(x[i]);
break;
case 3:
crash(x[i]);
break;
case 4:
addfuel(x[i]);
break;
case 5:
repair(x[i]);
break;
case 6:
x[i].showdata();
}
} while(keep != 7);
if(x[i].getstate() == 4 || x[i].getfuel() <= 0){
counter += 1;
}
if(x[i].getstate() == 1){
counter_stopped += 1;
}
int weight = x[i].getweight();
if(weight > max){
maxweight = weight;
maxIndex = i;
}
int weightengine = x[i].getengine();
if(weightengine < min){
min_engine = weightengine;
index_engine = i;
}
}
System.out.println("\nSUMMARY");
System.out.println("Amount of cars with no fuel: " + counter);
System.out.println("Amount of stopped cars: " + counter_stopped);
System.out.println("Heaviest car: " + maxIndex);
System.out.println("Car with the smallest engine: " + index_engine);
System.out.println("=============================================");
}
public static int menu() {
int option = 0;
Scanner s = new Scanner(System.in);
System.out.println("\n1. Accelerate Car ");
System.out.println("2. Stop Car ");
System.out.println("3. Crash Car ");
System.out.println("4. Add fuel ");
System.out.println("5. Repair ");
System.out.println("6. Show data ");
System.out.println("7. Exit ");
System.out.println("=============================================");
System.out.print("Choose an option : ");
option = s.nextInt();
System.out.println("=============================================");
return option;
}
public static void accelerate(Car myCar){
Scanner input = new Scanner(System.in);
int s;
System.out.print("Enter speed: ");
s = input.nextInt();
myCar.accelerate(s);
//myCar.showdata();
}
public static void stop(Car myCar){
myCar.stop();
}
public static void crash(Car myCar){
myCar.crash();
}
public static void addfuel(Car myCar){
int fuel;
Scanner input = new Scanner(System.in);
System.out.print("Amount to add: ");
fuel = input.nextInt();
myCar.addfuel(fuel);
}
public static void repair(Car myCar){
myCar.repair();
}
}
現在,當我編譯並測試哪個引擎或汽車更小或最重時,我會得到數字1。
你有什麼已經試圖調試這個問題? –
對不起,但我不知道你在問什麼。我不知道哪個代碼是顯示錯誤輸出的「驗證」,而且您的程序對我來說太大了,無法通過它來找出您正在討論的內容。請具體告訴我們什麼行顯示錯誤的輸出。 – ajb
我剛剛更新了我用來獲取最重的汽車的代碼。這是在我的「主」和我的第二個「爲」 –