繁体   English   中英

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

[英]Java, Should I use switch-case or if-else within a switch-case?

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

首先,用户选择游戏/任何内容,然后用户选择要复制到剪贴板的内容:用户名或密码。 那么,我应该在所有这些选项下添加另一个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结构,该结构可以按给定的参数缩放,以创建具有相同功能的所有切换案例,这甚至可能吗?

在您的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;
    }

如果它们将具有相同的行为,则不是必须在每种case都使用break

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM