主类
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);
}
}问题
我正在做一个计算器,我对计算机编程是个新手。我让程序工作了,但唯一的问题是我不能让程序停止。它回答一个问题,然后不断地启动另一个问题。任何关于如何阻止它的想法,请评论。
发布于 2016-04-21 12:53:41
我认为您需要了解如何接受用户的输入。
在你的代码中,这是错误的:
input.nextLine();
int test1 = 1;
int test2 = 2;
int test3 = 3;
int test4 = 4;因为稍后,您将检查test1是否为1,test2是否为2,等等。由于您没有更改这些变量的值,因此您的条件将始终计算为true。
正确的做法是:
int userInput = input.nextInt();然后检查userInput变量:
if (userInput == 1) {
...
}
if (userInput == 2) {
...
}
etc提示:在这种情况下,你应该使用else if:
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);
}
}发布于 2016-04-21 12:50:25
这个逻辑
if (test1 == 1)和
if (test2 == 2)永远都会是真的。
你应该比较一下
int test = input.nextInt();
if (test == test1) { // etc发布于 2016-04-21 13:09:36
您需要检查用户提供的输入,因为这只是一个输入
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
}在这里,当数学计算完成时,你应该再次要求用户输入另一个输入...而且一个扫描器对象就足够了,你不需要定义多个扫描器对象。
https://stackoverflow.com/questions/36759869
复制相似问题