【发布时间】:2015-11-04 02:27:04
【问题描述】:
这将是我在这里的第一个问题,我经常使用网站,但我第一次没有找到答案。我尝试将网上找到的一些代码移植到 java,最初是用 python 编写的,这是我第一次使用那些带有可变参数的“花哨”循环和函数,所以我遇到了一些与这些主题相关的问题。
我也知道标题不好的问题,但我真的不知道如何命名它。
我有这样的课:
public class Keyboard {
Keyboard(String... rows){
double y=0;
for(String row : rows){
double x=0;
/** For-each loop is applicable for arrays and Iterable.
* String is not an array. It holds array of characters but it is not an array itself.
*String does not implement interface Iterable too.
*If you want to use for-each loop for iteration over characters into the string you have to say:
*/
for(char letter : row.toCharArray()){
if(letter!=' '){
letter : new Point2D.Double(x,y);
letter : System.out.println("Punkt " + letter + " x: " + x + " y: " + y);
//to print it out like in python sauce, i guess I would have to overwrite tostring()
}
x+=0.5;
}
y++;
}
}
然后像这样主要:
public class test {
public static void main(String[] args){
Keyboard qwerty = new Keyboard("Q W E R T Y U I O P",
" A S D F G H J K L ",
" Z X C V B N M ");
Point W = qwerty['W']; //this line is wrong
}
我知道键盘在这种情况下需要 3 个字符串作为参数,依此类推,但我不知道如何,嗯,从键盘中创建的特定字母引用。
【问题讨论】: