简体   繁体   English

谁能帮助我修改Java代码?

[英]Can anyone help me modify my code in Java?

Okay to make this clearer this is what i need done to my entire program. 好的,这很清楚,这是我需要对整个程序进行的操作。 I need main to restore any calculators that are in the file "calc.bak" before presenting the user with a menu and main should save all calculators that exist in that same file (overwrite as appropriate) just before exiting. 在向用户展示菜单之前,我需要main还原文件“ calc.bak”中的所有计算器,并且main应该在退出前保存该文件中存在的所有计算器(适当覆盖)。 I also need to give the user the option to create a brand new calculator, name it, and add a function to it from an already existing collection (which are my Add, multiply, and divide). 我还需要为用户提供一个选项,以创建一个全新的计算器,为其命名并从一个已经存在的集合(即我的“加,乘和除”)中向其中添加一个函数。 I also need the user to create their own function by designing and naming a combination of any pair of functions. 我还需要用户通过设计和命名任意一对功能的组合来创建自己的功能。 The ouput of the first function would necessarily be input to the second function. 第一功能的输出将必须输入到第二功能。 For example, the user can create a new function called "addmult" that calls the add function (which prompts for two numbers and adds them) and establishes the sum as one of the operands for the multiply function (which would have to prompt for its second operand). 例如,用户可以创建一个名为“ addmult”的新函数,该函数调用add函数(提示输入两个数字并将它们相加),并将和确定为乘法函数的操作数之一(必须提示输入该数字)第二个操作数)。

The sample out put should look like this: Welcome to the Calculator Configurator 输出的示例应如下所示:欢迎使用Calculator Configurator

  1. restore a calculator from a file 从文件还原计算器

  2. create a calculator from scratch 从头开始创建计算器

  3. let me create a calculator you can use 让我创建一个可以使用的计算器

2 2

OK. 好。 so you want to create one yourself. 所以你想自己创造一个。

What is the name of your calculator? 您的计算器叫什么名字? Fred 弗雷德

Indicate which functions from our stock you'd like to add to your calculator (enter 0 to quit this menu): 表示你想添加到您的计算器(输入0退出这个菜单),它的功能从我们的股票:

Add(1) 加(1)

Multiply(2) 相乘(2)

Divide(3) 除(3)

Pair of functions(4) 一对功能(4)

input (2) 输入(2)

Indicate which functions from our stock you'd like to add to your calculator (enter 0 to quit this menu): 表示你想添加到您的计算器(输入0退出这个菜单),它的功能从我们的股票:

Add

Multiply

Divide 划分

Pair of functions 一对功能

1 Indicate which functions from our stock you'd like to add to your calculator (enter 0 to quit this menu): Add 1指明您想从我们的库存中将哪些功能添加到计算器(输入0退出此菜单):添加

Multiply

Divide 划分

Pair of functions 一对功能

4 4

Provide a name for this pair: 提供此对的名称:

BothAddAndMult 两者相加

Provide a description for this pair: 提供此对的描述:

multiplies and then adds 相乘然后相加

Which function should be first? 首先应该使用哪个功能?

Multiply(0) 乘(0)

Add(1) 加(1)

0 0

Which function should be first? 首先应该使用哪个功能?

Multiply

Add

1 1个

Indicate which functions from our stock you'd like to add to your calculator (enter 0 to quit this menu): 表示你想添加到您的计算器(输入0退出这个菜单),它的功能从我们的股票:

Add

Multiply

Divide 划分

Pair of functions 一对功能

4 4

Provide a name for this pair: 提供此对的名称:

MultAfter 之后

Provide a description for this pair: multiplies last after a multiply and add 提供对此对的描述:在相乘后最后相乘并相加

Which function should be first? 首先应该使用哪个功能?

Multiply

Add

BothAddAndMult 两者相加

2 2

Which function should be first? 首先应该使用哪个功能?

Multiply

Add

BothAddAndMult 两者相加

Indicate which functions from our stock you'd like to add to your calculator (enter 0 to quit this menu): 表示你想添加到您的计算器(输入0退出这个菜单),它的功能从我们的股票:

Add

Multiply

Divide 划分

Pair of functions 一对功能

0 0

I am a calculator named Fred 我是一个叫弗雷德的计算器

  1. quit 放弃

  2. clear memory 清除记忆

  3. multiply two numbers 两个数字相乘

  4. add two numbers 加两个数字

  5. multiplies and then adds 相乘然后相加

  6. multiplies last after a multiply and add 乘法和加法后最后相乘

Can someone help me reach this output? 有人可以帮助我达到此输出吗?

Here is my code so far 到目前为止,这是我的代码

CalcConfig.java CalcConfig.java

import java.util.Scanner;

// program to model a configurable calculator
public class CalcConfig {
 public static void main(String [] args)
 {
   System.out.println("Welcome to the Calculator Configurator");
   Scanner kbd = new Scanner(System.in);
   CalcFunction [] funs = {
     new Add(kbd, "add two numbers"),
     new Multiply(kbd, "Multiply two numbers"),
     new Divide(kbd, "divide two numbers")};
   Calculator calc = new Calculator(funs);
   calc.go(kbd);

}
}

Calculator.java 计算器.java

//my Calculator class

import java.util.Scanner;

// models a configurable calcuator
public class Calculator {
   private CalcFunction [] functions;
   private double memory = 0;
   private boolean clear = true;

public Calculator(CalcFunction [] functions)
{
   this.functions = functions;
}

public void go(Scanner kbd)
{
   int choice = 0;
   do
   {
      System.out.println(this);
      choice = kbd.nextInt();
      if (choice == 0) return; // choice is to quit
      if (choice == 1)
      {
         clear = true;
         continue;
      }
      if (choice < 0 || choice >= 5)
       {
          System.out.println("error");
          continue;
       }
      if (!clear)
       {
          System.out.print("use memory [" + memory + "] (y or n)? ");
          String ans = kbd.next();
          if (ans.equals("n")) clear = true;
       }
      if (clear)
       memory = functions[choice-2].doit();
     else
       memory = functions[choice-2].doit(memory);
     clear = false;
     System.out.println(memory);
} while(choice != 0);
}

public String toString()
{
    String out = "0. quit\n1. clear memory\n";
    for (int i=0; i<functions.length; i++)
    out += (i+2) + ". " + functions[i] + "\n";
    return out;
}
}

CalcFunction.java CalcFunction.java

//my CalcFunction class
import java.util.Scanner;

// generic class to model a function in a calculator
public abstract class CalcFunction {
       private Scanner kbd;
       private String description;

public CalcFunction(Scanner kbd, String description)
{
       this.kbd = kbd;
       this.description = description;
}

public abstract double doit();
public abstract double doit(double memory);

// get a string from the user
protected String getString(String prompt)
{
     System.out.print(prompt);
     return kbd.next();
}
// get a number from the user
protected double getNum(String prompt)
{
     System.out.print(prompt);
     while(!kbd.hasNextDouble())
     {
         System.out.print("Invalid: need a number: ");
         kbd.next(); // discard invalid input
     }
     return kbd.nextDouble();
}

public String toString()
{
  return description;
}
}

Add.java 添加.java

//just one of my functions(Add)
import java.util.Scanner;

// class to encapsulate adding two numbers
public class Add extends CalcFunction {
public Add(Scanner kbd, String description)
{
     super(kbd, description);
}

public double doit()
{
    double n1 = this.getNum("Enter a number: ");
    return this.doit(n1);
}

public double doit(double first)
{
    double n2 = this.getNum("Enter a second number: ");
    double answer = first + n2;
    return answer;
}
}

Please help if you can. 如果可以的话请帮忙。 Thanks! 谢谢!

Pseudo-code: 伪代码:

// 1 maps to add, 2 to subtract, etc

while(cur_func = scanner.read != sentinal value) {
    switch(cur_func) { 
        1:
            //toss add into the functions set
            break
        .
        .
        .
    }
}

toArray your set and build the calc

If you want the user to be able to dynamically add calculator functions to a calculator, then you will need something like this: 如果希望用户能够将计算器功能动态添加到计算器,则将需要以下内容:

public void addCalcFunction(CalcFunction newCalcFunction) {
    this.functions.add(newCalcFunction);
}

...but that will require this.functions to be modified to a Collection<CalcFunction> ...但是这需要将this.functions修改为Collection<CalcFunction>

If you want the user to refer to these functions by name, then you'll need to map them: 如果要用户按名称引用这些功能,则需要映射它们:

private Map<String, CalcFunction> functions;

... ...

public void addCalcFunction(String functionName, CalcFunction newCalcFunction) {
    this.functions.put(functionName, newCalcFunction);
}

Using a Map will require you to make further changes, but I'm sure you can figure those out. 使用Map将需要您进行进一步的更改,但是我敢肯定您可以解决这些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM