菜单驱动程序1

时间:2018-02-27 02:42:47

标签: java

我需要编写一个菜单驱动的程序,允许用户输入两个多项式,然后为用户提供一个选项菜单,我已经编写了部分代码来输入两个多项式...但我正在努力使用选项菜单...菜单上的第一个选项是“评估用户指定的x值的第一个多项”。到目前为止,这是我的代码:`

import java.util.Scanner;

public class Polynomial2 {

    public static void main(String[] args) {
        int i, coef, x, deg, count = 2;
        double total = 0.0, result;
        char opt;
        Scanner sc = new Scanner(System.in);

        do {
            System.out.println("Enter Degree");
            deg = sc.nextInt();
            System.out.println("Enter x");
            x = sc.nextInt();

            for (i = 0; i <= deg; i++) {
                System.out.println("Enter Coefficent for " + i);
                coef = sc.nextInt();
                total = total + coef * Math.pow(x, i);
            }
            count--;
        } while (count > 0);
        System.out.println("Total=" + total);

        {
            coef = sc.nextInt();
            result = sc.nextDouble();
            opt = sc.next().charAt(0);

            switch (opt) {
                case 'a':
                    result = total + coef * Math.pow(x, i);
                    System.out.println("Evalute Polynomial 1 at x");
                    break;
                case 'b':
                    result = total + coef * Math.pow(x, i);
                    System.out.println("Evaluate Polynomial 2 at x");
                    break;
                case 'c':
                    result = total + total;
                    System.out.println("Add Polynomial 1 and 2");
                    break;
                case 'd':
                    result = total - total;
                    System.out.println("Subtract Polynomial 1 from Polynomial 2");
                    break;
                case 'e':
                    result = total - total;
                    System.out.println("Subtract Polynomial 2 from Polynomial 1");
                    break;
                case 'f':
                    result = total * x;
                    System.out.println("Multiply Polynomial 1 by a constant");
                    break;
                case 'g':
                    result = total * x;
                    System.out.println("Multiple Polynomial 2 by a constant");
                    break;
            }
        }
    }
}

0 个答案:

没有答案