程序错误无法结束程序

时间:2016-04-21 04:45:17

标签: java

主类

import java.util.Scanner;

class Calculator
{
    public static void main(String args[])
    {
     Scanner input = new Scanner(System.in);
     Addition objecta = new Addition ();
     Multiplacation objectmu = new Multiplacation();
     Division objectd = new Division();
     Minus objectmi = new Minus();

     System.out.println("Enter operation");
     System.out.println("1.Addition");
     System.out.println("2.Multiplacation");
     System.out.println("3.Division");
     System.out.println("4.Subtraction");

     input.nextLine();
     int test1 = 1;
     int test2 = 2;
     int test3 = 3;
     int test4 = 4;
     if (test1 == 1)
    {
     Addition plus = new Addition();
     plus.add();
    } 
    if (test2 == 2)
    {
     Multiplacation multi = new Multiplacation();
     multi.multiply();
    }
    if (test3 == 3)
    {
     Division div = new Division();
     div.divide();
    }
    if (test4 == 4)
    {
     Minus mi = new Minus();
     mi.subtract();
    }
    }
}

import java.util.Scanner;

class Addition
{
    public static void add()
    {
        Scanner bob = new Scanner(System.in);
        double fnum, snum, answer;
        System.out.println("Eneter First Number");
        fnum = bob.nextDouble();
        System.out.println("Eneter Second Number");
        snum = bob.nextDouble();
        answer = fnum + snum;
        System.out.println(answer);
    }
}

import java.util.Scanner;

class Multiplacation
{
        public static void multiply()
    {
        Scanner bob = new Scanner(System.in);
        double fnum, snum, answer;
        System.out.println("Eneter First Number");
        fnum = bob.nextDouble();
        System.out.println("Eneter Second Number");
        snum = bob.nextDouble();
        answer = fnum * snum;
        System.out.println(answer);
    }
}

import java.util.Scanner;

class Division
{
        public static void divide()
    {
        Scanner bob = new Scanner(System.in);
        double fnum, snum, answer;
        System.out.println("Eneter First Number");
        fnum = bob.nextDouble();
        System.out.println("Eneter Second Number");
        snum = bob.nextDouble();
        answer = fnum / snum;
        System.out.println(answer);
    }
}

import java.util.Scanner;

class Minus
{
        public static void subtract()
    {
        Scanner bob = new Scanner(System.in);
        double fnum, snum, answer;
        System.out.println("Eneter First Number");
        fnum = bob.nextDouble();
        System.out.println("Eneter Second Number");
        snum = bob.nextDouble();
        answer = fnum - snum;
        System.out.println(answer);
    }
}

问题

我正在制作一个计算器,我是计算机编程的新手。我让程序工作,但唯一的问题是我无法让程序停止。它回答了一个问题,然后连续开始另一个问题任何想法如何阻止它请评论。

4 个答案:

答案 0 :(得分:3)

我认为您需要了解如何接受用户的输入。

在您的代码中,这是错误的:

 input.nextLine();
 int test1 = 1;
 int test2 = 2;
 int test3 = 3;
 int test4 = 4;

因为稍后,您检查test1是否为1,test2是否为2等等。由于您未更改这些变量的值,因此您的条件将始终评估为真。

正确的方法是:

int userInput = input.nextInt();

然后检查userInput变量:

if (userInput == 1) {
    ...
}

if (userInput == 2) {
    ...
}

etc

提示:在这种情况下,您应该使用else ifs:

if (userInput == 1) {
    ...
} else if (userInput == 2) {
    ...
} else if (userInput == 3) {
    ...
} else if (userInput == 4) {
    ...
}

其他改进:

你不应该创建多个扫描仪,一个就足够了。

您可以在Calculator课程中声明扫描程序:

class Calculator {
    public static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        ...
    }
}

在其他课程中,请使用Calculator.input而不是创建新的扫描仪。我给你举个例子:

class Addition
{
    public static void add()
    {
        double fnum, snum, answer;
        System.out.println("Eneter First Number");
        fnum = Calculator.input.nextDouble();
        System.out.println("Eneter Second Number");
        snum = Calculator.input.nextDouble();
        answer = fnum + snum;
        System.out.println(answer);
    }
}

答案 1 :(得分:1)

这个逻辑

if (test1 == 1)

if (test2 == 2)

总是如此。

你应该比较

int test = input.nextInt();

if (test == test1) {  // etc

答案 2 :(得分:1)

您需要检查用户提供的输入,因为那只是一个 if / if-else是最好的方法...

if (userInput == 1) {
     Addition plus = new Addition();
     plus.add();
} else if (userInput == 2) {
    ...your code 
} else if (userInput == 3) {
    ......your code 
} else if (userInput == 4) {
    ......your code 
}

这里当数学完成时,你应该再次询问用户另一个输入... 并且一个扫描仪对象不仅仅是好的,你不需要定义多个......

答案 3 :(得分:0)

您可以更改下面提到的计算器类,以便在完成执行时请求选项。在类似情况下,您可以使用switch case而不是if条件。在用户需要时,将退出选项添加到终止程序的选项列表中。

    class Calculator
{
    public static void main(String args[])
    {
     Scanner input = new Scanner(System.in);
     Addition objecta = new Addition ();
     Multiplacation objectmu = new Multiplacation();
     Division objectd = new Division();
     Minus objectmi = new Minus();

     while(true){

     System.out.println("Enter operation");
     System.out.println("1.Addition");
     System.out.println("2.Multiplacation");
     System.out.println("3.Division");
     System.out.println("4.Subtraction");
        System.out.println("5.Exit");


     switch(input.nextLine()){
         case "1":
             Addition plus = new Addition();
             plus.add();
             break;
         case "2":
             Multiplacation multi = new Multiplacation();
             multi.multiply();
             break;
         case "3":
             Division div = new Division();
             div.divide();
             break;
         case "4":
             Minus mi = new Minus();
             mi.subtract();
             break;
         case "5":
             System.exit(0);
         default:
             System.out.println("Please enter valid option");

     }
     }
     /*input.nextLine();
     int test1 = 1;
     int test2 = 2;
     int test3 = 3;
     int test4 = 4;
     if (test1 == 1)
    {
     Addition plus = new Addition();
     plus.add();
    } 
    if (test2 == 2)
    {
     Multiplacation multi = new Multiplacation();
     multi.multiply();
    }
    if (test3 == 3)
    {
     Division div = new Division();
     div.divide();
    }
    if (test4 == 4)
    {
     Minus mi = new Minus();
     mi.subtract();
    }*/
    }
}