如果我在主目录中添加了三个扫描仪,但我的程序仅“正确”运行,但是如果删除它们,则会出现“无法解析符号fnum,snum和total”错误。我该如何从主类的CalculatorClass调用此方法?
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner nameInput = new Scanner(System.in);
NameInputClass nameInputClassObject = new NameInputClass();
System.out.println("Hello! Please enter your name");
String name = nameInput.nextLine();
nameInputClassObject.nameInput(name);
Scanner input1 = new Scanner(System.in);
int fnum = input1.nextInt();
Scanner input2 = new Scanner(System.in);
int snum = input1.nextInt();
Scanner input3 = new Scanner(System.in);
int total = input1.nextInt();
CalculatorClass calculatorClassObject = new CalculatorClass();
calculatorClassObject.calcInput(snum,fnum,total);
public class NameInputClass {
public static void nameInput(String name){
System.out.println("Hello "+name);}}
import java.util.Scanner;
public class CalculatorClass {
public static void calcInput(int fnum, int snum, int total){
System.out.println("Give first number");
Scanner firstNumber = new Scanner(System.in);
fnum = firstNumber.nextInt();
System.out.println("Give second number");
Scanner secondNumber = new Scanner(System.in);
snum = firstNumber.nextInt();
total = fnum + snum;
System.out.println("your total = " +total);
}
}
答案 0 :(得分:0)
您将创建Main对象。 Main main = new Main();
但是我建议您应该正确分离所有对象和功能。创建类和对象的正确方法。
然后在您的主站中全部调用。