[英]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 。
changeDay()
changeDistance()
and changeLocation()
methods changeDay()
changeDistance()
和changeLocation()
方法实现自己的逻辑 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.