java - 创建一个类,开始使用OOP。 java程序不显示任何内容?

标签 java class oop object constructor

主要问题是当我运行程序时没有任何反应,但没有任何错误。我的程序应该创建一个新类(Fraction),其中包含一些方法来操作分数并使用一些不同的值对其进行测试。这是一个两个文件程序,我在构造函数方法方面遇到了一些麻烦。

我创建了一个应该初始化对象的分子和分母的方法,但我不知道要在定义中放入什么。对象的编号是在测试文件中选择的。

这是那篇文章: (位于我定义类及其所有方法的文件中)

public Fraction(int n, int d)
{
    //not sure what values to enter below
    numerator = ;
    denominator = ;
}

我也不确定我是否正确执行了循环,因为当我运行程序时没有任何反应,它只是说“构建成功”。我尝试输入确认对话框,但我还需要初始化程序吗?

下面是完整的代码。 程序中的第一个文件:

public class Fraction {

private int numerator;
private int denominator;
//returns the fraction
@Override
public String toString()
{
    return numerator + "/" + denominator;
}
//turns fraction into decimal
public double getDecimal()
{
    double decimal = numerator / denominator;
    return decimal;
}
//reduces fraction. doesn't return value
public void reduce()
{
    int gcf = 1, smaller;
    if (numerator < denominator)
    {
        smaller = numerator;
    } else
    {
        smaller = denominator;
    }
    for (int i = 1; i <= smaller; i++)
    {
        if (numerator % i == 0 && denominator % i == 0)
        {
            gcf = i;
        }
    }
    numerator = numerator / gcf;
    denominator = denominator / gcf;
    System.out.print(numerator + "/" + denominator);
}
//turns fraction into a mixed number and returns the new value
public String toMixed()
{
    int whole = numerator / denominator;      
    numerator %= denominator;
    String changedFraction = (whole + " " + numerator + "/" + denominator);
    return changedFraction;
}

public Fraction(int n, int d)
{
    //subbed in zeros below so it doesn't give me an error
    numerator = 0;
    denominator = 0;
}

//initializes the fraction to 1. Not sure how to call this method in the test file. 

   public Fraction()
   {
       numerator = 1;
       denominator = 1;
   }
}

第二个(测试)文件:

import javax.swing.*;

public class TestProgram
{

public static void main(String[] args)
{  
    //dialog box that runs program until user chooses to exit
    int option = JOptionPane.YES_OPTION;
    while (option == JOptionPane.YES_OPTION)
    {
 //menu that lets user choose which method they want to test the values with
    String input = JOptionPane.showInputDialog("Choose"
            + "\n" + "1. Test the toString() method"
            + "\n" + "2. Test the getDecimal() method"
            + "\n" + "3. Test the reduce() method"
            + "\n" + "4. Test the toMixed() method"
            + "\n" + "Click cancel to quit");
    int choice = Integer.parseInt(input);

    //array that holds 5 test values
    Fraction[] fractionArray = new Fraction[4];

    fractionArray[0] = new Fraction(9, 12);
    fractionArray[1] = new Fraction(31, 2);
    fractionArray[2] = new Fraction(12, 9);
    fractionArray[3] = new Fraction(12, 120);
    fractionArray[4] = new Fraction(16, 53);

    //calls the method chosen
    if (choice == 1)
    {
        for(int i = 0; i <fractionArray.length; i++)
        {
            fractionArray[i].toString();
        }
    }
    else if (choice == 2)
    {
        for(int i = 0; i <fractionArray.length; i++)
        {
            fractionArray[i].getDecimal();
        }
    }
    else if (choice == 3)
    {
        for(int i = 0; i <fractionArray.length; i++)
        {
            fractionArray[i].reduce();
        }
    }
    else if (choice == 4)
    {
        for(int i = 0; i <fractionArray.length; i++)
        {
            fractionArray[i].toMixed();
        }
    }
    //initializes the constructor method. not sure what values to put below.
       Fraction currentFraction = new Fraction(2,3);

    JOptionPane.showConfirmDialog(null, "Continue?");
    }
}
}

最佳答案

你肯定想要

public Fraction(int n, int d)
{
    //subbed in zeros below so it doesn't give me an error
    numerator = n;
    denominator = d;
}

关于java - 创建一个类,开始使用OOP。 java程序不显示任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34280179/

相关文章:

java - 使用 spring 入站文件适配器进行并发处理

java - 在 Akka Scheduler 中的特定日期安排作业

java - Joda Time : how to normalize ReadablePartial to expected fields?

objective-c - 类与协议(protocol)之间有什么区别

c++ - 定义 operator<< 内部类

java - DDD - 复合聚合序列化 - 设计问题

java - 通过 Opensmpp 发送长短信

javascript - 有没有更好的方法将多个 css classNames 分配给 React 组件?

php - Laravel 别名找不到类

php - 使用 PHP 维护不同页面的对象状态