Java 将英寸转换为英尺和英寸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19349739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting inches into feet and inches
提问by user2876927
I am currently taking a introduction to programming class and have reached a problem. I have been asked to convert a height measurement from inches to feet & inches.
我目前正在介绍编程课程,但遇到了一个问题。我被要求将高度测量值从英寸转换为英尺和英寸。
I have gotten to the point were I think I mostly have it but I get a not a statement error when I go to compile. Here is what I have so far for this method
我已经到了这一点,我认为我主要拥有它,但是当我去编译时,我得到了一个 not 语句错误。这是我到目前为止对这种方法的了解
/**
* @param inches to feet inches
*/
public String inchToFeet(int heightInInches) {
int IN_PER_FOOT;
int feet = heightInInches - IN_PER_FOOT;
String output;
feet = feet / 12;
output = String; inchToFeet() + "\'" + IN_PER_FOOT.toString() + "\"";
return output;
}
I am also using a static final int to keep the inches per foot constrained to 12, like this.
我还使用静态最终 int 将每英尺英寸限制为 12,就像这样。
public static final int IN_PER_FOOT = 12;
This is really the only issue I am having at the moment, the rest it just getting it along with a hourly rate to display.
这确实是我目前唯一遇到的问题,其余的只是让它与每小时的费率一起显示。
Edit:
编辑:
The compile error I keep getting is 'Not a Statement'. I have also removed the semicolon from before the last String but got another error as it was looking for the semicolon.
我不断收到的编译错误是“不是声明”。我还从最后一个字符串之前删除了分号,但在寻找分号时遇到了另一个错误。
I will try your suggestion in a little Rahul, been looking at this for to long and need a break.
我会在一个小小的 Rahul 中尝试你的建议,已经看这个很久了,需要休息一下。
回答by Rahul Tripathi
Your code doesnt seem to work what you want. You may simply try like this:-
您的代码似乎无法按照您的意愿工作。你可以简单地尝试这样:-
int inches = 40;
int feet = inches / 12;
int leftover = inches % 12;
System.out.println(feet + " feet and " + leftover + " inches");