If-else语句使我的程序立即停止——Java
我只想在一个程序中发布到目前为止我得到的信息,该程序应该执行以下操作:If-else语句使我的程序立即停止——Java,java,if-statement,Java,If Statement,我只想在一个程序中发布到目前为止我得到的信息,该程序应该执行以下操作: -根据用户提供的输入计算BAC(血液酒精含量),并通过各种输入确定他们是否会获得DUI(根据提供的输入超过限值)。 下面是我的代码: 导入java.util.* public class Proj2_Mazzone { public static void main (String[] args) { Scanner scan = new Scanner(System.in);
-根据用户提供的输入计算BAC(血液酒精含量),并通过各种输入确定他们是否会获得DUI(根据提供的输入超过限值)。
下面是我的代码:
导入java.util.*
public class Proj2_Mazzone
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
String gender;
int D, W, H, age;
double ratio, limit, alcAbsorbed, alcMetabolized, BAC;
//这里根据给定的输入计算BAC
//使用&&with if语句生成4个不同的场景。。。
//所以f21,fnot21,m21,mnot21
System.out.print(“如果您是男性,请输入M;如果您是女性,请输入F:”;
性别=scan.nextLine();
//21岁以上男性
if(性别、同等信号(“m”)和年龄>21岁){
比率=.73;
极限=.08;
吸收alc=(3.701*D)/(W*比率);
代谢ALC=.015*H;
BAC=ALC吸收-ALC代谢;
如果(BAC>=限制){
System.out.println(“你超出了限制!”);
System.out.println(“您的BAC为“+BAC+”!”);
}
否则如果(BAC=限制){
System.out.println(“你超出了限制!”);
System.out.println(“您的BAC为“+BAC+”!”);
}
否则,如果(BAC 21){
比率=.66;
极限=.08;
吸收alc=(3.701*D)/(W*比率);
代谢ALC=.015*H;
BAC=ALC吸收-ALC代谢;
如果(BAC>=限制){
System.out.println(“你超出了限制!”);
System.out.println(“您的BAC为“+BAC+”!”);
}
否则如果(BAC=限制){
System.out.println(“你超出了限制!”);
System.out.println(“您的BAC为“+BAC+”!”);
}
else-if(BACelse-if语句中的真正问题是其中一个可能不会触发。与其在if(BAC中执行if-else-if
语句,不如创建一个公共函数。该函数包含两个参数,即比率和限制。
除此之外,所有年龄限制和男性或女性的所有其他计算都是通用的
在这个函数中进行计算,然后简单地打印出来
这将缩短您的代码,使其更易于阅读和理解。有一个逻辑问题:21岁的女性失败。您的主要假设是:
if(gender.equalsIgnoreCase("f") && age > 21){
...
}
if(gender.equalsIgnoreCase("f") && age < 21){
....
}
if(性别、平等信号和年龄>21岁){
...
}
if(性别、同等信号和年龄<21岁){
....
}
如果时间正好是21,则两个块都不会被输入。我建议在发现问题后重构此代码,以减少重复。同时尽量避免可能导致代码无法运行的If语句,而使用If-else语句-您总是想打印一些内容,否则会感觉程序刚刚停止
也许是这样的
public static void main (String[] args)
{
// Scan variables etc
// calculate ratio
double ratio = -1;
if (gender.equalsIgnoreCase("m"))
{
ratio = .73;
}
if (gender.equalsIgnoreCase("f"))
{
ratio = .66;
}
// calculate limit
double limit = -1
if (age > 21)
limit = .08;
limit = .02;
// calculate blood alcohol level
alcAbsorbed = (3.701 * D) / (W * ratio);
alcMetabolized = .015 * H;
double bloodAlcoholLevel = alcAbsorbed - alcMetabolized;
if(bloodAlcoholLevel >= limit)
{
System.out.println("You're over the limit!");
System.out.println("Your BAC is " + BAC + "!");
}
else
{
System.out.println("You're good...for now.");
System.out.println("Your BAC is " + BAC + ".");
}
}
这样,您的代码就可以更简短、更容易阅读,更不用说更改了。:-您的问题来自这样一个事实,即您的条件对于一个21岁的女性来说是错误的!她被排除在所有案例之外
您的代码也很难调试,因为它很难阅读。
考虑下面的重构,在这里我创建了用于确定值的方法。
我非常喜欢这样一个简单的代码,但这是为了向您展示,您永远不应该复制/粘贴代码。当您重复代码时,这意味着您可以将其放入一个方法中,该方法将使用正确的参数多次调用
public class StackOverflow {
// TODO define the real values!
private static final int D = 0, W = 0, H = 0;
public static void main(String[] args) {
// Get the input
Scanner scan = new Scanner(System.in);
System.out.print("Enter M if you're a male, or F if you're a female: ");
String gender = scan.nextLine();
System.out.print("Enter your age: ");
int age = Integer.valueOf(scan.nextLine());
// Get the good values
double ratio = getRatio(gender);
double bac = getBAC(ratio);
double limit = getLimit(age);
// Print the result
if (bac <= limit) {
System.out.println("You're good...for now.");
} else {
System.out.println("You're over the limit!");
}
System.out.println("Your BAC is " + bac + "!");
}
/**
* This method returns the ratio according to the gender.
*/
private static double getRatio(String gender) {
if ("m".equalsIgnoreCase(gender)) {
return .73;
} else if ("f".equalsIgnoreCase(gender)) {
return .66;
}
throw new IllegalArgumentException("Unknown gender: " + gender);
}
/**
* This method returns the BAC according to the ratio.
*/
private static double getBAC(double ratio) {
double alcAbsorbed = (3.701 * D) / (W * ratio);
double alcMetabolized = .015 * H;
return alcAbsorbed - alcMetabolized;
}
/**
* This method returns the limit according to the age.
*/
private static double getLimit(int age) {
if (age < 21) {
return .02;
} else {
return .08;
}
}
}
公共类堆栈溢出{
//TODO定义真正的值!
私有静态最终整数D=0,W=0,H=0;
公共静态void main(字符串[]args){
//获取输入
扫描仪扫描=新扫描仪(System.in);
System.out.print(“如果您是男性,请输入M;如果您是女性,请输入F:”;
字符串性别=scan.nextLine();
系统输出打印(“输入您的年龄:”;
int age=Integer.valueOf(scan.nextLine());
//获得好的价值观
双倍比率=getRatio(性别);
双bac=getBAC(比率);
双重限制=getLimit(年龄);
//打印结果
如果(bac现在正在检查代码。同时,你需要修正这对21岁的人不起作用的事实。啊……这是真的。我在想……我应该在每个场景中使用for循环吗?比如对f21、fnot21、m21、mnot21使用for循环,然后在内部执行相同的语句?何时初始化age
变量?Vincent-我在上面用scan.nextInt初始化了它,我问用户是男性还是女性。“停止”是什么意思?它永远不会退出吗?哪一行是“停止”?年龄是通过scan.nextInt在所有这些之前启动的。此外,我尝试过这样做,但限制尚未启动,因此它不允许我…我尝试将限制作为年龄以下的if-else语句,但它不起作用。然后启动限制!如果没有限制,则此程序没有意义。尝试我的解决方案,看看它是否有效。它可能会不是因为时间晚了,我已经筋疲力尽了,但我想不出如何启动限制,并尝试在下面的if-then语句中调用它。我修复了它,但它仍然在我按m或f时关闭。看到我的答案了。但也许这只是一个伟大的思想相似的例子?:-)哈哈!我没有看到你的帖子,我在写我的。正如你所说,伟人所见略同;)嗯……虽然我真的非常感谢你的参与,但代码中包含了很多我们在课堂上还没有学到的东西,这是为了一个项目,我正试图提前完成。因此,如果我最终使用了return和private内容,我会在一周左右的时间内让你知道。非常感谢!我不会使用我想是的。我们还没有学习“return,private,throw,非法参数exception”@Mazzone我已经更新了答案,使函数内联,因此没有使用“private”、“static”或“return”关键字。没有老师认为这里面有任何错误
public static void main (String[] args)
{
// Scan variables etc
// calculate ratio
double ratio = -1;
if (gender.equalsIgnoreCase("m"))
{
ratio = .73;
}
if (gender.equalsIgnoreCase("f"))
{
ratio = .66;
}
// calculate limit
double limit = -1
if (age > 21)
limit = .08;
limit = .02;
// calculate blood alcohol level
alcAbsorbed = (3.701 * D) / (W * ratio);
alcMetabolized = .015 * H;
double bloodAlcoholLevel = alcAbsorbed - alcMetabolized;
if(bloodAlcoholLevel >= limit)
{
System.out.println("You're over the limit!");
System.out.println("Your BAC is " + BAC + "!");
}
else
{
System.out.println("You're good...for now.");
System.out.println("Your BAC is " + BAC + ".");
}
}
public class StackOverflow {
// TODO define the real values!
private static final int D = 0, W = 0, H = 0;
public static void main(String[] args) {
// Get the input
Scanner scan = new Scanner(System.in);
System.out.print("Enter M if you're a male, or F if you're a female: ");
String gender = scan.nextLine();
System.out.print("Enter your age: ");
int age = Integer.valueOf(scan.nextLine());
// Get the good values
double ratio = getRatio(gender);
double bac = getBAC(ratio);
double limit = getLimit(age);
// Print the result
if (bac <= limit) {
System.out.println("You're good...for now.");
} else {
System.out.println("You're over the limit!");
}
System.out.println("Your BAC is " + bac + "!");
}
/**
* This method returns the ratio according to the gender.
*/
private static double getRatio(String gender) {
if ("m".equalsIgnoreCase(gender)) {
return .73;
} else if ("f".equalsIgnoreCase(gender)) {
return .66;
}
throw new IllegalArgumentException("Unknown gender: " + gender);
}
/**
* This method returns the BAC according to the ratio.
*/
private static double getBAC(double ratio) {
double alcAbsorbed = (3.701 * D) / (W * ratio);
double alcMetabolized = .015 * H;
return alcAbsorbed - alcMetabolized;
}
/**
* This method returns the limit according to the age.
*/
private static double getLimit(int age) {
if (age < 21) {
return .02;
} else {
return .08;
}
}
}