以下java代码运行良好,并且在“public static void main(String [] args)'”下编写时提供了所需的输出。 as:
import com.google.common.collect.ObjectArrays;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import java.util.regex.*;
public class ArrayReturnSurTime {
public static void main( String[] args )
{
int count1=0;
int count2=0;
int total_full_sur=0;
float word_time= 0.25f;
float full_sur_time=0.0f;
float kan_sur_time=0.0f;
float kun_sur_time=0.0f;
String word="srg]mp";
String modifyword=null;
String after =null;
String rl=null;
char[] c = word.toCharArray();
int m=c.length;
for(int i= 0; i<c.length; i++){
count1++;
if(c[i]==']'){count2++;}
total_full_sur= count1-2*count2;
full_sur_time=word_time/total_full_sur;
kan_sur_time = (float) (full_sur_time*0.1);
kun_sur_time = (float) (full_sur_time*0.9);
}
for(int i=0; i<word.length(); i++){
char a = word.charAt(i);
if( Character.toString(a).equals("]")){
int j = i-1;
int k = i+1;
char b = word.charAt(j);
char d = word.charAt(k);
modifyword= word.replace( Character.valueOf(b).toString(), ".");
after= modifyword.replace( Character.valueOf(d).toString(), ",");
rl = after;
String [] sur= word.split("(?!^)");
String[] tmp_rl= rl.split ("(?!^)");
float[] rlg_time= new float[sur.length];
for(int x=0; x<rl.length(); x++){
switch (tmp_rl[x]) {
case ".":
rlg_time[x]=kan_sur_time;
break;
case ",":
rlg_time[x]=kun_sur_time;
break;
case "]":
rlg_time[x]=(float)0.001;
break;
default:
rlg_time[x]=full_sur_time;
break;
}
System.out.print(rlg_time[x]+" ");
}
}}}
}
它将浮点值打印为:
0.0625 0.0625 0.00625 0.001 0.05625 0.0625
我通过一种方法(ransurtime(String sur36)&#39;覆盖代码(需要改变微小代码)来重新编程。返回&#39; float [] rlg_time&#39;得到相同的输出:
import com.google.common.collect.ObjectArrays;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import java.util.regex.*;
public class ArrayReturnSurTime {
public static float[] ranisurtime(String sur36) {
int count1=0;
int count2=0;
int total_full_sur=0;
float word_time= 0.25f;
float full_sur_time=0.0f;
float kan_sur_time=0.0f;
float kun_sur_time=0.0f;
String word=sur36;
String modifyword=null;String after =null;String rl=null;
char[] c = word.toCharArray();
int m=c.length;
for(int i= 0; i<c.length; i++){
count1++;
if(c[i]==']'){count2++;}
total_full_sur= count1-2*count2;
full_sur_time=word_time/total_full_sur;
kan_sur_time = (float) (full_sur_time*0.1);
kun_sur_time = (float) (full_sur_time*0.9);
}
for(int p=0; p<word.length(); p++){
char a = word.charAt(p);
if( Character.toString(a).equals("]")) {
int j = p-1;int k = p+1;char b = word.charAt(j);char d = word.charAt(k);
modifyword= word.replace( Character.valueOf(b).toString(), ".");
after= modifyword.replace( Character.valueOf(d).toString(), ",");
rl = after;
String[] tmp_rl= rl.split ("(?!^)");
float[] rlg_time= new float[tmp_rl.length];
for(int x=0; x<rl.length(); x++){
switch (tmp_rl[x]) {
case ".":
rlg_time[x]=kan_sur_time;
break;
case ",":
rlg_time[x]=kun_sur_time;
break;
case "]":
rlg_time[x]=(float)0.001;
break;
default:
rlg_time[x]=full_sur_time;
break;
}
}
return rlg_time; //This 'rlg_time' is not being returned. Compiler ask return statement although this is the return statement,
}//First closing bracket to be crossed by 'return rlg_time'.
} // Second closing bracket to be crossed by 'return rlg_time'.
//return null;//It is the right place at which 'return rlg_time'. statement can do right, but how to manipulate to omit the above two closing brackets reach here.
}
public static void main( String[] args )
{
String sur36="srg]mp";
float[] A = ranisurtime(sur36);
for(int i=0; i<A.length; i++){
System.out.print(A[i]);
}
}
}
但我失败了。我知道&#39;返回rlg_time&#39;是不是在正确的地方(它应该移动两个右括号,即&#39;}如果要返回它,但是对编程和java不熟悉,我脑海中没有任何东西可以实现它。我需要有助于克服这种情况。
答案 0 :(得分:3)
因为你声明了和它没有工作的唯一原因 在if语句的正文中定义了
rlg_time
!什么 你试图实现的是返回这个变量的结果 方法结束!!!
But, mind you, that the variables are local as per the blocks! Hence, your rlg_time is not returning the proper result.
要解决此问题,请在方法开头将初始化rlg_time
与其他变量一起初始化。使用局部变量来使用临时值,例如local
。完成reult后,在该局部变量rlg_time
的限制(访问点结束)之前指定local
此值。现在您可以成功返回rlg_time
。顺便说一句,它根本不与Java相关联!
答案 1 :(得分:0)
极难阅读您的代码。请正确缩进您的代码。无论如何如果没有其他错误,只需返回类型问题然后将代码更改为: -
import com.google.common.collect.ObjectArrays;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import java.util.regex.*;
public class ArrayReturnSurTime {
public static float[] ranisurtime(String sur36) {
int count1=0;
int count2=0;
int total_full_sur=0;
float word_time= 0.25f;
float full_sur_time=0.0f;
float kan_sur_time=0.0f;
float kun_sur_time=0.0f;
float[] rlg_time=null;
String word=sur36;
String modifyword=null;String after =null;String rl=null;
char[] c = word.toCharArray();
int m=c.length;
for(int i= 0; i<c.length; i++){
count1++;
if(c[i]==']'){count2++;}
total_full_sur= count1-2*count2;
full_sur_time=word_time/total_full_sur;
kan_sur_time = (float) (full_sur_time*0.1);
kun_sur_time = (float) (full_sur_time*0.9);
}
for(int p=0; p<word.length(); p++){
char a = word.charAt(p);
if( Character.toString(a).equals("]")) {
int j = p-1;int k = p+1;char b = word.charAt(j);char d = word.charAt(k);
modifyword= word.replace( Character.valueOf(b).toString(), ".");
after= modifyword.replace( Character.valueOf(d).toString(), ",");
rl = after;
String[] tmp_rl= rl.split ("(?!^)");
rlg_time= new float[tmp_rl.length]; //changed here
for(int x=0; x<rl.length(); x++){
switch (tmp_rl[x]) {
case ".":
rlg_time[x]=kan_sur_time;
break;
case ",":
rlg_time[x]=kun_sur_time;
break;
case "]":
rlg_time[x]=(float)0.001;
break;
default:
rlg_time[x]=full_sur_time;
break;
}
}
}//First closing bracket to be crossed by 'return rlg_time'.
} // Second closing bracket to be crossed by 'return rlg_time'.
return rlg_time;//It is the right place at which 'return rlg_time'. statement can do right, but how to manipulate to reach here.
}
public static void main( String[] args )
{
String sur36="srg]mp";
float[] A = ranisurtime(sur36);
for(int i=0; i<A.length; i++){
System.out.print(A[i]);
}
}
}
我没有尝试过,但应该可以。您正在初始化FLoat [] array rlg_time in if statment。因此,只要if语句结束,变量就会被释放。将变量声明移到if语句之外。
答案 2 :(得分:0)
首先,看一下时间计算的重写:
for(int i= 0; i<word.length(); i++){
if(word.charAt(i)==']'){count2++;}
}
total_full_sur= word.length() - 2*count2;
full_sur_time=word_time/total_full_sur;
kan_sur_time = (float) (full_sur_time*0.1);
kun_sur_time = (float) (full_sur_time*0.9);
除了消除无用的复杂情况外,请注意您不会在依赖于循环已完成的循环中包含计算。
如果以相同的方式考虑下一个循环,它将解决您的问题。