我有一个至少有三个元素的锯齿状数组,我需要解析前五个元素,用空格填充任何空值.
// there will ALWAYS be three elements
String whiconcatC = scrubbedInputArray[0];
String whiconcatD = scrubbedInputArray[1];
String whiconcatE = scrubbedInputArray[2];
// there MAY be a fourth or fifth element
if (scrubbedInputTokens > 3) {
String whiconcatF = scrubbedInputArray[3];
} else {
String whiconcatF = " ";
}
//
if (scrubbedInputTokens > 4) {
String whiconcatG = scrubbedInputArray[4];
} else {
String whiconcatG = " ";
}
Run Code Online (Sandbox Code Playgroud)
虽然上面的代码在编译期间不会产生错误,但后续行引用whiconcatF或whiconcatG在编译期间会出错cannot find symbol.
我已经尝试使用forEach和StringTokenizer(在将数组转换为分隔的字符串之后),但无法弄清楚如何在实例中使用默认值,即第4和第5点没有值. …
我有这个代码从args获得平均值:
public class Mariamne
{
public static void main(String[] args)
{
System.out.print("\n");
try
{
int sum = 0;
float prom = 0;
for (int i = 0; i<=args.length; i++)
{
int a = Integer.parseInt(args[i]);
sum = a + sum;
prom = prom + 1;
}
System.out.println(sum/prom);
}
catch(Exception e)
{
System.out.println("Error.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,我不能让它工作(没有例外它说"数组越界"),有没有办法来定义args的大小?请帮忙!
我是C++/C#开发人员,也是Java新手.在互联网上发现的一些参考文献表明我们不能在Java中通过引用调用.这是完全正确的,还是以任何方式我可以从函数中返回多个值?
其次,默认参数似乎不支持Java,我也试图找到有没有办法在Java中使用默认参数.
谢谢.
为什么这会回归真实?
String b = "(5, 5)";
String a = "(7, 8)" ;
if(a.equals(b));
{
System.out.printf("Somehow \"%s\" and \"%s\" are the same" ,a,b);
}
Run Code Online (Sandbox Code Playgroud)
输出:
Somehow "(7, 8)" and "(5, 5)" are the same
Run Code Online (Sandbox Code Playgroud) String firstname , middlename , lastname ;
firstname = fn.getText().substring(0,1);
middlename = mn.getText().substring(0,1);
lastname = ln.getText();
String shortname = shortname.concat(lastname); // error is in this line, shortname.concat
shortname = shortname.concat(" ");
shortname = shortname.concat(firstname);
shortname = shortname.concat(".");
shortname = shortname.concat("middlename");
shortname = shortname.concat(".");
shrt.setText(shortname);
Run Code Online (Sandbox Code Playgroud)
没有其他行有任何错误.只是shortname变量未初始化.
注意:简单的解决方案请.我上课11.把这个JAVA拿出来.
java ×6
args ×1
arrays ×1
average ×1
boolean ×1
if-statement ×1
non-static ×1
parsing ×1
string ×1