英文:
multiple method execution with same input processed
问题
热忱的问候。
我有一个场景,我有10个方法,即process1、process2...,process10,根据输入,我需要按特定顺序执行这些方法中的几个。
- 1,3,5,7
- 2,4,6,8
- 1,5,9,10
无论我执行什么顺序,第一个方法的输出都应该作为第二个方法的输入,依此类推。
我在互联网上搜索了很多,但没有找到合适的东西。我开始编写下面的代码。目前我正在使用Java的concat方法来实现这一点。
public class Perform {
static String op = "";
public static void main(String[] args) {
process(list);
}
public static void process(List<String> list) {
for (String str : list) {
if (str.startsWith("Samsung")) {
process1("test").concat(process2(op).concat(process3(op)).concat(process4(op)));
}
}
}
public static String process1(String str) {
System.out.println("process1 start");
System.out.println("str1:" + str);
System.out.println("process1 end");
return str + "1";
}
public static String process2(String str) {
System.out.println("process2 start");
System.out.println("str2:" + str);
System.out.println("process2 end");
return str + "2";
}
public static String process3(String str) {
System.out.println("process3 start");
System.out.println("str3:" + str);
System.out.println("process3 end");
return str + "3";
}
public static String process4(String str) {
System.out.println("process4 start");
System.out.println("str4:" + str);
System.out.println("process4 end");
return str + "4";
}
}
输出:
process1 start
str1:test
process1 end
process2 start
str2:
op :
process2 end
process3 start
str3:
process3 end
process4 start
str4:
process4 end
op:test1234
问题是:期望的答案正在处理所有4个方法,但问题是:它没有打印参数“str”的值。为什么?
如果我将代码中的:
op = process1("test");
op = process2(op);
op = process3(op);
op = process4(op);
替换为:
process1("test").concat(process2(op).concat(process3(op)).concat(process4(op)));
那么它会打印参数“str”的值。
请回答下面的代码问题,同时建议我一些适用于这种情况的好代码。
英文:
warm greetings.
I am having a scenario that i have 10 methods i.e. process1, process2..., process10
according to input i need to execute few methods out of 10 in specific order.
1)1,3,5,7
2)2,4,6,8
3)1,5,9,10
whatever order i am executing, output of 1st method should be used as the input of second method and so on.
i searched a lot in internet but didn't found anything suitable. I started doing below code.
as of now i am using concat method of java to achieve this.
public class Perform {
static String op = "";
public static void main(String[] args) {
process(list);
}
public static void process(List<String> list)
{
for(String str: list)
{
if(str.startsWith("Samsung"))
{
process1("test").concat(process2(op).concat(process3(op)).concat(process4(op)));
}
}
}
public static String process1(String str)
{
System.out.println("process1 start");
System.out.println("str1:"+str);
System.out.println("process1 end");
return str+"1";
}
public static String process2(String str)
{
System.out.println("process2 start");
System.out.println("str2:"+str);
System.out.println("process2 end");
return str+"2";
}
public static String process3(String str)
{
System.out.println("process3 start");
System.out.println("str3:"+str);
System.out.println("process3 end");
return str+"3";
}
public static String process4(String str)
{
System.out.println("process4 start");
System.out.println("str4:"+str);
System.out.println("process4 end");
return str+"4";
}
}
output :
process1 start
str1:test
process1 end
process2 start
str2:
op :
process2 end
process3 start
str3:
process3 end
process4 start
str4:
process4 end
op:test1234
the answer is expected and processing all 4 methods but the issue is : it is not printing the value of "str" which is the parameter. why ?
if i am keeping the code
op = process1("test");
op = process2(op);
op = process3(op);
op = process4(op);
in place of
process1("test").concat(process2(op).concat(process3(op)).concat(process4(op)));
then it is printing the value of str which is an parameter.
Please answer the question from below code as well as suggest me some good code for this scenario.
答案1
得分: 0
我已找到解决方案。我开发了以下代码来解决这个问题。如果有人有更好的解决方案,请告诉我。
public class Perform {
private static List<Integer> SamsungMethodExecution = Arrays.asList(1,3,5,7);
private static List<Integer> NokiaMethodExecution = Arrays.asList(2,4,6,8);
private static List<Integer> LgMethodExecution = Arrays.asList(1,5,9,10);
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Lgmsg");
list.add("Samsungmsg");
list.add("Nokiamsg");
list.add("Lgmsg");
list.add("Samsungmsg");
list.add("Nokiamsg");
list.add("Lgmsg");
list.add("Samsungmsg");
list.add("Nokiamsg");
process(list);
}
public static void process(List<String> list) {
for(String str: list) {
if(str.startsWith("Samsung")) {
System.out.println("SAMSUNG======================================");
Process.process(SamsungMethodExecution, str.replace("Samsung", ""));
System.out.println("SAMSUNG======================================");
System.out.println();
} else if(str.startsWith("Nokia")) {
System.out.println("NOKIA========================================");
Process.process(NokiaMethodExecution, str.replace("Nokia", ""));
System.out.println("NOKIA========================================");
System.out.println();
} else {
System.out.println("LG===========================================");
Process.process(LgMethodExecution, str.replace("Lg", ""));
System.out.println("LG===========================================");
System.out.println();
}
}
}
}
public class Process {
public static String process(List<Integer> processList, String msg) {
String output = "";
for(int i: processList) {
switch(i) {
case 1:
output = Process.process1(msg); break;
case 2:
output = Process.process2(msg); break;
case 3:
output = Process.process3(msg); break;
case 4:
output = Process.process4(msg); break;
case 5:
output = Process.process5(msg); break;
case 6:
output = Process.process6(msg); break;
case 7:
output = Process.process7(msg); break;
case 8:
output = Process.process8(msg); break;
case 9:
output = Process.process9(msg); break;
case 10:
output = Process.process10(msg); break;
default :
System.out.println("对不起,找不到特定的匹配");
}
}
return output;
}
// 各个 processX 方法的定义
}
两个类,一个是主类(Perform),另一个是实际逻辑的过程类(Process)。
英文:
I have found the solution. I developed below code to solve it. Please let me know if anybody is having a better solution for this.
public class Perform {
private static List<Integer> SamsungMethodExecution = Arrays.asList(1,3,5,7);
private static List<Integer> NokiaMethodExecution = Arrays.asList(2,4,6,8);
private static List<Integer> LgMethodExecution = Arrays.asList(1,5,9,10);
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Lgmsg");
list.add("Samsungmsg");
list.add("Nokiamsg");
list.add("Lgmsg");
list.add("Samsungmsg");
list.add("Nokiamsg");
list.add("Lgmsg");
list.add("Samsungmsg");
list.add("Nokiamsg");
//
process(list);
}
public static void process(List<String> list)
{
for(String str: list)
{
if(str.startsWith("Samsung"))
{
System.out.println("SAMSUNG======================================");
Process.process(SamsungMethodExecution, str.replace("Samsung", ""));
System.out.println("SAMSUNG======================================");
System.out.println();
}else if(str.startsWith("Nokia"))
{
System.out.println("NOKIA========================================");
Process.process(NokiaMethodExecution, str.replace("Nokia", ""));
System.out.println("NOKIA========================================");
System.out.println();
}else
{
System.out.println("LG===========================================");
Process.process(LgMethodExecution, str.replace("Lg", ""));
System.out.println("LG===========================================");
System.out.println();
}
}
}
public class Process {
public static String process(List<Integer> processList, String msg) {
String output = "";
for(int i: processList)
{
switch(i)
{
case 1:
output = Process.process1(msg); break;
case 2:
output = Process.process2(msg); break;
case 3:
output = Process.process3(msg); break;
case 4:
output = Process.process4(msg); break;
case 5:
output = Process.process5(msg); break;
case 6:
output = Process.process6(msg); break;
case 7:
output = Process.process7(msg); break;
case 8:
output = Process.process8(msg); break;
case 9:
output = Process.process9(msg); break;
case 10:
output = Process.process10(msg); break;
default :
System.out.println("Sorry, no specific match found");
}
}
return output;
}
public static String process1(String msg)
{
System.out.println("process1: "+msg);
return msg;
}
public static String process2(String msg)
{
System.out.println("process2: "+msg);
return msg;
}
public static String process3(String msg)
{
System.out.println("process3: "+msg);
return msg;
}
public static String process4(String msg)
{
System.out.println("process4: "+msg);
return msg;
}
public static String process5(String msg)
{
System.out.println("process5: "+msg);
return msg;
}
public static String process6(String msg)
{
System.out.println("process6: "+msg);
return msg;
}
public static String process7(String msg)
{
System.out.println("process7: "+msg);
return msg;
}
public static String process8(String msg)
{
System.out.println("process8: "+msg);
return msg;
}
public static String process9(String msg)
{
System.out.println("process9: "+msg);
return msg;
}
public static String process10(String msg)
{
System.out.println("process10: "+msg);
return msg;
}
}
Two classes one is main class and another is Process class which is having a actual logic.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论