您好,我正在尝试使用返回类型“ String”的方法来打印字符序列。下面提到的是逻辑。我得到的错误是:
prog1.java:34: error: missing return statement
}
^
1 error
下面提到的是code.it应该打印字符..i可以处理“ void”类型,但不能处理返回类型“ int”。任何建议将不胜感激。
import java.util.*;
class prog1
{
public static void main(String[] args)
{
double height;
double width;
String fillcharac;
Scanner sc = new Scanner(System.in);
System.out.println("enter a value for height");
height = sc.nextDouble();
Scanner sc1 = new Scanner(System.in);
System.out.println("enter a value for width:");
width = sc.nextDouble();
Scanner sc2 = new Scanner(System.in);
System.out.println("enter the character value:");
fillcharac = sc2.next();
String x = print(height, width, fillcharac);
System.out.println(x);
}
public static String print(double height, double width, String fill)
{
for (double i = 1; i <= height; i++)
{
for (double j = 1; j <= width; j++)
{
return fill;
}
}
}
}
看来您的内部for
循环应该“构建”一个在循环结束后返回的字符串。
也许应该命名该方法,build
而不是print
稍后进行打印?
就像是:
public static String build( double height, double width, String fill ) {
String result;
for( double i=1; i<=height; i++) {
for(double j=1; j<=width; j++) {
// Build your string here using i, j and fill
// result = ...
}
}
return result;
}
然后,您可以执行以下操作:
// Build the output string
x = build( height, width, fillcharac );
// Print the output string
System.out.println( x );
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句