代码无法从方法返回整数值(Java / Eclipse)(Code Fails to Return Integer Value From Method (Java/Eclipse))

编程入门 行业动态 更新时间:2024-10-24 10:17:35
代码无法从方法返回整数值(Java / Eclipse)(Code Fails to Return Integer Value From Method (Java/Eclipse))

在过去的两天里,我一直在搜索这个网站和其他网站,因此我意识到与此类似的大量预先存在的问题。 但是,在三四周前才开始编程,我无法理解它们中的任何一个及其线程。 考虑到这一点,我有一个问题,对更有经验的人可能拥有一个痛苦的基本和直接的解决方案。


使用Eclipse,我必须创建一个执行以下操作的程序:

读取两个数字(整数) 包含一个方法,它将两个整数作为参数,并返回两个中较大的一个。 包含从main()调用的另一个方法,将(2)中方法返回的较大数字作为参数,并返回最后一位数字。 打印(2)和(3)中方法返回的两个值。

我创建的代码(如下所示)成功读入用户输入,但无法打印出较大的整数和较大的整数的最后一位数。 它反而打印出“0”。 这是打印到控制台的内容:


Enter First Integer: (Entered Integer) Enter Second Integer: (Entered Integer) Larger Integer: 0 Last Digit: 0

我相信代码应该正常运行,除了在方法中确定的值没有返回到main的事实。 Eclipse没有显示错误消息,这导致我相信问题出在我的返回语句中。 欢迎和期望任何建议或解决方案。 谢谢。


import java.util.Scanner; public class thursdayWork{ //Determines larger integer and returns it. public static int processAndReturn(int int1, int int2, int answer){ answer = Math.max(int1, int2); return answer; } //Determines last digit of integer and returns it. public static int processLargerInt(int answer, int lastDigit){ if((answer >= 0) && (answer < 10))lastDigit = (answer % 10); else if((answer >= 10) && (answer < 100))lastDigit = (answer % 100); else if((answer >= 100) && (answer < 1000))lastDigit = (answer % 1000); else if((answer >= 1000) && (answer < 10000))lastDigit = (answer % 10000); else System.out.print("Pick smaller numbers."); return lastDigit; } //Calls methods and prints returned values. public static void main(String[] args){ Scanner console = new Scanner(System.in); int int1; int int2; int answer = 0; int lastDigit = 0; System.out.print("Enter First Integer: "); int1 = console.nextInt(); System.out.print("Enter Second Integer: "); int2 = console.nextInt(); processAndReturn(int1, int2, answer); System.out.println("Larger Integer: " + answer); processLargerInt(answer, lastDigit); System.out.print("Last Digit: " + lastDigit); } }

I have been scouring this site and others for the last two days, and thus am aware of the overwhelming amount of preexisting questions similar to this one. However, having only started programming three or four weeks ago, I am unable to make sense of any of them and their threads. Considering this, I have an issue that to the more experienced likely possesses a painfully basic and straightforward solution.


Using Eclipse, I must create a program which does the following:

Reads in two numbers (integers) Contains a method that takes the two integers as parameters and returns the larger of the two. Contains another method that is called from main() takes the larger number returned by the method in (2) as a parameter and returns the last digit. Prints the two values returned by the methods in (2) and (3).

The code I have created (displayed below) successfully reads in the user input, but fails to print out both the larger integer and the larger integer's last digit. It instead prints out "0". This is what is printed out to the console:


Enter First Integer: (Entered Integer) Enter Second Integer: (Entered Integer) Larger Integer: 0 Last Digit: 0

I believe that the code should function correctly, save for the fact that values determined inside the methods are not being returned to the main. No error messages are being displayed by Eclipse, which leads me to believe that the issue lies within my return statements. Any suggestions or solutions are welcomed and desired. Thank you.


import java.util.Scanner; public class thursdayWork{ //Determines larger integer and returns it. public static int processAndReturn(int int1, int int2, int answer){ answer = Math.max(int1, int2); return answer; } //Determines last digit of integer and returns it. public static int processLargerInt(int answer, int lastDigit){ if((answer >= 0) && (answer < 10))lastDigit = (answer % 10); else if((answer >= 10) && (answer < 100))lastDigit = (answer % 100); else if((answer >= 100) && (answer < 1000))lastDigit = (answer % 1000); else if((answer >= 1000) && (answer < 10000))lastDigit = (answer % 10000); else System.out.print("Pick smaller numbers."); return lastDigit; } //Calls methods and prints returned values. public static void main(String[] args){ Scanner console = new Scanner(System.in); int int1; int int2; int answer = 0; int lastDigit = 0; System.out.print("Enter First Integer: "); int1 = console.nextInt(); System.out.print("Enter Second Integer: "); int2 = console.nextInt(); processAndReturn(int1, int2, answer); System.out.println("Larger Integer: " + answer); processLargerInt(answer, lastDigit); System.out.print("Last Digit: " + lastDigit); } }

最满意答案

您应该从方法返回,而不是将答案作为参数传递给两个方法。 您应该详细了解按值传递与传递参考。

public static void main(String[] args) { // Your scanner code here. int answer = processAndReturn(int1, int2); System.out.println("Larger Integer: " + answer); int lastDigit = processLargerInt(answer); System.out.print("Last Digit: " + lastDigit); } public static int processAndReturn(int int1, int int2){ return Math.max(int1, int2); } public static int processLargerInt(int answer) { return answer % 10; }

Instead of passing the answer as a param to both the methods, you should have it returned from the method. You should read more about pass by value vs pass by reference.

public static void main(String[] args) { // Your scanner code here. int answer = processAndReturn(int1, int2); System.out.println("Larger Integer: " + answer); int lastDigit = processLargerInt(answer); System.out.print("Last Digit: " + lastDigit); } public static int processAndReturn(int int1, int int2){ return Math.max(int1, int2); } public static int processLargerInt(int answer) { return answer % 10; }

更多推荐

本文发布于:2023-07-31 18:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1347349.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   代码   方法   Java   Eclipse

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!