import java.util.Scanner; public class test { public static void main(String[] args) { Scanner userInput = new Scanner(System.in); //how many times java will go throw the for-loop фSystem.out.println(" enter loopTimes"); int repeatQuestions = userInput.nextInt(); //variables int count; int result = 0; //if repeatQuestions = 2;loop will run 2 times for(count = 0; count < repeatQuestions; count++) { //first run Part: display 0,second time Part: will display 1 System.out.println("Part: " + count); //prompt first number System.out.println("Enter first number"); int numberOne = userInput.nextInt(); //prompt second number System.out.println("Enter second number"); int numberTwo = userInput.nextInt(); //passing parameters to method and store result in variable result = adding2Numbers(numberOne, numberTwo); //*** enter your code here *** } //display last number that was stored in variable result System.out.println(result); /* * Run: * repeatQuestions = 2; * * At Part: 0 * numberOne = 2; * numberTwo = 3; * result = 5; * * At Part: 1 * numberOne = 5; * numberTwo = 6; * result = 11; * *??? How to compare which result is bigger in Part:0 or Part:1 ??? */ userInput.close(); } /** * @param method adding two number together * @return sum of two number that method is received */ public static int adding2Numbers(int numberOne, int numberTwo) { int result = numberOne + numberTwo; return result; } }
Closed due to the fact that off-topic participants Yuriy Spb ♦ , VenZell , Vladimir Martyanov , aleksandr barakin , user194374 10 Mar '16 at 6:06 .
It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:
- “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Vladimir Martyanov, aleksandr barakin, Community Spirit
- Maybe bring your solution to the problem? - Sergey Mitrofanov
|
1 answer
int result = 0;
Change to:
int result[] = new int[2];
And this:
result = adding2Numbers(numberOne, numberTwo);
Change to this:
result[count] = adding2Numbers(numberOne, numberTwo);
And then calmly compare result[0]
and result[1]
UPD: Well, if the code can be inserted ONLY in a loop and you can not touch the source code then:
The variable int oldResult = 0;
outside the loop, you still need to add. Then do this:
if(oldResult>result){ result=oldResult; }else{ oldResult=result; }
PS And for the formulation of the question you have to put the click.
- From the condition of the problem it is clear that you can only insert the code, but not outrage over what is already there. The question itself is contained in the source code. The decision does not cite because it is against the rules. - Sergey Mitrofanov
- @ Sergey Mitrofanov I don’t say that there is no task, I say that the question is very badly designed. - Riĥard Brugekĥaim
|