2015-11-01 85 views
0

在我开始之前,我想说明一下,在编写代码时,我非常习惯于初学者,所以如果我所问的内容看起来非常基本,我很抱歉。初学Java:从方法中返回值

这就是说我正在努力与我的代码中的返回方法。我可以编写这个程序,而不用像我那样将它分解成方法,但我被告知,编写代码时很好的做法是将它拆分成方法,以便调试更容易。

下面的代码似乎有一些主要缺陷。

public static boolean hybridNot() 
{ 
    String typeCar = input("Hybrid or Electric?"); 
    boolean electric = false; 

if (typeCar.equalsIgnoreCase("Electric")) 
{ 
    electric = true; 
} 

else if (typeCar.equalsIgnoreCase("Hybrid")) 
{ 
    electric = false; 
} 

else 
{ 
    print("Sorry I didn't understand that"); 
} 

return; 
} 

public static boolean solarNot() 
{ 
    String panelsMaybe = input("Solar panel?"); 
    boolean solarPanel = false; 

    if (panelsMaybe.equalsIgnoreCase("Yes")) 
    { 
     solarPanel = true; 
    } 

    else if (panelsMaybe.equalsIgnoreCase("No")) 
    { 
     solarPanel = false; 
    } 

    else 
    { 
     print("Sorry I didn't understand that"); 
    } 

    return; 
    } 

public static int discountNot() 
{ 
    final int basicPrice = 20000; 
    final int electricCost = 2000; 
    final int solarCost = 5000; 
    boolean electric = hybridNot(); 
    boolean solarPanel = solarNot(); 
    int totalPrice; 

    if ((solarPanel = true) || (electric = true)) 
    { 
     totalPrice = basicPrice + solarCost + electricCost - 500; 
    } 

    else if ((solarPanel = true) || (electric = false)) 
    { 
     totalPrice = basicPrice + solarCost; 
    } 

    else if ((solarPanel = false) || (electric = true)) 
    { 
     totalPrice = basicPrice + electricCost; 
    } 

    else 
    { 
     totalPrice = basicPrice; 
    } 

    return; 

    } 

public static void totalCost() 
{ 
    final int basicPrice = 20000; 
    final int electricCost = 2000; 
    final int solarCost = 5000; 
    final int discountCost = 500; 
    boolean hybrid = hybridNot(); 
    boolean solarPanel = solarNot(); 
    int finalPrice = 0; 

    finalPrice = discountNot(); 

    if (finalPrice >= 26500) 
    { 
     print("Basic Price: " + basicPrice + "\n" + "Electric model: " + electricCost + "\n" + "Solar Panel: " + solarCost + "\n" + "Discount: " + discountCost); 
    } 

    else if (finalPrice >= 25000) 
    { 
     print("Basic Price: " + basicPrice + "\n" + "Solar Panel: " + solarCost); 
    } 

    else if (finalPrice >= 22000) 
    { 
     print("Basic Price: " + basicPrice + "\n" + "Electric model: " + electricCost); 
    } 

    else 
    { 
     print("Basic Price: " + basicPrice); 
    } 

    print("Total: " + finalPrice); 

    } 

由于某些原因,hybridNot和solarNot似乎在转向下一个方法之前重复自己。对我来说,似乎我可能会在方法结束时返回一个问题,但我实在无法弄清楚什么是错误的。方法totalCost似乎忽略了方法discountNot中的if语句,并且布尔值没有正确传递给totalCost,我只在finalPrice >= 26500时获得该值。

再一次,我是一般的Java新手,我也是新来的stackoverflow(所以嗨!!)所以请告诉我,如果我做错了什么,我会看看下一次正确的!谢谢!!

+1

我困惑,你说的方法做任何事情,因为这样的代码不实际编译。你不能在一个声明了返回类型的方法(例如'boolean')中编写'return'语句,而不用根据你希望返回的值返回'return'。 – RealSkeptic

+0

@RealSkeptic:公平地说,这就是问题所在。但是,当我尝试实际返回值时,程序变得越来越糟。要么它开始循环,要么编译器抱怨我返回了错误的类型。 – Overclock

+0

这意味着存在逻辑问题。这是有道理的,因为你有三种可能性(电动的,混合的,不好的输入),但是你选择了一种只给你两种可能性的返回类型。你需要改变逻辑(也许把方法里面的验证循环?抛出一个异常?) – RealSkeptic

回答

1

你没有在你的函数中返回任何值。用途:

public static boolean hybridNot(){ 
    //... 
    return electric; 
} 

返回booleanelectric。在其他函数中使用相似的语法来返回相关的变量。

此外,方法不断地自我重复的原因是因为你给他们打电话两次:

public static int discountNot() 
{ 
    //... 
    boolean electric = hybridNot(); //here 
    boolean solarPanel = solarNot(); 
    //... 
} 

public static void totalCost() 
{ 
    //... 
    boolean hybrid = hybridNot(); //and here 
    boolean solarPanel = solarNot(); 
    //... 
} 
+0

我之前在我的代码中曾经有过这样的事情,但它所做的只是进入一个循环,询问我是否需要混合或电动一次又一次。如果我误解了你,道歉。对不起,我没有看到你的理由,为什么他们重​​复自己。这解决了重复问题,非常感谢! – Overclock

2

你应该根据方法的返回类型返回值。

public static boolean hybridNot() 
{ 
    String typeCar = input("Hybrid or Electric?");  
    return (typeCar.equalsIgnoreCase("Electric")) 
} 

我不是说要在检查状态的函数中获取输入 - 它本身就是邪恶的。

现在的位置:

if ((solarPanel = true) || (electric = true)) 

你做作业,不comparement。比较你可以这样做:

if (solarPanel || electric) 

因为变量已经是布尔值了。

这里:

boolean hybrid = hybridNot(); 
    boolean solarPanel = solarNot(); 
    int finalPrice = 0; 

    finalPrice = discountNot(); 

discountNothybridNotsolarNot再次呼吁:

public static int discountNot() 
{ 
    ... 
    boolean electric = hybridNot(); 
    boolean solarPanel = solarNot(); 
+0

请注意,OP初始化'boolean's为'false',所以如果到达“我不知道”选项,它仍然会返回false(如果函数被更正) – Arc676

+0

你是对的,它看起来像从第一眼看,试图在布尔值上有一个三态。我会更新。 –

+0

谢谢你的回复。然而,我有几个问题。 我不明白为什么你会返回你有什么,是不是分类为一个字符串?同时比较会导致编译器抱怨'||'是一个意外的类型。 – Overclock