gpt4 book ai didi

java - 菜单驱动的 Java 可在 3 个类别之间进行选择。如何将所有这些类合并到一个类中?

转载 作者:行者123 更新时间:2023-12-02 02:12:20 25 4
gpt4 key购买 nike

这是梁的 Java 书,最后的作业。这是 3 个不同的问题。我让每个人都能完美地独立工作。但我要在菜单驱动选项中使用一个文件,其中每个选项都会调用其分配到的类。

菜单可以工作,并且选择选项 1 后就可以工作。但剩下的 2 个我在重命名它们时遇到了麻烦,这样它们就可以被调用。我假设它们不起作用,因为它们有自己的 main(String[] args) 并且我们不能有这些重复项或有多个“main”。我的问题是如何重命名最后两个以便它们可以工作?

import java.io.File;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Stack;
import java.util.TreeSet;


public class BorjaTask7 {



private static Scanner input;
public static void main(String[] args) {



input = new Scanner(System.in);

int option = 0;
while (option !=4) {
System.out.print(
"Main menu\n" +

"1: infix to Postfix\n" +
"2: Postfix Expressions\n" +
"3: Descending Order\n" +
"4: Exit\n" + "\n" +
"Enter a choice: ");

option = input.nextInt();

switch (option) {
case 1: System.out.println("Infix Expression Postfix Expression");
System.out.println(" (1 + 2) * 3 "
+ infixToPostfix("(1 + 2) * 3"));
System.out.println(" 2 * (1 + 3) "
+ infixToPostfix("2 * (1 + 3)"));
break;

case 2: System.out.println("result = " + postfix.pop());
break;

case 3: System.out.println(iterator.next());
break;
}
}
}

private static String infixToPostfix(String expression) {

LinkedList<String> operatorList = new LinkedList<>();

// Create a Linked list to store operands
LinkedList<String> resultList = new LinkedList<>();

// Create a stack to store '('
Stack<Character> stack = new Stack<>();

// Insert blanks around (, ), +, -, /, and *
expression = insertBlanks(expression);

// Extract operands and operators
String[] tokens = expression.split(" ");

// Scan tokens
for (String token: tokens) {
if (token.length() == 0) // Blank space
continue; // Back to the while loop to extract the next token
else if (token.charAt(0) == '(') // Push '(' onto the stack
stack.push(token.charAt(0));
else if (!stack.isEmpty() && stack.peek() == '(' &&
token.charAt(0) != ')') {
// Place operators within "( )" and the
// front to the front of the operatorList
if (Character.isDigit(token.charAt(0)))
resultList.addLast(token);
else if (token.charAt(0) == '+' || token.charAt(0) == '-' ||
token.charAt(0) == '*' || token.charAt(0) == '/' )
operatorList.addFirst(token);
}
else if (!stack.isEmpty() && token.charAt(0) == ')') {
// Add the operatorList to the result
resultList.addAll(operatorList);
operatorList.clear();
stack.pop();
}
else if (token.charAt(0) == '+' || token.charAt(0) == '-')
operatorList.addLast(token); // Add +, - to the end of list
else if (token.charAt(0) == '*' || token.charAt(0) == '/')
operatorList.addFirst(token); // Add +, - to the front of list
else if (Character.isDigit(token.charAt(0)))
resultList.addLast(token); // Add digits to result list
}

// Format the result string
String result = "";
resultList.addAll(operatorList);
for (String e: resultList) {
result += e + " ";
}

// return result
return result;
}

/** Method Inserts blanks around (, ), +, -, /, and *. */
public static String insertBlanks(String s) {
String result = "";

for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(' || s.charAt(i) == ')' ||
s.charAt(i) == '+' || s.charAt(i) == '-' ||
s.charAt(i) == '*' || s.charAt(i) == '/')
result += " " + s.charAt(i) + " ";
else
result += s.charAt(i);
}

return result;
}

// second class here
public static void main(String[] args) throws IllegalArgumentException {

String[] tokens;
switch (arg.length) {
case 0: throw new IllegalArgumentException("Usage: 1 5 * 10");
case 1: tokens = arg[0].split(" "); break;
default:
tokens = arg;
}

Stack<Double> postfix = new Stack<>();

for (String token : tokens) {

if (isOperator(token.charAt(0))) {
evaluate(postfix, token.charAt(0));
} else {
postfix.push(Double.parseDouble(token));
}

}

System.out.println("result = " + postfix.pop());
}

private static void evaluate(Stack<Double> postfix, char operator) {
double num2 = postfix.pop();
double num1 = postfix.pop();
switch (operator) {
case '+':
postfix.push(num1 + num2); break;
case '-':
postfix.push(num1 - num2); break;
case '/':
postfix.push(num1 / num2); break;
case '*':
postfix.push(num1 * num2); break;
}
}

private static boolean isOperator(char ch) {
return (ch == '/' ||
ch == '+' ||
ch == '-' ||
ch == '*');
}


// 3rd class


public class Task1 {
private static Scanner in;

public static void main(String[] args) throws Exception {
//

if (args.length != 1) {
System.out.println("Usage: TextFile");
System.exit(0);

}
String filename = args[0];

//TreeSet<String>treeSet = new TreeSet<String>();

TreeSet<String> treeSet = new TreeSet<String>(Collections.reverseOrder());
try
{
in = new Scanner(new File(filename));
String line;
while ((line = in.nextLine())!=null)
{
String[] tokens = line.split("[|\n|\t|\r|.|,|)|(|-|\"]");



for (int i = 0; i < tokens.length; i++)

treeSet.add(tokens[i]);

}
}
catch (Exception ex)
{

}



System.out.println("\nDisplay words in descending order");


Iterator<String> iterator = treeSet.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
}

为了方便起见,我放置了 class2 和 class3,这样你就可以看到里面有什么。

任何帮助将不胜感激。

最佳答案

将所有功能从所有三个 main() 方法中移出并移至同一类的公共(public)方法中。每个类仍然可以拥有各自的 main() 方法(仅委托(delegate)给本地公共(public)方法来实现功能),但对于最终项目,创建第四个类,其中仅包含一个 main() 方法,该方法调用其他三个类的公共(public)方法基于用户的菜单选择的类(class)。

static void main(String[] args) {

Assignment01Class asgn01 = new Assignment01Class();
Assignment02Class asgn02 = new Assignment02Class();
Assignment03Class asgn03 = new Assignment03Class();

String userInput = // code to get user input
String choice = // code to get user choice
String result = '';
try {
switch(choice) {
case 1: result = asgn01.infixToPostfix(userInput); break;
case 2: result = asgn02.postfixExpressions(userInput); break;
case 3: result = asgn03.descendingOrder(userInput); break;
}
}
catch (Exception e) {
// exception handling code
}

// code to show result
}

关于java - 菜单驱动的 Java 可在 3 个类别之间进行选择。如何将所有这些类合并到一个类中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49820346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com