我制作了一个名为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
我刚刚更新了我用来获取最重的汽车的代码。这是在我的“主”和我的第二个“为” –