两种方法同时执行?为什么呢?

时间:2015-11-19 11:54:51

标签: java methods switch-statement execution

package contractmanager;
import java.util.*;
/**
*
* @author Tom McCloud
*/
public class ContractManager {
    static Scanner keyb = new Scanner(System.in);
    // global scanner
    public static void main(String[] args) {
        int option;
        //variable declaration
        String clientName;
        String packageSize;
        String dataBundle;
        String reference;
        int period;
        boolean intlCalls;
        //display menu to user
        System.out.println("Welcome: \n");
        System.out.println("1. Enter new contract       ");
        System.out.println("2. Display contract summary");
        System.out.println("3. Display summary of contract for selected month");
        System.out.println("4. Find and display contract");
        System.out.println("0. Exit");

        //take option off user
        option = keyb.nextInt();

        //WIP - only working on option 1 at the minute
        switch(option) {
        case 1:
               clientName = clientName();
               packageSize = packageSize();
               dataBundle = dataBundle();
               reference = reference();
               break;
        }

        exit();

    }

    public static void exit()
    {
        System.out.println("Thank you for using the contract manager. Goodbye!");
    }

    public static String clientName()
    {
        String name = " ";
        System.out.println("Please input your full name: ");
        name = keyb.nextLine(); 


         return name;
    }

    public static String packageSize()
    {
        String size;

        System.out.println("Please input your package size: ");
        System.out.println(" 1. Small \n 2. Medium \n 3. Large");
        size = keyb.next();

        return size; 
    }

    public static String dataBundle()
    {
        String data;

            System.out.println("Please input data bundle size: ");
            System.out.println("1. Low \n 2. Medium \n 3. High \n 4. Unlimited");
            data = keyb.next();


        return data;
    }

    public static String reference()
    {
        String ref;
        boolean isRefValid = false;
        do {
            System.out.println("Please input your reference code: ");
            ref = keyb.next();

            if(ref.length() > 6)
            {
                System.out.println("Reference number too long, re-enter!");
            }

            for(int i = 0; i < 2; i++)
            {
               if(Character.isDigit(ref.charAt(i)))
               {
                   System.out.println("First two characters must be letters!");
               }
            }

        } while(isRefValid = false);

        return ref;     
    }
}

所以,这是我的一些代码。如果我按enter code here一个,它会执行这些,现在技术上一旦每个方法完成并返回,它们不应该按照彼此的顺序排列吗?

例如,在按“1”后执行时,我得到以下输出:

Please input your full name: 
Please input your package size: 
 1. Small 
 2. Medium 
 3. Large

虽然这应该是一个接一个,但在输入全名后,它应该转到包装尺寸步骤。如果我输入它进入第三步而不是重复第二步的输入。

1 个答案:

答案 0 :(得分:1)

我认为这是因为在您的clientName函数中,您刚刚打印了“请输入您的全名:”而无需等待输入。例如,你必须在这里执行类似下面的操作scan.nextLine()将等到用户按下输入:

Scanner scan = new Scanner();
System.out.println("Please input your full name:");

String name= scan.next();

System.out.println(name);

scan.nextLine();

更新:尝试更新clientName功能,如下所示

public static String clientName() {
    String name = " ";
    System.out.println("Please input your full name: ");
    name = keyb.next();

    keyb.nextLine();

    return name;
}