Java分数计算器,全局变量?

时间:2013-11-26 04:09:39

标签: java eclipse calculator

这是我第二次提出这个问题,因为这个作业将在明天到期,我仍然不清楚如何在我的代码中取得进展!我在AP计算机编程课程中,所以我是一个完全的初学者。我的目标(到目前为止)是将两个分数相乘。有没有办法在另一个方法中在该方法之外的特定方法中使用变量?我希望不要混淆,谢谢!

import java.util.Scanner;
import java.util.StringTokenizer;

public class javatest3 {
    static int num1 = 0;
    static int num2 = 0;
    static int denom1 = 0;
    static int denom2 = 0;
    public static void main(String[] args){
    System.out.println("Enter an expression (or \"quit\"): "); //prompts user for input
    intro();

}
public static void intro(){
    Scanner input = new Scanner(System.in); 
    String user= input.nextLine();
    while (!user.equals("quit") & input.hasNextLine()){ //processes code when user     input does not equal quit
    StringTokenizer chunks = new StringTokenizer(user, " "); //parses by white space
    String fraction1 = chunks.nextToken(); //first fraction
    String operand = chunks.nextToken(); //operator 
    String fraction2 = chunks.nextToken(); //second fraction
    System.out.println("Fraction 1: " + fraction1); 
    System.out.println("Operation: " + operand); 
    System.out.println("Fraction 2: " + fraction2); 
    System.out.println("Enter an expression (or \"quit\"): "); //prompts user for more input


while (user.contains("*")){
    parse(fraction1);
    parse(fraction2);
    System.out.println("hi");
    int num = num1 * num2;
    int denom = denom1 * denom2;
    System.out.println(num + "/" + denom);
    user = input.next();

        }
    }
}

public static void parse(String fraction) {
    if (fraction.contains("_")){
        StringTokenizer mixed = new StringTokenizer(fraction, "_");
        int wholeNumber = Integer.parseInt(mixed.nextToken());
        System.out.println(wholeNumber);
        String frac = mixed.nextToken();
        System.out.println(frac);
        StringTokenizer parseFraction = new StringTokenizer(frac, "/"); //parses by forward slash
        int num = Integer.parseInt(parseFraction.nextToken());  
        System.out.println(num);
        int denom = Integer.parseInt(parseFraction.nextToken());
        System.out.println(denom);




}
else if (!fraction.contains("_") && fraction.contains("/")){
    StringTokenizer parseFraction = new StringTokenizer(fraction, "/"); //parses by forward slash
    int num = Integer.parseInt(parseFraction.nextToken());  
    System.out.println(num);
    int denom = Integer.parseInt(parseFraction.nextToken());
    System.out.println(denom);



}else{ 
    StringTokenizer whiteSpace = new StringTokenizer(fraction, " "); 
    int num = Integer.parseInt(whiteSpace.nextToken());  
    System.out.println(num);
}
}}

1 个答案:

答案 0 :(得分:4)

  

有没有办法在另一种方法中在该方法之外的特定方法中使用变量?

是的,你可以这样做。您可以在方法中声明变量,在那里使用它并将其传递给另一个您可能想要使用它的方法。像这样的东西

void test1() {
    int var = 1;
    System.out.println(var); // using it
    test2(var); // calling other method and passing the value of var
}

void test2(int passedVarValue) {
    System.out.println(passedVarValue); // using the passed value of the variable
    // other stuffs
}