我的目标是要求用户输入一个操作,它将从另一个进行基本计算的类中计算出来,然后它将得到结果,并要求用户再次选择一个操作,然后继续进行操作数。或选择C并将数字清除为0
import java.util.Scanner;
public class CalculatorClient {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Welcome to calculator\n You will be able to use this program to add, subtract, multiply, and divide values!\n");
double number1;
System.out.println("Enter a number: ");
number1 = keyboard.nextDouble();
Calculator obj1 = new Calculator(number1);
keyboard.nextLine();
// System.out.println("Please enter an operation ( + , - , / , *) :");
// input=keyboard.nextLine();
System.out.println("Please enter an operation ( + , - , / , *) :");
String input = keyboard.nextLine();;
我不确定为什么,但是我用来测试操作员输入的while循环在运行时不起作用。
while (!"+".equals(input) || !"-".equals(input) || !"/".equals(input) || !"*".equals(input)) {
System.out.println("That is not a valid operation, please enter an operation ( + , - , / , *) :");
input = keyboard.nextLine();
}
int counter=0;
Loopy:
while(counter<1)
{
switch (input) {
case "+": {
int add;
System.out.println("Enter a second number to add: ");
add = keyboard.nextInt();
System.out.println("\nThe sum is: ");
System.out.println("The nubmer is now ");
System.out.println(obj1.getNumber());
System.out.println(obj1.add(add));
number1 = obj1.getNumber();
break;
}
case "-": {
int subtract;
System.out.println("Enter a second number to subtract: ");
subtract = keyboard.nextInt();
System.out.println("\nThe difference is: ");
System.out.println(obj1.subtract(subtract));
System.out.println("The nubmer is now ");
System.out.println(obj1.getNumber());
number1 = obj1.getNumber();
break;
}
case "/": {
int div;
System.out.println("Enter a second number to divide");
div = keyboard.nextInt();
System.out.println("\nThe quotient is: ");
System.out.println(obj1.divide(div));
System.out.println("The nubmer is now ");
System.out.println(obj1.getNumber());
number1 = obj1.getNumber();
break;
}
case "*": {
int times;
System.out.println("Enter a second nubmer to multiply");
times = keyboard.nextInt();
System.out.println("\nThe product is: ");
System.out.println("The nubmer is now ");
System.out.println(obj1.getNumber());
System.out.println(obj1.multiply(times));
number1 = obj1.getNumber();
break;
}
case "C": {
obj1.setNumber(number1);
System.out.println("The nubmer is now ");
System.out.println(obj1.getNumber());
break;
}
}
counter--;
由于某些原因,此println最终打印两次
System.out.println("\nPlease enter an operation ( + , - , / , *) :");
input = keyboard.nextLine();
continue;
}
}
}
````
Here is my calculator program (My instruction is to make it on a separate file
````
public class Calculator
{
private double number;
Calculator(double x){number=x;}
Calculator(){}
public void setNumber(double x){number=x;}
public double getNumber(){return number;}
public double add(double a){
number+=a;
return (number);
}
public double subtract(double a){
number-=a;
return (number);
}
public double multiply(double a){
number*=a;
return (number);
}
public double divide(double a){
number/=a;
return (number);
}
}
答案 0 :(得分:0)
我认为您的问题在以下几行中:
int counter = 0;
Loopy:
while(计数器<1)
**方法的逻辑
**最后
反击-;
因此计数器始终小于1,因此始终处于循环状态