尝试调用另一个类中的方法以在IF语句中使用

时间:2018-11-02 07:12:17

标签: java class methods

我正在尝试在运行IF语句时调用一个类,该语句将询问用户是否要查看英语或西班牙语的程序。西班牙语代码位于不同的类别中,因此我想打电话的是,当/如果用户选择以西班牙语查看该程序并且不理会下面编写的英语代码,则是这样。

Error: Java cannot find symbol
symbol: method Spanish()
location: variable span of type source.Spanish

下面是我的第二堂课(我想打电话):

package source;

import java.util.Scanner;
import java.lang.String;

public class Spanish {
    public Spanish() {}
}

下面是我尝试在主班级中打电话的方式:

if (language.equals("Span")) {
    source.Spanish span = new source.Spanish();
    span.Spanish();
}
else {
   //More code.
}

我第一次问一个问题,所以很抱歉,如果格式不正确或是否已经回答了该问题,我看了过去的一些问题,但到目前为止还算运气。谢谢:)


编辑

package source;

import java.util.Scanner;
import java.lang.String;

public class CashRegister {
public static void main(String[] args) {

   String registerfloat;
   String input;
   String name;
   String anotherTransaction;
   String item2;
   String choice;
   String language = "";
   double balance;
   double cost;
   double change = 0;
   double cash = 0;
   double amountRequired = 0;
   double totalAmount = 0;

   Scanner in = new Scanner(System.in);

   System.out.println("Hello! Welcome to the Cash Register.\n");

   while (true) {
       if (language.equals("Eng"))break;
       if (language.equals("Span"))break;

       else {
           System.out.println("Which language would you like to use? English (Eng) or Spanish (Span)?:");
           language = in.nextLine();
       }
   }

   if(language.equals("Span")) {
       source.Spanish span = new source.Spanish();
       span.Spanish();
   }
   else {

       System.out.print("Please enter cash register's float: $");
       registerfloat = in.nextLine();
       balance = Double.parseDouble(registerfloat);


       boolean loop = true;
       while (loop == true) {
           System.out.println("Do you wish to continue this transaction?: (Yes/No)");
           choice = in.nextLine();
           loop = false;
           switch (choice) {
               case "Yes":

                   System.out.print("Please enter the item's name:\n");
                   input = in.nextLine();
                   name = input;

                   System.out.print("Please enter the item's cost:");
                   input = in.nextLine();
                   cost = Double.parseDouble(input);


                   System.out.println("Do you wish to add another item?: Yes/No");
                   item2 = in.nextLine();

                   while (true) {
                       if (item2.equals("No"))
                           break;
                       else {
                           System.out.print("Please enter the item's name:\n");
                           input = in.nextLine();
                           name = input;

                           System.out.print("Please enter the item's cost:");
                           input = in.nextLine();
                           cost = Double.parseDouble(input);

                           System.out.println("Do you wish to add another item?: Yes/No");
                           item2 = in.nextLine();

                       }
                   }

                   Transaction trans = new Transaction(name, cost);

                   amountRequired = amountRequired + trans.getCost();
                   totalAmount = totalAmount + trans.getCost();

                   System.out.print("Please enter the cash amount tendered: $");
                   input = in.nextLine();
                   cash = cash + Double.parseDouble(input);
                   amountRequired = amountRequired - cash;

                   balance = balance + cash;
                   change = cash - totalAmount;

                   System.out.println("Amount of change required = " + change);
                   loop = true;
                   break;
               case "No":
                   balance = balance - change;
                   System.out.print("Balance of the Cash Register: $" + balance + "\n");

                   System.out.println("\nThank you for using the Cash Register!");

                   System.exit(0);
               default:
                   loop = true;
                   System.out.println("Wrong input, try again!");
                   break;
           }
       }
   }

} }

以上是CashRegister中我的全部代码。 “西班牙文”具有完全相同的代码,除了打印语句是西班牙语而不是英语。另外,如果它很难阅读或里面有不必要的东西,它很抱歉,它是一个小组作业,在早期阶段,所以有点混乱。干杯

更新2

package source;

import java.util.Scanner;

public class RunClass {
public static void main(String[] args) {

    String registerfloat;
    String language = " ";
    double balance;

    Scanner in = new Scanner(System.in);

    System.out.println("Hello! Welcome to the Cash Register.\n");

    System.out.print("Please enter cash register's float: $");
    registerfloat = in.nextLine();
    balance = Double.parseDouble(registerfloat);

    while (true) {
        if (language.equals("Eng")) break;
        if (language.equals("Span")) break;

        else {
            System.out.println("Which language would you like to use? English (Eng) or Spanish (Span)?:");
            language = in.nextLine();
        }
    }

    if (language.equals("Eng")) {
        source.CashRegister.CashRegister();
    }
    else {
        // trying to enter Spanish.register here but it does not even show as its not on the current branch.
    }

}

}

1 个答案:

答案 0 :(得分:0)

在您的Spanish班上,您有

public class Spanish {
    public Spanish() {}
}

但是public Spanish()不是普通的方法,它是一个构造函数,只能使用您已经做过的关键字new进行调用。

if (language.equals("Span")) {
    Spanish span = new Spanish();
    span.Spanish();
}

(由于不需要软件包名称,因此删除了它的名称),因此您需要在Spanish类中执行实际工作的另一个方法。

除了您重复了所有代码外,主要的问题是,您试图在CashRegister的main()方法中进行所有操作

首先,我将创建一个简单的起始类,将其称为Starter来询问语言问题,然后让CashRegisterSpanish类处理主要功能。

class Starter {
    public static void main(String[] args) {
        //Ask question about language as before

        if (language.equals("Span")) {
            Spanish.register();
        } else {
            CashRegister.register();
        }
    }
}

然后在registerCashRegister中都实现一个Spanish方法,该方法包含else之后的代码,请注意,我使方法static < / p>

public class CashRegister {
    public static void register() {
        System.out.print("Please enter cash register's float: $");
        registerfloat = in.nextLine();
        balance = Double.parseDouble(registerfloat);
        //... rest of code
    }
}

这应该可以帮助您入门,但是还有许多进一步的改进可以完成。


如果您不想使用静态方法,则Starter类需要进行如下修改,但是我认为使用静态方法更有意义。

 if (language.equals("Span")) {
     Spanish spanish = new Spanish();
     spanish.register();
 } else {
     CashRegister cashRegister = new CashRegister();
     cashRegister.register();
 }