Java If语句无法按预期运行

时间:2013-12-03 12:34:17

标签: java if-statement

我正在尝试制作一个足够简单的if语句来创建一系列整数,在这种情况下=> 0和< = 250,但似乎无法正常工作。无论高度是多少,它都不会将其拉起并运行第二个块。

public static void display(int height, int weight)
{
    if(height >= 0 && height <= 250 )
    {
        System.out.println( format() );
    }
    else
    {
        System.out.println("Please enter a height (in cm) above 0 and below 250");
    }
}

以下是format()代码:

    public static String format()
{
    String name = "Harry Potter";
    int height = 256;
    int weight = 84;
    return String.format( "Name:\t %s %n" + "Height:\t %scm%n" + "Weight:\t %skg%n", name, height, weight);
}

2 个答案:

答案 0 :(得分:0)

它在这里工作。看看

public class Grocery
{//start class

 public static void main (String []args)
{//start main
 display(4,5);

}//end main


public static void display(int height, int weight)
{
if(height >= 0 && height <= 250 )
{
    System.out.println( format() );
}
else
{
    System.out.println("Please enter a height (in cm) above 0 and below 250");
}
}
   public static String format()
{
String name = "Harry Potter";
int height = 256;
int weight = 84;
return String.format( "Name:\t %s %n" + "Height:\t %scm%n" + "Weight:\t %skg%n", name, height, weight);
}
   }//end class

<强>输出

姓名:哈利波特

身高:256厘米

重量:84kg

答案 1 :(得分:0)

也许你想要更像这样的东西。

public static String format(int weight, int height, String name) {
     return String.format( "Name:\t %s %n" + "Height:\t %scm%n" 
                         + "Weight:\t %skg%n", name, height, weight);
}

public static void display(int height, int weight, String name) {

    if(height >= 0 && height <= 250 ) {
        System.out.println( format(height, weight, name) );
    }
    else {
        System.out.println("Please enter a height (in cm) above 0 and below 250");
    }
}

public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
    String name = scanner.nextLine();
    int weight = scanner.nextInt();
    int height = scanner.nextInt();

    display(wieght, height, name);
}