简体   繁体   English

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

[英]Trying to call a method in another class to use in a IF statement

I am trying to call a class when running an IF statement which will ask the user if they want to view the program in English or Spanish.我试图在运行 IF 语句时调用一个类,该语句将询问用户是否想用英语或西班牙语查看程序。 The Spanish Code is in a different class and I want to call is so when/if the user chooses to view the program in Spanish and disregards the English code written below.西班牙语代码在不同的类别中,我想在用户选择以西班牙语查看程序并忽略下面写的英文代码时调用它。

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

Below is my second class(that I want to call):下面是我的第二堂课(我想打电话):

package source;

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

public class Spanish {
    public Spanish() {}
}

And below is how I am trying to call in my main class:下面是我试图在我的主类中调用的方式:

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

My first time asking a question so I am sorry if the format isn't right or if the question has already been answered, I looked at a few of the past questions but no luck so far.我第一次提出问题,所以如果格式不正确或者问题已经得到回答,我很抱歉,我查看了过去的一些问题,但到目前为止没有运气。 Thank you :)谢谢 :)


EDIT编辑

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;
           }
       }
   }

} } } }

Above is my entire code from CashRegister.以上是我来自 CashRegister 的完整代码。 "Spanish" has the EXACT same code except the print statements are in Spanish rather than English. “西班牙语”具有完全相同的代码,除了打印语句是西班牙语而不是英语。 Also, sorry if its hard to read or there is unnecessary stuff in there, its a group assignment and in early stages so is a bit of a mess.另外,对不起,如果它很难阅读或那里有不必要的东西,它是一个小组作业,在早期阶段所以有点混乱。 Cheers干杯

Update 2更新 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.
    }

}

} }

In your Spanish class you have在你的Spanish课上,你有

public class Spanish {
    public Spanish() {}
}

but public Spanish() is not an ordinary method, it's a constructor that only can be called with the keyword new which you already do.但是public Spanish()不是一个普通的方法,它是一个构造函数,只能用你已经做过的关键字new来调用。

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

(I removed the package name since it isn't needed) So you need another method in your Spanish class that does the actual work. (我删除了包名,因为它不需要)所以你需要在你的Spanish课中使用另一种方法来完成实际工作。

The main problem though, apart from the fact that you have duplicated all the code, is that you are trying to do everything in the main() method of CashRegister但是,主要问题是,除了您复制了所有代码这一事实之外,您正在尝试在CashRegister的 main() 方法中执行所有CashRegister

I would, for a start, create a simple starting class, lets call it Starter that ask the language question and then lets the CashRegister or Spanish class handle the main functionality.首先,我会创建一个简单的起始类,将其称为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();
        }
    }
}

And then you implement a register method in both CashRegister and Spanish that contain the code after else , note that I made the method static然后你在CashRegisterSpanish中实现了一个register方法,其中包含else之后的代码,请注意我将方法CashRegister static

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
    }
}

This should get you started but there are many further improvements that can be done.这应该会让你开始,但还有许多可以做的进一步改进。


If you don't want to have a static method the Starter class needs to be modified as below but I think a static method makes more sense.如果您不想拥有静态方法,则需要按如下方式修改Starter类,但我认为静态方法更有意义。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM