我正在尝试创建一个字符串集,其中填充了来自Hashtable的键,因此for-each循环可以遍历Set并将默认值放在Hashtable中.我仍然在学习Java,但我尝试这样做的方式并不是有效的语法.有人可以证明这样做的正确方法,并解释为什么我的方式不起作用,他们的方式.
private Hashtable<String, String> defaultConfig() {
Hashtable<String, String> tbl = new Hashtable<String, String>();
tbl.put("nginx-servers","/etc/nginx/servers");
tbl.put("fpm-servers","/etc/fpm/");
tbl.put("fpm-portavail","9001");
tbl.put("webalizer-script","/usr/local/bin/webalizer.sh");
tbl.put("sys-useradd","/sbin/useradd");
tbl.put("sys-nginx","/usr/sbin/nginx");
tbl.put("sys-fpmrc","/etc/rc.d/php_fpm");
tbl.put("www-sites","/var/www/sites/");
tbl.put("www-group","www");
return tbl;
}
//This sets missing configuration options to their defaults.
private void fixMissing(Hashtable<String, String> tbl) {
Hashtable<String, String> defaults = new Hashtable<String, String>(defaultConfig());
//The part in error is below...
Set<String> keys = new Set<String>(defaults.keySet());
for (String k : keys) {
if (!tbl.containsKey(k)) {
tbl.put(k, defaults.get(k));
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在学习Java,并坚持自我测试练习写一个向后打印字符串的递归函数...
我理解编译器错误,但我不知道该怎么办.
我的代码......
class Back {
void Backwards(String s) {
if (s.length = 0) {
System.out.println();
return;
}
System.out.print(s.charAt(s.length));
s = s.substring(0, s.length-1);
Backwards(s);
}
}
class RTest {
public static void main(String args[]) {
Back b;
b.Backwards("A STRING");
}
Run Code Online (Sandbox Code Playgroud)
}
编译器输出......
john@fekete:~/javadev$ javac Recur.java
Recur.java:3: error: cannot find symbol
if (s.length = 0) {
^
symbol: variable length
location: variable s of type String
Recur.java:7: error: cannot find symbol
System.out.print(s.charAt(s.length));
^
symbol: variable length
location: variable s of type …Run Code Online (Sandbox Code Playgroud) 我正在学习Java.
我应该编写一个程序,将所有大写字母转换为小写,全部小写转换为大写.它在书中说我只需要从大写中减去32并将32添加到小写.
这是我的代码......
class Caseconv {
public static void main(String args[])
throws java.io.IOException {
char ch;
do {
ch = (char) System.in.read();
if (ch >= 97 & ch <= 122) ch = ch - 32;
if (ch >= 65 & ch <= 90) ch = ch + 32;
System.out.print(ch);
} while (ch != '\n');
}
}
Run Code Online (Sandbox Code Playgroud)
但是编译器不希望这样做,我得到了这个错误.
Caseconv.java:13: error: possible loss of precision
if (ch >= 97 & ch <= 122) ch = ch - 32;
^
required: char
found: …Run Code Online (Sandbox Code Playgroud) 我试图使用BufferedReader从输入中读取a.它第一次工作,但第二次运行我得到一个例外.
john@fekete:~/devel/java/pricecalc$ java frontend.CUI
> gsdfgd
Invalid command!
> I/O Error getting string: java.io.IOException: Stream closed
I/O Error: java.io.IOException: java.io.IOException: Stream closed
> I/O Error getting string: java.io.IOException: Stream closed
I/O Error: java.io.IOException: java.io.IOException: Stream closed
> I/O Error getting string: java.io.IOException: Stream closed
Run Code Online (Sandbox Code Playgroud)
它只是在循环中继续运行.我一定错过了什么.
public static void main(String args[]) {
if (args.length == 0) {
while (!exit) {
try {
exit = processLine(commandLine());
} catch (IOException e) {
System.out.println("I/O Error: " + e);
}
}
System.out.println("Bye!");
} else if …Run Code Online (Sandbox Code Playgroud) 我在C++中使用指针进行向下类型转换时遇到了一些问题,在我想出这样做之前,谷歌基本上告诉我这是不可能的,而且我从C++学到的任何书都没有涉及到.我认为这会起作用......
long int TheLong=723330;
int TheInt1=0;
int TheInt2=0;
long int * pTheLong1 = &TheLong;
long int * pTheLong2 = &TheLong + 0x4;
TheInt1 = *pTheLong1;
TheInt2 = *pTheLong2;
cout << "The double is " << TheLong << " which is "
<< TheInt1 << " * " << TheInt2 << "\n";
Run Code Online (Sandbox Code Playgroud)
第五行的增量可能不正确,但输出让我担心我使用gcc 3.4.2的C编译器会自动将TheInt1转换为long int或其他东西.输出看起来像这样......
双倍是723330,即723330*4067360
TheInt1的输出不可能高,并且没有TheInt2的输出.
我有三个问题......
我是否走在正确的轨道上?
第五行的适当增量是多少?
为什么地狱是TheInt1/TheInt2允许如此大的价值?
这段代码与学习Java(Oracle Press Books)一书完全一样,但它不起作用.我不明白为什么它不起作用,它应该工作.我已经尝试过OpenJDK和Sun JDK 7,错误是一样的.
ThreadCom.java:56: error: unexpected type
if (thrd.getName().compareTo("Tick") = 0) {
^
required: variable
found: value
1 error
Run Code Online (Sandbox Code Playgroud)
有问题的代码......
class MyThread implements Runnable {
Thread thrd;
TickTock ttOb;
MyThread(String name, TickTock tt) {
thrd = new Thread(this, name);
ttOb = tt;
thrd.start();
}
public void run() {
if (thrd.getName().compareTo("Tick") = 0) { // <- that line
for (int i=0; i<5; i++) ttOb.tick(true);
ttOb.tick(false);
} else {
for (int i=0; i<5; i++) ttOb.tock(true);
ttOb.tock(false);
}
}
}
Run Code Online (Sandbox Code Playgroud)
代码与书中的完全一致.