ОК, поэтому я работал над калькулятором с классами (для игры с классами, но с функцией), и когда когда-либо я запускаю все, я вернусь к нулю, независимо от того, что я набираю или говорю, чтобы использовать для Оператор. Вот мой код:Java Calculator с классами
Основной класс:
import java.util.Scanner;
//numof = number of numbers in array
// numarrays = the array for user input
// finial = finial number aka the answer
public class Calculator {
public static double finial;
/**
* @return the finial
*/
public static double getFinial() {
return finial;
}
/**
* @param numof the finial to set
*/
public static void setFinial(double finial) {
finial = numof;
}
public static int numof;
/**
* @return the numof
*/
public static int getNumof() {
return numof;
}
/**
* @param numof the numof to set
*/
public static void setNumof(int numof) {
numof = numof;
}
public static double[] numarrays;
/**
* @return the numarrays
*/
public static double[] getNumarrays() {
return numarrays;
}
/**
* @param numarrays the numarrays to set
*/
public static void setNumarrays(double[] numarrays) {
numarrays = numarrays;
}
@SuppressWarnings("resource")
public static void main (String[] args) {
System.out.println("Hello and welcome to my calculator, in this calculator you can add, subtract or multiply");
System.out.println("For the next step I need to know how many numbers you would like to input? ");
int numof;
Scanner numofnums= new Scanner(System.in);
numof = numofnums.nextInt();
Calculator.setNumof(numof);
System.out.println("So next you are going to input the numbers");
double[] numarrays = new double[numof];
for (int k=0; k < numof; k++){
System.out.println("Please enter number");
Scanner input = new Scanner(System.in);
numarrays[k] = input.nextDouble();
}
Calculator.setNumarrays(numarrays);
System.out.println("Please enter what you would like to do with these numbers add,subtract,avg,multiply");
Scanner OP = new Scanner(System.in);
String OPerator= OP.next();
if (OPerator.equals ("add")){
Add.adding();
}
else if (OPerator.equals ("subtract")){
subtract.subtracting();
}
else if (OPerator.equals ("multiply")){
multiply.multiplying();
}
else if (OPerator.equals ("avg")){
avg.avging();
}
System.out.println("The answer is " + Calculator.getFinial());
}
}
здесь оный класс:
public class Add extends Calculator {
public static void adding() {
double finial = 0;
for (int h = 0; h < Calculator.getNumof(); h++){
finial = finial + Calculator.getNumarrays()[h];
}
Calculator.setFinial(finial);
}
}
У меня есть еще три класса, но это просто оператор классы, дайте мне знать, если вам нужно их
Это отличная возможность ознакомиться с использованием отладчика. Используя один из них, вы можете последовательно перебирать код по мере его выполнения и наблюдать за поведением и значениями времени выполнения. Это позволит вам увидеть, где логика не делает то, что вы ожидаете, и сузить проблему. – David