English 中文(简体)
Java的ches脚 in和产出作为回报状况
原标题:Converting inches to feet inches in Java and outputting as return statment

So im making a method that converts height to return as a String "5 feet 8 inches" and teacher gave us this method so its going to be a return not display

这是我迄今为止所做的事情,但我相信,你可以做些什么来回来,因为只有一种类型。

  public String convertHeightToFeetInches(int newHeight)
{
    int leftOver = newHeight % IN_PER_FOOT;
    if(newHeight < (IN_PER_FOOT * 2)){
        return "1 foot" + leftOver + "inches";

iii

iii

每当我经历一次差错,就会出现类似情况。

问题回答

鉴于这是你的法典:

public String convertHeightToFeetInches()
{
  int leftOver = newHeight % IN_PER_FOOT;
  if(newHeight < (IN_PER_FOOT * 2)){
    System.out.println("1 foot" + leftOver + "inches");
  }
}

归还str,只是这样做:

public String convertHeightToFeetInches()
{
  int leftOver = newHeight % IN_PER_FOOT;
  if(newHeight < (IN_PER_FOOT * 2))
  {
    return "1 foot" + leftOver + "inches";
  }
}

然而,请注意,这并不意味着任何面积在1英尺至X英寸之间。 您:

1英尺

你们需要增加一些空间。 当然,就额外信贷而言,如果其余部分为零,那么你可能只想说“一脚”而不是“一脚.”。 在此情况下,你希望在 rel=“nofollow”>StringBuilder ,然后在你完成学业后再回去。 您利用现有代码,使用<代码>StringBuilder(但仍无所需空间特性):

public String convertHeightToFeetInches()
{
  StringBuilder buff = new StringBuilder();
  int leftOver = newHeight % IN_PER_FOOT;

  if(newHeight < (IN_PER_FOOT * 2))
  {
    buff.append("1 foot");
    buff.append(leftOver);
    buff.append(inches);
  }

  return buff.toString();
}

此时此刻,我离开你......看你走了......。


EDIT:

由于我所用的每一种方法都错失了回归统计错误,因此陷入了困境。

在你目前的法典中,你有两种可能的执行途径,但只有一条途径具有价值。

public String convertHeightToFeetInches()
{
  int leftOver = newHeight % IN_PER_FOOT;
  if(newHeight < (IN_PER_FOOT * 2))
  {
    return "1 foot" + leftOver + "inches";    // we return a value - good
  }
  else                                        // there is also this execution path ...
  {
                                              // ... and you don t return anything - bad
  }
}

将<代码>String的返回类型改为int的返回类型,不忘记在方法结束时的回报价值。

public int convert heightToFeetInches() {
    int leftOver = (heightInInches % 12);
    return leftOver;
}

或者如果你希望作为<条码>回归,则照此办理:

public String convert heightToFeetInches() {
    int leftOver = (heightInInches % 12);

    return String.valueOf(leftOver);
}

OOT: 是否有C/C++/C# Programmer? 如果是的话,请确保你采用所有方法和变数来降低第1字的排位:hsa ToFeetInches(,不使用Hala ToFeetInches(<>,以防止对其他方案人员产生误解。 由于在 Java,我们使用上个案例说,它为<条码><>/条码>或<条码>,其中第一个字为<条码>>> 。

Think of how you would author the string without returning - just a normal print statement. Then, instead of printing it, return that String.

您可在稍后日期印出交结果;例如System.out.println (convertFeet ToHalaInches ("68 inches”);

Simply create a String Object and assign it a null value. Later, you can store your result inside this object and return that, like so:

public String convertHeightToFeetInches(int newHeight) {    

    final int IN_PER_FOOT = 12;
    int leftOver = newHeight % IN_PER_FOOT;
    String result = "";
    if(newHeight < (IN_PER_FOOT * 2)){
        result = "1 foot " + leftOver + " inches";

    } else {


        } return result;

}

请将此方法称为:

String conversion = convertHeightToFeetInches(5);

页: 1

public static final int IN_PER_FOOT = 12;
private int height;

/来源方法

public String convertheightToFeetInches() 
{
    int leftOver = (height %= IN_PER_FOOT);
    int newHeight = (height % IN_PER_FOOT);
    return newHeight + "Foot" + leftOver + "Inches";
}

//my height method is not shown but its simple and you should already get that part





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签