英文:
How to Create a Global Variable with a condition
问题
import java.lang.*;
import java.util.Properties;
public class constants {
public static String driverFile = null;
public static String storageDirectory = null;
if (System.getProperty("os.name").startsWith("Windows")){
storageDirectory = "C:\\Scripts\\Price_Tracker";
driverFile = "geckodriver-v0.27.0-win64.exe";
}else{//is linux
storageDirectory = "/Price_Tracker";
driverFile = "geckodriver-v0.27.0-linux64";
}// end if(System.getProperty("os.name").startsWith("Windows"))
}//end public class constants
英文:
I want to create a global variable, but I want it to be one of two values, based on the machine's OS. I thought I could do this by writing the following code:
import java.lang.*;
import java.util.Properties;
public class constants {
public static String driverFile = null;
public static String storageDirectory = null;
if (System.getProperty("os.name").startsWith("Windows")){
storageDirectory = "C:\\Scripts\\Price_Tracker";
driverFile = "geckodriver-v0.27.0-win64.exe";
}else{//is linux
storageDirectory = "/Price_Tracker";
driverFile = "geckodriver-v0.27.0-linux64";
}// end if(System.getProperty("os.name").startsWith("Windows")
}//end public class constants
but I get an error saying " Cannot resolve symbol 'getProperty' " , is what I'm trying to do even possible?
答案1
得分: 1
public class constants {
public static String driverFile = null;
public static String storageDirectory = null;
static {
if (System.getProperty("os.name").startsWith("Windows")) {
storageDirectory = "C:\\Scripts\\Price_Tracker";
driverFile = "geckodriver-v0.27.0-win64.exe";
} else { // is linux
storageDirectory = "/Price_Tracker";
driverFile = "geckodriver-v0.27.0-linux64";
} // end if(System.getProperty("os.name").startsWith("Windows"))
}
}// end public class constants
英文:
public class constants {
public static String driverFile = null;
public static String storageDirectory = null;
static {
if (System.getProperty("os.name").startsWith("Windows")){
storageDirectory = "C:\\Scripts\\Price_Tracker";
driverFile = "geckodriver-v0.27.0-win64.exe";
}else{//is linux
storageDirectory = "/Price_Tracker";
driverFile = "geckodriver-v0.27.0-linux64";
}// end if(System.getProperty("os.name").startsWith("Windows")
}
}//end public class constants
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论