首页 > 解决方案 > Java:累积评分系统不起作用(从预设值中添加/减去而不是比较 Java 中的字符串)

问题描述

因此,我制作了一个程序,该程序接受输入来确定汽车是否可以以随机生成的距离和乘客进行旅行,但我无法让我的评分系统正常工作。我试图让我的分数系统根据人们的回答方式增加/减少。(我只有汽车的颜色和汽车的型号 if/else 语句编码)我得到了一个关于比较字符串的问题的链接,这不是我在这里想要做的,我正在尝试增加或将“totalScore”变量减少 1 或 2。我还希望“totalScore”在最后累积,这样它会说明汽车是否能够继续旅行(必须为 10 或更高)我的问题是我无法获得“totalScore” 变量增加或减少,我想知道如何使它增加或减少。抱歉第一次没有说清楚。

Main.java 文件:

import java.util.*;
public class Main {
   public static void main(String[] args) {
       Random randRdt = new Random();
       Scanner userIn = new Scanner(System.in);
       int totalScore = (10);
       int score = (totalScore);
       boolean posOfOh = (false);
       System.out.println("Travel project start");
       System.out.println("Enter your car's color: ");
       String given_color = userIn.next();
       System.out.println("Enter your car's model: ");
       String given_model = userIn.next();
       System.out.println("Enter the year it was made: ");
       int given_year = userIn.nextInt();
       System.out.println("Enter your car's mpg (miles per galon): ");
       double given_mpg = userIn.nextDouble();
       System.out.println("Enter your car's max fuel (galons): ");
       double given_fuel_level = userIn.nextDouble();
       System.out.println("Enter your car's max cappacity: ");
       int given_max_occupants = userIn.nextInt();
       System.out.println("Enter true or false depending on whether or not your car has apple play: ");
       boolean given_has_music = userIn.nextBoolean();
       Car carObj = new Car(given_color, given_model, given_year, given_mpg, given_fuel_level, given_max_occupants, given_has_music);
       int randOcc = randRdt.nextInt(11);
       System.out.println("Number of people joining the trip: " + randOcc);
       double randDist = randRdt.nextInt(751);
       System.out.println("Distance you will be traveling (miles): " + randDist);
       if (given_color == "black") {
         totalScore = score - 1;
         posOfOh = (true);
       } else if (given_color == "white") {
         totalScore = score + 1;
         posOfOh = (false);
       } else {
         totalScore = (score);
       }
       if (given_model == "tesla") {
         posOfOh = (false);
         totalScore = score + 2;
       } else if (given_model == "volkswagon") {
         totalScore = score - 2;
       } else {
         totalScore = (score);
       }
       System.out.println(totalScore);
   }
}

汽车.java 文件:

class Car {
  String color;
  String model;
  int year;
  double mpg;
  double max_fuel; 
  int max_occupants;
  boolean has_music;
  public Car(String given_color, String given_model, int given_year, double given_mpg, double given_fuel_level, int given_max_occupants, boolean given_has_music)
  {
    color = given_color;
    model = given_model;
    year = given_year;
    mpg = given_mpg;
    max_fuel = given_fuel_level;
    max_occupants = given_max_occupants;
    has_music = given_has_music;
    }
    public double getMax_fuel() {
      return max_fuel;
    }
    public double getMpg() {
      return mpg;
    }
    public boolean isHas_music() {
      return has_music;
    }
    public int getMax_occupants() {
        return max_occupants;
    }
    public String getColor() {
      return color;
    }
    public int getYear() {
      return year;
    }
    public String getModel() {
      return model;
    }
  }

请帮助我几乎不知道我在做什么。

标签: java

解决方案


推荐阅读