计算器,不能让它打印出来,因为我想要它

时间:2015-01-16 11:01:37

标签: java arrays

这是我的作业。但是,我无法得到结果。我希望它打印出来:

> 2*7*6
   2
*  7
----
  14 
*  6
----
  84

等等。无论我输入多少个数字,我都希望代码能够正常工作。到目前为止,这是我的代码;

public static int add(int a, int b) {
    return a + b;
}

public static int sub(int a, int b) {
    return a - b;
}

public static int multiply(int a, int b) {
    return a * b;
}

public static void main(String[] args) 
{

    Scanner in = new Scanner(System.in);

    System.out.print("(ex. 8*2*6): ");
    String amount = in.nextLine(); 

    if ( amount.contains("+") ) {
        String[] parts = amount.split("\\+");
    } else if ( amount.contains("-") ) {
        String[] parts = amount.split("\\-");
    } else if ( amount.contains("*") ) {
        String[] parts = amount.split("\\*");
        int[] results = new int[parts.length];

        // Convert from string to integer
        for (int i = 0; i < parts.length; i++) {
            try {
                results[i] = Integer.parseInt(parts[i]);
            } catch (NumberFormatException nfe) {};
        }

        // Print result
        int counter = 1;
        for (int i = 0; i <= results.length; i++) {
            if ( i == 0) {
            System.out.println("   " + results[i]);
            System.out.println("*  " + results[counter]);
            System.out.println("----");
            int total = multiply(results[i], results[counter]);
            System.out.println("  " +  total);
            } else if ( i > 1 ) {
                System.out.println("*  " + results[i]);
                System.out.println("----");
                System.out.println("  " + multiply(results[i], results[counter]) );
            }
            }

    } else {
        System.out.println("Error");
    }

我做错了什么?

1 个答案:

答案 0 :(得分:1)

不是c#不是吗?我不确定我是否理解你。

在c#中,你有没有试过这样的东西。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args) { 

            //   Scanner in = new Scanner(System.in);
          //  String amount = Console.ReadLine();
            String amount = "2*7*6*5*3*2";

            if (amount.Contains('+')) {
                String[] parts = amount.Split('+');

            }
            else
                if (amount.Contains('-')) {
                    String[] parts = amount.Split('-');
                }
                else if (amount.Contains("*")) {
                    String[] parts = amount.Split('*');
                    int[] results = new int[parts.Length];

                    // Convert from string to integer
                    for (int i = 0; i < parts.Length; i++) {
                        try {
                            results[i] = int.Parse(parts[i]);
                        }
                        catch (FormatException nfe) { };
                    }

                    // Print result      
                    int total = results[0];

                    for (int i = 1; i < results.Length; i++) {

                        if (i == 1)
                            Console.WriteLine("   " + results[i - 1]);

                        Console.WriteLine("*  " + results[i]);
                        Console.WriteLine("----");

                        total = multiply(results[i], total);
                        Console.WriteLine("  " + total);
                    }

                }
                else {
                    Console.WriteLine("Error");
                }

            Console.ReadKey();
        }

        public static int add(int a, int b) {
            return a + b;
        }

        public static int sub(int a, int b) {
            return a - b;
        }

        public static int multiply(int a, int b) {
            return a * b;
        }

        public static void main(String[] args) {


        }
    }

}