从另一个方法传递变量值
我已经解决这个问题几个小时了,并且取得了重大进展(很大程度上感谢搜索该网站并应用在类似问题中找到的提示),但我现在似乎陷入了僵局。请看一下我所做的事情,要么指出我出错的地方并提供要纠正的伪代码,要么向我指出一个可以帮助我填补我的理解空白的资源。我真的觉得好像我只是错过了一个小细节,这将使这个主题对我来说有意义。
该应用程序的目的是根据用户输入的 2 个分子和 2 个分母进行分数加、减、乘和除(是的,这是一个课程作业,所以请不要提供源代码,而是提供有关我在哪里的指针)概念上出了问题)。我将其分解为多个步骤,第一步是获取用户的输入并将其输出以进行确认。
方法文件:
package Fractions;
import java.util.Scanner;
public class FractionValues
{
// Declare integer class variables for package Fractions
int fracNum1;
int fracDenom1;
int fracNum2;
int fracDenom2;
// Obtain four integers from user input and output to console as two fractions
public static void getFractions(int fracNum1, int fracDenom1, int fracNum2, int fracDenom2)
{
Scanner inInt = new Scanner(System.in);
System.out.println("Enter an integer for the numerator of the first " +
"fraction: ");
fracNum1 = inInt.nextInt();
System.out.println("Enter an integer for the denominator of the first " +
"fraction: ");
fracDenom1 = inInt.nextInt();
System.out.println("Enter an integer for the numerator of the second " +
"fraction: ");
fracNum2 = inInt.nextInt();
System.out.println("Enter an integer for the denominator fo the second " +
"fraction: ");
fracDenom2 = inInt.nextInt();
System.out.println("===================================================" +
"=================");
}
// Return values of variables from input for use in other classes
public int getFracNum1() {return fracNum1;}
public int getFracDenom1() {return fracDenom1;}
public int getFracNum2() {return fracNum2;}
public int getFracDenom2() {return fracDenom2;}
}
main 方法文件:
package Fractions;
public class TestFractions2
{
public static void main(String[] args)
{
// Call getFractions method to assign variables from user input
FractionValues newFracNum1 = new FractionValues();
newFracNum1.getFracNum1();
FractionValues newFracDenom1 = new FractionValues();
newFracDenom1.getFracDenom1();
FractionValues newFracNum2 = new FractionValues();
newFracNum2.getFracNum2();
FractionValues newFracDenom2 = new FractionValues();
newFracDenom2.getFracDenom2();
System.out.println("You entered " + newFracNum1.getFracNum1() + "/" + newFracDenom1.getFracDenom2() + " and " +
newFracNum2.getFracNum2() + "/" + newFracDenom2.getFracDenom2() + " as your fractions.");
}
}
至少经过一番努力,两个文件现在都可以编译了。但是,该应用程序不起作用。这是我得到的输出:
您输入了 0/0 和 0/0 作为分数。
一旦这部分起作用,我将添加一个 if 语句,提示用户响应是否要继续他们的选择,或返回到输入提示。
根据下面的宝贵反馈和作业的限制,我得到了以下内容:
package Fractions;
import java.util.Scanner;
public class FractionValues
{
int fracNum1;
int fracDenom1;
int fracNum2;
int fracDenom2;
Scanner inInt = new Scanner(System.in);
// Obtain four integers from user input
public int getFracNum1()
{
System.out.println("Enter an integer for the first numerator: ");
return fracNum1 = inInt.nextInt();
}
public int getFracDenom1()
{
System.out.println("Enter an integer for the first denominator: ");
return fracDenom1 = inInt.nextInt();
}
public int getFracNum2()
{
System.out.println("Enter an integer for the second numerator: ");
return fracNum2 = inInt.nextInt();
}
public int getFracDenom2()
{
System.out.println("Enter an integer for the second denominator: ");
return fracDenom2 = inInt.nextInt();
}
}
以及主要应用方法:
package Fractions;
public class TestFractions2
{
public static void main(String[] args)
{
// Call FractionValues methods to assign variables from user input
FractionValues newFracNum1 = new FractionValues();
FractionValues newFracDenom1 = new FractionValues();
FractionValues newFracNum2 = new FractionValues();
FractionValues newFracDenom2 = new FractionValues();
System.out.println("You entered " + newFracNum1.getFracNum1() + "/" +
newFracDenom1.getFracDenom2() + " and " + newFracNum2.getFracNum2() +
"/" + newFracDenom2.getFracDenom2() + " as your fractions.");
}
}
两个文件都正确编译,我得到以下预期输出:
为第一个分子输入一个整数:
2 输入第二个分母的整数:
5 输入第二个分子的整数:
6 输入第二个分母的整数:
3 您输入 2/5 和 6/3 作为分数。
非常感谢您在方法和构造函数方面的帮助,以及对命名约定的建设性评论。你的评论引导我的地方很可能使我从考试不及格变成了优秀!即使在一位非常耐心朋友的帮助下,我已经为这个概念苦苦挣扎了好几个星期。
I've been working through this problem for several hours, and I've made significant progress (thanks in large part to searching this site and applying tips found in similar questions) but I now seem to be at an impasse. Please take a look through what I have done and either point out where I've gone wrong and provide psuedo code to correct, or point me to a resource that can help me by filling in the gap of my understanding. I really feel as though I'm just missing a tiny detail that will make this topic make sense to me.
The purpose of the application is to add, subtract, multiply, and divide fractions based on user input of 2 numerators and 2 denominators (yes, this is for a course assignment so please don't give source code but rather pointers on where I've gone wrong conceptually). I've broken it down into steps, the first of which is getting the user's input and outputting it back for confirmation.
Method file:
package Fractions;
import java.util.Scanner;
public class FractionValues
{
// Declare integer class variables for package Fractions
int fracNum1;
int fracDenom1;
int fracNum2;
int fracDenom2;
// Obtain four integers from user input and output to console as two fractions
public static void getFractions(int fracNum1, int fracDenom1, int fracNum2, int fracDenom2)
{
Scanner inInt = new Scanner(System.in);
System.out.println("Enter an integer for the numerator of the first " +
"fraction: ");
fracNum1 = inInt.nextInt();
System.out.println("Enter an integer for the denominator of the first " +
"fraction: ");
fracDenom1 = inInt.nextInt();
System.out.println("Enter an integer for the numerator of the second " +
"fraction: ");
fracNum2 = inInt.nextInt();
System.out.println("Enter an integer for the denominator fo the second " +
"fraction: ");
fracDenom2 = inInt.nextInt();
System.out.println("===================================================" +
"=================");
}
// Return values of variables from input for use in other classes
public int getFracNum1() {return fracNum1;}
public int getFracDenom1() {return fracDenom1;}
public int getFracNum2() {return fracNum2;}
public int getFracDenom2() {return fracDenom2;}
}
main Method file:
package Fractions;
public class TestFractions2
{
public static void main(String[] args)
{
// Call getFractions method to assign variables from user input
FractionValues newFracNum1 = new FractionValues();
newFracNum1.getFracNum1();
FractionValues newFracDenom1 = new FractionValues();
newFracDenom1.getFracDenom1();
FractionValues newFracNum2 = new FractionValues();
newFracNum2.getFracNum2();
FractionValues newFracDenom2 = new FractionValues();
newFracDenom2.getFracDenom2();
System.out.println("You entered " + newFracNum1.getFracNum1() + "/" + newFracDenom1.getFracDenom2() + " and " +
newFracNum2.getFracNum2() + "/" + newFracDenom2.getFracDenom2() + " as your fractions.");
}
}
At least after some struggles, both files now compile. However, the application doesn't work. This is the output I get:
You entered 0/0 and 0/0 as your fractions.
Once this part works, I'll be adding an if statement prompting the user to respond whether they wish to continue with their choices, or return to the entry prompts.
Based on the valuable feedback below and the constraints of the assignment, I've gotten the following:
package Fractions;
import java.util.Scanner;
public class FractionValues
{
int fracNum1;
int fracDenom1;
int fracNum2;
int fracDenom2;
Scanner inInt = new Scanner(System.in);
// Obtain four integers from user input
public int getFracNum1()
{
System.out.println("Enter an integer for the first numerator: ");
return fracNum1 = inInt.nextInt();
}
public int getFracDenom1()
{
System.out.println("Enter an integer for the first denominator: ");
return fracDenom1 = inInt.nextInt();
}
public int getFracNum2()
{
System.out.println("Enter an integer for the second numerator: ");
return fracNum2 = inInt.nextInt();
}
public int getFracDenom2()
{
System.out.println("Enter an integer for the second denominator: ");
return fracDenom2 = inInt.nextInt();
}
}
and the main application method:
package Fractions;
public class TestFractions2
{
public static void main(String[] args)
{
// Call FractionValues methods to assign variables from user input
FractionValues newFracNum1 = new FractionValues();
FractionValues newFracDenom1 = new FractionValues();
FractionValues newFracNum2 = new FractionValues();
FractionValues newFracDenom2 = new FractionValues();
System.out.println("You entered " + newFracNum1.getFracNum1() + "/" +
newFracDenom1.getFracDenom2() + " and " + newFracNum2.getFracNum2() +
"/" + newFracDenom2.getFracDenom2() + " as your fractions.");
}
}
Both files compile properly, and I get the following expected output:
Enter an integer for the first numerator:
2
Enter an integer for the second denominator:
5
Enter an integer for the second numerator:
6
Enter an integer for the second denominator:
3
You entered 2/5 and 6/3 as your fractions.
Thank you very much for your help with methods and constructors, and constructive comments on naming conventions. The places your comments led me have very likely taken me from failing my exam to acing it! I've been struggling with this concept for weeks, even with the help of a very patient friend.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
代码存在一些问题。首先,您看到的输出是在
Object
类中找到的toString()
默认实现的结果(其中所有类,包括GetFractions 最终导出)。重写此方法以返回实例的字符串表示形式:
或者不将实例传递给 System.out.println(),而是传递成员访问方法调用的结果(此类方法称为 getter):
请注意,
double
和其他基本类型将自动转换为字符串。其次,静态
GetFractions()
方法修改其参数,这是无效的(没有人会看到更改,因为它们是按值传递的)。该方法应该修改现有实例的同名实例变量,并且它不应该是静态的,或者应该是基于用户提供的数据创建新实例的工厂方法,在这种情况下,它将把值传递给构造函数或者它本身应该是一个构造函数。无论哪种方式,您都不想修改方法的参数。以下是三种解决方案的概述:从输入流读取数据的非静态方法:
静态工厂方法:
构造函数:
另请注意,将
InputStream
传递到所有这些方法中比硬编码提供了更大的灵活性System.in
。第三,您应该重新考虑您的命名约定。虽然可以像调用类一样调用静态方法,但通常是一种不好的做法。另外,建议使用名词表达式调用类和对象,使用动词表达式调用方法。这有助于设计您的类并使代码更具可读性。
GetFractions
更适合作为方法的名称而不是类的名称。Fractions
会产生更好的类名。There are a few problems with the code. First, the output you see is the product of the default implementation of
toString()
that is found inObject
class (from which all classes includingGetFractions
ultimately derive). Override this method to return string representation of your instances:or instead of passing the instance to
System.out.println()
pass a result of a member access method call (such methods are known as getters):Note that
double
and other primitive types will automagically be converted to strings.Second, your static
GetFractions()
method modifies its arguments which is ineffective (nobody will see the change since they're passed by value). The method should either modify same-named instance variables of an existing instance and then it should not be static or it should be a factory method creating new instances based on the data provided by the user in which case it would pass the values to a constructor or it should be a constructor itself. Either way, you don't want to modify the method's parameters. Here is the outline of the three solutions:Non-static method which reads data in from an input stream:
Static factory method:
Constructor:
Notice also that passing an
InputStream
into all these methods affords greater flexibility than hardcodingSystem.in
.Third, you should reconsider your naming convention. Calling a static method the same as the class, while possible, is generally a bad practice. Also, it's advisable to call classes and objects with noun expressions and methods with verb expressions. This helps to design your classes and makes the code more readable.
GetFractions
is more suitable as a name of a method rather than a class.Fractions
would make for a better class name.您的代码隐式调用 GetFractions 类的 toString() 方法。因为您还没有覆盖它,所以它是对象(超类)的 toString() 方法。对象的 toString() 方法返回类似类名加上哈希码的内容(请参阅 http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#toString%28%29 )。这就是您在输出中看到的内容。
你有两种可能性:覆盖 toString 或像 Eng.Fouad 提到的那样更改你的代码。
your code implicit calls the toString() method of you class GetFractions. Because you haven't overwritten it, its the toString() method from object (the super class). The toString() method from objects returns soemthing like the class name plus a hashcode (see http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#toString%28%29). that's what you see in your output.
you have two possibilities: overwrite toString or change your code like Eng.Fouad mentioned.
更正代码后它打印 0-s 的原因是您实际上从未调用
GetFractions
静态方法,它不是构造函数。永远不要像构造函数那样命名方法,也许只有当它是静态工厂方法时。
另外,在原始代码中,您打印了
GetFractions
对象,从而导致了您尚未覆盖的toString
调用。另请注意,对 get... 的调用没有任何效果,因为返回的值不存储在任何地方。
The reason why it prints 0-s after you corrected the code is that you actually never call the
GetFractions
static method, which is NOT a constructor.Never ever name a method like a constructor, maybe only if it's a static factory method.
Also with the orginal code, you printed the
GetFractions
objects causing the call oftoString
you haven't overwritten.Also notice, that the calls to get... are without any effect since the returned values are not stored anywhere.