有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在方法上使用控制语句?如果可能的话

我不知道该怎么说

如果是钱&书信电报;1000和房子();你是穷人吗

或者

如果是钱>=1000和<;5000和房子();是==正常打印你的ok,你的太丰富了

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");
        }
    }

}

共 (1) 个答案

  1. # 1 楼答案

    使用两者的返回值进行比较

    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()执行同样的操作。然后,用另一种方法比较两者的返回值