2017-12-03 134 views
-1

我不知道怎麼說如何在一個方法上使用控制語句?如果可能的話

if money(); < 1000 and house();是==小打印你的可憐

if money(); > = 1000和< 5000和house();是==爲正常打印你好,你太富有

package ArrayTest; 

import java.util.Scanner; 

/** 
* Created by quwi on 02/12/2017. 
*/`enter code here` 
public class Happyness { 
    private static Scanner scanner = new Scanner(System.in); 


    public static void main(String[] args) { 
//  house(); 
//  money(); 
     comparison(); 


    } 

    private static void house() { 


     //  getting user input and using the switch statement to see the size 
     //  of their house 

     System.out.println("Please enter the size of your house "); 
     String cap = scanner.next(); 

     switch (cap) { 
      case "small": 
       System.out.println("you have a small house it means your poor"); 
       break; 
      case "normal": 
       System.out.println("your house normal, it mean your not poor nor rich"); 
       break; 
      case "large": 
       System.out.println("your house is big it means your rich"); 
       break; 
      default: 
       System.out.println("please choose between: small, normal or large"); 
       break; 
     } 
    } 


    private static void money() { 

     System.out.println("please enter a number"); 
     int wage = scanner.nextInt(); 
     if (wage < 1000) { 
      System.out.println("your poor"); 
     } else if (wage >= 1000 && wage < 5000) { 
      System.out.println("your not poor, your OK"); 
     } else if (wage > 5000) { 
      System.out.println("your rich, Give me money"); 
     } else { 
      System.out.println("please enter a number nothing else"); 
     } 
    } 

    private static void comparison() { 


     System.out.println("please enter a number"); 
     int decision = scanner.nextInt(); 

     switch (decision) { 
      case 1: 
       money(); 
       break; 
      case 2: 
       house(); 
       break; 
      case 3: 
       money(); 
       house(); 
       break; 

      default: 
       System.out.println("Please choose 1, 2 or 3"); 
     } 
    } 

} 
+0

你在這裏發佈的代碼有什麼問題? –

+0

爲了根據'money()'和'house()'做出決定,他們都需要返回一個值。在'comaprison()'中你可以有'int amount = money()' – c0der

回答

0

使用從兩者的返回值進行比較。

private static String money() { 

    System.out.println("please enter a number"); 
    int wage = scanner.nextInt(); 
    if (wage < 1000) { 
     return "poor"; 
    } else if (wage >= 1000 && wage < 5000) { 
     return "ok"; 
    } else if (wage > 5000) { 
     return "rich"; 
    } else { 
     //ask for input again or exit, whatever 
    } 
} 

house()做同樣的事情。然後,比較兩種方法的返回值。

+0

這樣的東西,它似乎沒有工作,我添加了返回值,但它似乎沒有打印「窮人」,「好的」或「富」 –

+0

它似乎沒有工作,我添加了回報,但它似乎並沒有打印「窮人」,「好」或「富」。最後的回報也令人困惑。它說返回需要一個字符串發現一個int。 –

相關問題