2017-01-31 12 views
0

このプログラムは、クリップボードに文字列(パスワード)をコピーします。ユーザー名もコピーするオプションを追加します。したがって、ユーザーがオンラインアカウントでユーザー名を忘れた場合(または単に怠けている場合)、それを取得することも可能です。Java、switch-caseまたはif-elseを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文で

+0

本当に長いif/then/elseを使用しているか、ループ内で行うのがより理にかなっている場合は、長いswitch文を自分自身として使用すると、また、データが大きく変更された場合は、設定ファイルにオプションを保存して、配列などにロードして、再コンパイルせずにデータを変更することができます。 – clearlight

+0

あなたのswitch文のインデックス 'n'は、この場合疎ではない、つまりギャップがないことを意味し、文字列の配列に直接インデックスを持ち、if/then/elseまたはスイッチを忘れる可能性があります。 – clearlight

+0

あなたは次のようになります: 'thePassword = passwords [userChoice];'物のタイプ?それは多くの意味があり、私がこれを展開すると、はるかに簡単になります。 ファイルから情報をロードすることは、まだあまり行っていないことですが、これをその方向に展開しようとしています。 – Guest

答えて

0

あなたはこのようなものを使用してみてください:

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

は、彼らが同じ振る舞いを持ってしようとしている場合は、すべてのcasebreakを使用することは必須ではありません。

+0

申し訳ありませんが、私は別のものでそれらのそれぞれを埋めるのですが、知っていいです、繰り返しはcopypastingによるものです。しかし、それは将来、有用な知識になる可能性が非常に高いでしょう。 – Guest

関連する問題