Java 如何让我的程序执行随机方法?
我正在为谷歌科学博览会做一个项目。我正在制作一个应用程序,它接受两个数字(条形码和序列号),并使用不同的算法(如Fisher-Yates shuffle)将它们更改为两个不同的数字。 我的代码如下所示:Java 如何让我的程序执行随机方法?,java,algorithm,user-interface,encryption,methods,Java,Algorithm,User Interface,Encryption,Methods,我正在为谷歌科学博览会做一个项目。我正在制作一个应用程序,它接受两个数字(条形码和序列号),并使用不同的算法(如Fisher-Yates shuffle)将它们更改为两个不同的数字。 我的代码如下所示: enter code here public static void main(String[] args) { reverseNum b = new reverseNum(); addOne c= new addOne(); plusThree f= new plu
enter code here public static void main(String[] args) {
reverseNum b = new reverseNum();
addOne c= new addOne();
plusThree f= new plusThree();
plusNine i = new plusNine();
plusTwo l = new plusTwo();
minusTwo p = new minusTwo();
plusOne r= new plusOne();
plusFour u= new plusFour();
threeTwo x= new threeTwo();
intoTwo ab= new intoTwo();
plusFive bc = new plusFive();
intoSix cd= new intoSix();
twoOne de = new twoOne() ;
plusSeven ef= new plusSeven();
plusEight fg= new plusEight();
minOne gh = new minOne();
intoSeven hi = new intoSeven();
intoEight ij = new intoEight();
intoNine jk = new intoNine();
intOne kl = new intOne();
intoFour lm = new intoFour();
// TODO code application logic here
Scanner user_input = new Scanner( System.in );
String a ;
System.out.println("Enter the Barcode:");
a = user_input.next();
System.out.println("Encrypted Barcode:"+ blah.randomMethod(a));
//Random method from the above given methods should modify this.
String z;
System.out.println("Enter the Serial Number:");
z= user_input.next();
System.out.println("Encrypted Serial Number:" + blah.randomMethod(z) );
//Random method from the above given methods should modify this
}
}
我的主要目标是使用上述方法(算法)修改两个给定的数字。我希望在用户每次运行程序时从给定列表中使用随机方法修改给定的数字。我还需要将这些都放到GUI中。请帮助。使用策略模式。让每个算法实现该策略的一个版本。然后使用工厂,使用您想要的任何机制获得随机策略实现。无需将输入组合成单个对象。使用策略模式。让每个算法实现该策略的一个版本。然后使用工厂,使用您想要的任何机制获得随机策略实现。无需将输入组合成单个对象。关于策略和工厂模式,我同意jgitter 我在贴一个例子
public interface Algorithm {
public String execute(String input);
}
public class SomeAlgorithm implements Algorithm {
public String execute(String input) {
///... Algorithm here
}
}
public class AnotherAlgorithm implements Algorithm {
public String execute(String input) {
///... Algorithm here
}
}
public abstract class AlgorithmFactory {
public static Algorithm createRandomAlgorithm() {
//Create a new Random object
Random randomEngine = new Random();
//Retrieve a number from 0 (inclusive) to 2 (exclusive)
int randomNumber = randomEngine.nextInt(2);
//Select which implementation to use according to the random number
switch(randomNumber) {
case 0: return new SomeAlgorithm();
case 1: return new AnotherAlgorithm();
}
//This will most likely never get called, but a return value is mandatory
return null;
}
}
然后,您将检索并执行如下算法:
String encryption = AlgorithmFactory.createRandomAlgorithm().execute(input);
请注意,工厂是抽象的,有一个静态的方法,这不是我将如何处理的,但它将帮助您轻松地解决问题。我同意jgitter关于战略和工厂模式的观点 我在贴一个例子
public interface Algorithm {
public String execute(String input);
}
public class SomeAlgorithm implements Algorithm {
public String execute(String input) {
///... Algorithm here
}
}
public class AnotherAlgorithm implements Algorithm {
public String execute(String input) {
///... Algorithm here
}
}
public abstract class AlgorithmFactory {
public static Algorithm createRandomAlgorithm() {
//Create a new Random object
Random randomEngine = new Random();
//Retrieve a number from 0 (inclusive) to 2 (exclusive)
int randomNumber = randomEngine.nextInt(2);
//Select which implementation to use according to the random number
switch(randomNumber) {
case 0: return new SomeAlgorithm();
case 1: return new AnotherAlgorithm();
}
//This will most likely never get called, but a return value is mandatory
return null;
}
}
然后,您将检索并执行如下算法:
String encryption = AlgorithmFactory.createRandomAlgorithm().execute(input);
请注意,工厂是抽象的,有一个静态的方法,这不是我的方法,但它将帮助您轻松地解决问题。您试图用随机方法解决什么问题?我不理解您使用单一变量的意思。您可以创建一个包含条形码和序列号变量的嵌入式类,并将它们作为单个对象一起操作。然后,您的方法可以将它们作为单个参数,并在适当的位置修改它们,这可能会更简洁。这就是你要找的吗?@gmcgath:我想他是想提供一个随机实现。我的意思是,我希望f和g是条形码和序列号的默认变量,我希望每次用户运行程序时都能用随机方法修改它们描述以及您希望对实际方法实现保密的事实并不会让我对您的方案产生任何好感。你听说过吗?你想用随机方法解决什么问题?我不明白你说的单一变量是什么意思。您可以创建一个包含条形码和序列号变量的嵌入式类,并将它们作为单个对象一起操作。然后,您的方法可以将它们作为单个参数,并在适当的位置修改它们,这可能会更简洁。这就是你要找的吗?@gmcgath:我想他是想提供一个随机实现。我的意思是,我希望f和g是条形码和序列号的默认变量,我希望每次用户运行程序时都能用随机方法修改它们描述以及您希望对实际方法实现保密的事实并不会让我对您的方案产生任何好感。你听说过吗?你能不能通过提供一个简短的代码示例,让这个答案不那么抽象?如果您知道行话,那么使用模式行话描述解决方案是很好的。我经常看到模式被用在货物崇拜的方式中,而不是试图首先理解它们。向他展示一个基本的工厂方法,其中包含一个随机数和一个案例陈述。看起来Mati击败了我。你能不能通过提供一个简短的代码示例,让这个答案稍微抽象一点?如果您知道行话,那么使用模式行话描述解决方案是很好的。我经常看到模式被用在货物崇拜的方式中,而不是试图首先理解它们。向他展示一个基本的工厂方法,包括一个随机数和一个case语句。看起来Mati打败了我。感谢您给出一个示例实现。看起来不错。谢谢你的考虑。但不幸的是,这不是我想要的。非常感谢:)@Mati cicero请解释为什么这不是你想要的。给定的实现缺少什么?感谢您提供了一个示例实现。看起来不错。谢谢你的考虑。但不幸的是,这不是我想要的。非常感谢:)@Mati cicero请解释为什么这不是你想要的。给定的实现缺少什么?