このプログラムは、クリップボードに文字列(パスワード)をコピーします。ユーザー名もコピーするオプションを追加します。したがって、ユーザーがオンラインアカウントでユーザー名を忘れた場合(または単に怠けている場合)、それを取得することも可能です。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文で
本当に長いif/then/elseを使用しているか、ループ内で行うのがより理にかなっている場合は、長いswitch文を自分自身として使用すると、また、データが大きく変更された場合は、設定ファイルにオプションを保存して、配列などにロードして、再コンパイルせずにデータを変更することができます。 – clearlight
あなたのswitch文のインデックス 'n'は、この場合疎ではない、つまりギャップがないことを意味し、文字列の配列に直接インデックスを持ち、if/then/elseまたはスイッチを忘れる可能性があります。 – clearlight
あなたは次のようになります: 'thePassword = passwords [userChoice];'物のタイプ?それは多くの意味があり、私がこれを展開すると、はるかに簡単になります。 ファイルから情報をロードすることは、まだあまり行っていないことですが、これをその方向に展開しようとしています。 – Guest