简体   繁体   English

我如何从一个类中的整数实现if语句

[英]How do I implement an if statement from an integer within a class

I am making a program that implements a class file. 我正在制作一个实现类文件的程序。 What I want to do is use an if statement to output a string or just a specific println. 我想做的是使用if语句输出字符串或仅输出特定的println。

//Daily travel increments.
System.out.println("After one day ");
crossCountry.changeDay(1);
crossCountry.changeDistance(1);
crossCountry.changeLocation(1);
crossCountry.printStates();

What I want to do is print a string based on the "location" number. 我要做的是根据“位置”数字打印字符串。 For example, if the location is 1, then the output for printing the "location" line should be "Dallas Texas" or something to that extent. 例如,如果位置为1,则用于打印“位置”行的输出应为“达拉斯德克萨斯州”或某种程度的输出。

A full source can be found at this link. 完整的资源可以在此链接找到。

http://pastebin.com/DyK0BQZ4 http://pastebin.com/DyK0BQZ4

I'm not sure if this can be done or not but I am looking for some information on how to implement it as is. 我不确定是否可以做到这一点,但我正在寻找有关如何按原样实施的信息。 If this is not possible please let me know my options. 如果这不可能,请告诉我我的选择。

I was thinking to make it simple, considering that there is no user input, I could eliminate the Location declaration from both my main java file and my class file and just add a print statement immediately after the printStates line. 考虑到没有用户输入,我正在考虑简化操作,可以从主Java文件和类文件中都消除Location声明,而只需在printStates行之后添加一条print语句。 Although I would still like to know if there is a way to implement something else based on the location number. 尽管我仍然想知道是否有一种方法可以根据位置编号实现其他功能。

You can use a switch statement, but if you must use the if statement, then the printStates method could be something like 您可以使用switch语句,但是如果必须使用if语句,则printStates方法可能类似于

void printStates() 
{
    String locationName = "Unknown"; // or some other default location name
    if (location == 1)
    {
        locationName = "somelocation, SL";
    }
    else if (location == 2)
    {
        locationName = "thelocation, TL";       
    }
    else if (location == 3)
    {
        locationName = "otherlocation, OL";     
    }

    System.out.println("Day: " + day + "Distance Driven: " + distance +
    "Current Location: " + locationName);
}

If they are sequential integers and you want to store them in memory, you can use an array of String to do it, and you can either fill it in declaration or runtime (see how to handle arrays here ) 如果它们是顺序整数,并且想要将它们存储在内存中,则可以使用String数组来执行此操作,并且可以在声明或运行时中填充它(请参见此处的处理数组)

String[] locations = {"Dallas Texas", "Somewhere over the rainbow", "Your momma"};

You can read them by their array position (index starts at zero). 您可以通过它们的数组位置(索引从零开始)读取它们。 This way you'll avoid all of those if then else or switch . 这样,您就可以避免所有其他情况,否则将要切换 Always validate if the array position exists. 始终验证数组位置是否存在。 Assuming that locationId it's the location integer: 假设locationId是位置整数:

System.out.println(locations[locationId]); // print "Dallas Texas" 

If they're not contiguous you can use structures that index your elements by their key (in this case it's the location ID), like a HashTable . 如果它们不是连续的,则可以使用通过其键(在本例中为位置ID)为元素索引元素的结构,例如HashTable

  • You need to implement your own logic for changeDay() changeDistance() and changeLocation() methods 您需要为changeDay() changeDistance()changeLocation()方法实现自己的逻辑
  • You don't need If for each location instead use an array (or any Data structure) to index the current location 您不需要为每个位置使用If ,而是使用array (或任何Data结构)来索引当前位置

- --

public class JavaApplication33 {

  public static void main(String[] args) {

    // Create object.
    Travel crossCountry = new Travel();

    // Initial output.
    System.out.println("You decide to travel across the country.");
    System.out.println("You start your trip in Augusta, Ga.");
    System.out.println("You average 75mph and 12 hours driving per day");

    System.out.println("After " + 1 + " day ");
    crossCountry.changeDay(1);
    crossCountry.changeDistance(1);
    crossCountry.changeLocation(1);
    crossCountry.printStates();

  }
}


class Travel {

  int day = 0;
  int distance = 0;
  int location = 0;
  int increment = 900;
  String[] locations = {"Augusta", "Portland", "Kentucky", "Chicago", "NY", "Indiana"};

  void changeDay(int newValue) {
    day += newValue;

  }

  public void changeLocation(int newValue) {
    location += newValue;

  }

  public void changeDistance(int newValue) {
    distance += 75 * 12 * newValue;

  }

  void printStates() {
    System.out.println("Day: " + day + " Distance Driven: " + distance + " Current Location: "
        + locations[location]);
  }

}

switch statement 切换语句

switch (integerValue)
case 1:
  System.out.println("output 1");
  break;
case 2:
  System.out.println("output 2");
  break;
default:
  System.out.println("wat");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何将这些方法从 class 实现到主 class? - How do i implement these methods from a class into a main class? 如何从if / else if代码中将整数带入该代码外部的整数? (java) - How do I carry over an integer from within a if/else if code into an integer from outside that code? (java) 如何从 starter class 中的方法访问私有 integer(在嵌套类中声明)? - How do you access a private integer (declared in a nested class) from a method within the starter class? 如何在事件监听器的语句中调用函数? - How do I call a function from within a statement in the event listener? 如何在方法中使用其他类的整数? - How do I use an integer from an other class in a method? 如何仅接受整数输入(仅使用扫描器类和if and else语句,如果可能,则使用while循环-没有布尔值)? - How do I take only integer inputs (with only scanner class and if and else statement or while loops if possible - no booleans)? 如何打印另一个类的return语句? - How do I print a return statement from another class? 如何从同一个 package 中的 class 上使用扩展? - How do I use extends on a class from within same package? 如何在ActionListener中实现类? - How do I implement a Class into an ActionListener? 如何正确实现此类? - How do I implement this class correctly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM