当我编译这段代码时:
interface Rideable {
String getGait();
}
public class Camel implements Rideable {
int x = 2;
public static void main(String[] args) {
new Camel().go(8);
}
void go(int speed) {
System.out.println((++speed * x++)
+ this.getGait());
}
String getGait() {
return " mph, lope";
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Camel.java:13: error: getGait() in Camel cannot implement getGait() in Rideable
String getGait() {
^
attempting to assign weaker access privileges; was public
1 error
Run Code Online (Sandbox Code Playgroud)
如何在接口中声明的getGait方法被公开?
我正在尝试循环包含其他数组的数组,这些数组由带空格的字符串组成.问题是我似乎无法保留字符串中的间距.如果我将IFS更改为\n,则将带有空格的字符串分成多个项目,或者如果我将IFS保持不变,则将数组的所有元素视为1项,这里是一些示例代码:
#!/bin/sh
low1=("AA QQ" "BB LL")
low2=("CC" "DD")
low3=("EE" "FF")
high=(low1 low2 low3)
for high_item in ${high[@]}
do
eval arrayz=\${$high_item[@]}
#IFS=$'\n'
for item in $arrayz
do
echo $item
done
done
Run Code Online (Sandbox Code Playgroud)
输出:
AA QQ BB LL CC DD EE FF
正如您所看到的,"AA QQ"和"BB LL"元素已被拆分.
如果我取消注释设置IFS为的行,\n我将获得以下内容:
AA QQ BB LL CC DD EE FF
现在"AA QQ"和"BB LL"连接起来了!
无论如何,我可以保留这些元素,就像它们原始的一样......我需要输出看起来像这样:
AA QQ BB LL CC DD EE FF
我有以下文件:
FILE1.TXT
###################################################
Dump stat Title information for 'ssummary' view
###################################################
Tab=> 'Instance' Title=> {text {Total instances: 7831}}
Tab=> 'Device' Title=> {text {Total spice devices: 256}}
Tab=> 'Memory' Title=> {text {Total memory allocated: 962192 kB}}
Tab=> 'Cpu' Title=> {text {Total cumulative CPU time: 9030 ms}}
Run Code Online (Sandbox Code Playgroud)
FILE2.TXT
###################################################
Dump stat Title information for 'ssummary' view
###################################################
Tab=> 'Instance' Title=> {text {Total instances: 7831}}
Tab=> 'Device' Title=> {text {Total spice devices: 256}}
Tab=> 'Memory' Title=> {text {Total memory allocated: 9621932 kB}}
Tab=> …Run Code Online (Sandbox Code Playgroud) 我在官方java教程中写了一个简单的"在多维数组中查找数字".这是教程中包含的代码:
class LabeledBreak {
public static void main(String[] args) {
int [][] numbers = {
{22, 34, 675, 23, 23},
{34, 76, 98, 23, 11},
{65, 234, 87, 23, 76}
};
int searchFor = 123;
boolean found = false;
int i;
int j = 0; // <-- this line
search:
for (i = 0; i < numbers.length; i++) {
for (j = 0; j < numbers[i].length; j++) {
if (searchFor == numbers[i][j]) {
found = true;
break search;
}
} …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,如果我将String名称保持为未初始化,则会出现编译错误,但是char初始化没有问题未被初始化.为什么这种行为有所不同?
class Demo {
public static void main(String[] args) {
char initial;
String name;
for (String input: args) {
name += input;
initial = input.charAt(0);
System.out.print(initial + " ");
}
}
Run Code Online (Sandbox Code Playgroud) #! /bin/sh
a1=260
a2=9150
echo "$a1 * 100 / $a2" | bc
Run Code Online (Sandbox Code Playgroud)
输出是
2
Run Code Online (Sandbox Code Playgroud)
它应该在哪里
2.8415
Run Code Online (Sandbox Code Playgroud)
虽然我正在使用,为什么精度会丢失bc?
如果我尝试在声明语句以外的语句上使用大括号初始化数组,为什么会出现编译错误
class Demo {
public static void main(String[] args) {
String[] myName;
myName = {"AA", "BB", "CC"};
for(String s: myName) {
System.out.print(s + " ");
}
}
}
Run Code Online (Sandbox Code Playgroud)
只在声明语句中使用大括号初始化有什么意义?
demo.java:4: error: illegal start of expression
myName = {"AA", "BB", "CC"};
^
demo.java:4: error: not a statement
myName = {"AA", "BB", "CC"};
^
demo.java:4: error: ';' expected
myName = {"AA", "BB", "CC"};
^
demo.java:5: error: illegal start of type
for(String s: myName) {
^
demo.java:5: error: ')' expected
for(String s: myName) …Run Code Online (Sandbox Code Playgroud)