【问题标题】:Java, Should I use switch-case or if-else within a switch-case?Java,我应该在 switch-case 中使用 switch-case 还是 if-else?
【发布时间】:2017-01-31 19:00:17
【问题描述】:

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

首先用户选择游戏/其他内容,然后用户可以选择复制到剪贴板的内容:用户名或密码。那么我是在所有这些选项下添加另一个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 结构,该结构可以扩展到给它的参数,以创建具有相同功能的所有开关案例,这甚至可能吗?

【问题讨论】:

  • 每当你发现自己使用很长的 if/then/else 或很长的 switch 语句时,如果在循环中做更有意义的话。如果您的数据发生很大变化,可以将选项保存在配置文件中并将其加载到数组或其他东西中,这样您就可以在不重新编译的情况下更改数据。
  • switch 语句的索引'n',在这种情况下,它是非稀疏的,意味着没有间隙,也可能是直接指向字符串数组的索引,而忘记if/then/else 或 switch。
  • 哦,你的意思是:thePassword = passwords[userChoice]; 之类的东西?这很有意义,并且当我扩展它时使事情变得更容易。从文件加载信息我还没有做太多的事情,但我将把它扩展到那个方向。
  • 是的,这就是我的意思。很多东西要习惯,但你会开始考虑优化,并掌握如何处理这样的事情的技巧,它会变得更容易和更自然。
  • 非常感谢。这解决了我的问题,我就做一个二维数组来存储登录信息,我认为暂时是这样,下一步我会看看我是否可以进行文件读取有点工作。太糟糕了,我不能投票赞成评论,我现在也不能赞成任何事情(至少可见);代表太低了。

标签: java function if-statement switch-statement


【解决方案1】:

在你的 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 上使用它们。

【讨论】:

  • 好吧,很高兴知道,虽然我打算用不同的东西填充每个人,但重复是由于复制粘贴。但是,这很可能在未来成为有用的知识。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-14
  • 1970-01-01
  • 1970-01-01
  • 2013-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多