Why isn't this recursion working in Java? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to calculate the factorial of the integer user inputs, but it does not return anything. Why?
Thanks.
import java.util.Scanner;
`class App {
public static int factorial(int n) {
if (n == 0) {
return 1;
} else {
int recurse = factorial(n - 1);
int result = n * recurse;
return result;
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer to calculate its factorial: ");
int users = input.nextInt();
factorial(users);
}
}

The problem in your code is that you are have not giving a print statement for displaying factorial of the number entered. Just returning a value will not print it. If you are working in BlueJ environment, you can only use the code by directly executing the method factorial. Thank You.

The problem in your code is that , you are have not given a print statement for >displaying factorial of the number entered.

Related

How Can I Write A Statement To Print Out If The Number Is Odd Or Even in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to write A program To check and Print Out If The Number Is Odd Or Even.
This Is What I Have Programmed So Far:
import java.util.Scanner;
public class Irs_Lab
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
System.out.print("Enter an integer :: ");
int num = kb.nextInt();
}
}
I Heard You can Use A % (mod) to get the remainder of division.
Can anyone help me on this.
You can use the modular operator to see if a number is even or odd(as you said). When dividing a number by 2, if it is even it will have a remainder of 0, if it is odd it will have a remainder of 1. Using if(a % 2 == 0){//even}else{//odd} is your likely solution

How to Recurse to print [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Here is the question:
Write a recursive void method that has one parameter which is an integer and that writes to the screen the number of asterisks “*” given by the argument. The output should be all in one line. Assume that the argument is positive.
It is not a homework or assignment. Just a question in the slides with no answers...
I just don't know how to do it. I am a noob in this
here is my code and what i know
import java.util.Scanner;
public class Ez {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String str = input.nextLine();
String words[] = str.split("\\s");
int length = words.length;
int clength = str.length();
System.out.println(length);
System.out.println(clength);
}
public static void asterisksCounter (int n) {
}
}
Any big brains that know how to solve this question?
Thank you :)
While this is not a place to just ask a question and get an answer, since you mentioned that you have no idea how recursion works, giving you the code.
static void write(int n){
if(n == 0){
return;
}
System.out.print("*");
write(n-1);
}
General expectation is for the OP to try the problem by themselves, inform the community of the effort they put in and where exactly they are stuck. We would have been happier to help you after seeing some code from your side in the write() method.

Don't know why my while loop doesn't work (Java) ..? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
The purpose of my code is to enter a pin, and it'll check if it's right or not. If it isn't, the question will loop.
For some reason, my code doesn't loop properly, and a lot of the code is underlined. Specifically the while loop itself and the second JOptionPane
// package loop;
import javax.swing.JOptionPane;
public class loop {
public static void main(String[] args) {
int correctPin = 3333;
int count = 0;
String maybePin = JOptionPane.showInputDialog("Please enter the PIN");
int sMaybePin = Integer.parseInt(maybePin);
while(correctPin != sMaybePin);{
maybePin = JOptionPane.showInputDialog("Please enter the PIN");
count = count-1;
}
JOptionPane.showMessageDialog(null, count);
}
}
while(correctPin != sMaybePin); <--
Look at that ; that terminates the loop right there. You need to remove that.
You never update sMabyPin which is the variable you are checking against. If you do what #John and #ANS suggested you'll be stuck in an infinite loop.
Remove the ; after the while statement and correct set the value of the variable sMaybePin to the input vlaue and ot works
public static void main(String[] args) {
int correctPin = 3333;
int count = 0;
String maybePin = JOptionPane.showInputDialog("Please enter the PIN");
int sMaybePin = Integer.parseInt(maybePin);
while(correctPin != sMaybePin){
sMaybePin = Integer.parseInt(JOptionPane.showInputDialog("Please enter the PIN"));
count = count-1;
}
JOptionPane.showMessageDialog(null, count);
}

If statement not running: any ideas? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to compare the third number to the last number and the fourth number to the fifth number. The if statement isn't running at all any help would be appreciated.
import java.util.*;
class Problem2{
public static void main (String[] args){
Scanner s = new Scanner(System.in);
System.out.println("Enter a number between 100000 and 999996: ");
int m = s.nextInt();
if(m < 100000 || m > 999996){
System.out.println("Out of range!");
}
else{
String j = Integer.toString(m);
for(int i=2;i<6,i++){
System.out.println(j.charAt(i));
if(j.charAt(i) == (j.charAt(i)+3)&& (j.charAt(i)+1) == (j.charAt(i)+2)){
System.out.println("Works!");
}
}
}
}
}
You are incrementing the value, not the index of the character.
Change this:
if (j.charAt(i) == (j.charAt(i)+3)&& (j.charAt(i)+1) == (j.charAt(i)+2))
To this:
if (j.charAt(i) == j.charAt(i+3) && j.charAt(i+1) == j.charAt(i+2))
// increment indexes --^ --^ --^
Move the additions inside of the charAt() calls.
if(j.charAt(i) == (j.charAt(i+3))&& (j.charAt(i+1)) == (j.charAt(i+2)))

Do while with try catch JAVA validation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm having a trouble figuring out how to solve this issue when it just stops when I call this method when I input letters and other types. What is the problem with my code? Can anyone help? Thanks in advance!
Here is my code:
public int selectOption(int maxRange, String sourceType) throws IOException
{
do
{
try
{
userInput = input.nextInt();
}//end try
catch(Exception e)
{
validInput=false;
input.next();
}//end catch
if (userInput<1 || userInput>maxRange)
{
MovieHouse.clearScreen();
System.out.println("Select from the options only!");
loadHeader();
if(sourceType.equals("main"))
MovieHouse.homeMenu();
else if(sourceType.equals("movie menu"))
loadMovieMenu();
}//end if
}while(userInput<1 || userInput>maxRange || validInput==false);
return userInput;
}//end selectOption
"input letters" - Does it mean normally the program take digits and you are providing char as input.
Not clear what is your issue.

Categories