Java,我应该在switch-case中使用switch-case还是if-else?

时间:2017-01-31 19:00:17

标签: java function if-statement switch-statement

此程序在剪贴板上复制一个字符串(密码),我想添加复制用户名的选项。因此,如果用户在在线帐户上忘记他/她的用户名(或者只是懒惰),也可以获得该用户名。

首先,用户选择游戏/其他,然后用户可以选择复制到剪贴板的内容:用户名或密码。那么我在所有这些选项下添加另一个switch-case,还是选择if-statement?我应该在每个下面打一个函数调用吗?

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package helloworldapp;

/**
 *
 * @author Au-Thor
 */
import java.util.Scanner; 
import java.awt.datatransfer.*;
import java.awt.Toolkit;

public class HelloWorldApp {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       int n = 1;
       String addedString = "replace"; //This is for later usage
       String titleNames[] = {"Planetside 2","Nasty Website","Useless Social Media Account","Someother"};
       Scanner userInput1 = new Scanner(System.in);
       String thePassword = "Nothing";

       System.out.println("Enter a key: "); //This is for later usage
       addedString=(userInput1.nextLine()); //This is for later usage

    while(n!=0){ 
        Scanner userChoice = new Scanner(System.in);  // Reading from System.in

        for(int i = 0; i < titleNames.length; i++){  //Menu print-out
            int h=i+1;
            System.out.println( "["+h+".] " + titleNames[i]); 
        }

        System.out.println( "\n[0.] Quit\n");         
        System.out.println("\nEnter a number: ");
        n = userChoice.nextInt(); // Scans the next token of the input as an int.

        switch (n) {
            case 1:  //Ask if the user wants the username or the password
                     thePassword = "MAD PASSWORD FOR MY ACCOUNT" ;
                     break;
            case 2:  thePassword = "Replace";
                     break;
            case 3:  thePassword = "Replace";
                     break;
            case 4:  thePassword = "Replace";
                     break;
            case 5:  thePassword = "Replace";
                     break;
            case 6:  thePassword = "Replace";
                     break;
            case 7:  thePassword = "Replace";
                     break;
            case 8:  thePassword = "Replace";
                     break;
            case 9:  thePassword = "Replace";
                     break;
            case 10: thePassword = "Replace";
                     break;
            case 11: thePassword = "Replace";
                     break;
            case 12: thePassword = "Replace";
                     break;
            case 0:
                break;
            default: System.out.println("\nOption does not exist");;
                     break;
        }
        System.out.println("Current: " +thePassword+"\n"); //Change this to the Page or Game the credentials are for

        String myString = thePassword;
        StringSelection stringSelection = new StringSelection(myString);
        Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
        clpbrd.setContents(stringSelection, null);
        }

    System.out.println("Quitting..");
    }    
}

额外的东西: 有没有什么可以更有效地完成(除了所有这些:D)?我应该使用更多功能吗?我是否可以使用函数生成switch-case结构,该结构可以扩展到给定的参数,以创建具有相同功能的所有开关案例,甚至是可能的?

1 个答案:

答案 0 :(得分:0)

在你的switch语句中,你可以试试这样的东西:

switch (n) {
        case 1:  //Ask if the user wants the username or the password
                 thePassword = "MAD PASSWORD FOR MY ACCOUNT" ;
                 break;
        case 2:                       
        case 3:  
        case 4:  
        case 5:  
        case 6:  
        case 7: 
        case 8:  
        case 9:  
        case 10: 
        case 11: 
        case 12: thePassword = "Replace";
                 break;
        case 0:
            break;
        default: System.out.println("\nOption does not exist");;
                 break;
    }

如果break具有相同的行为,则不必在每个case上使用rawTime[][]