How to Recurse to print [closed] - java

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.

Related

my code runs perfectly, but as soon as I submit it, I get a compilation error. Can anyone help me? Included the code below [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 1 year ago.
Improve this question
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) throws IOException {
Scanner in=new Scanner(System.in);
int t=in.nextInt();
int sum=0;
for(int i=0; i<t; i++)
{
int n=in.nextInt();
for(int j=1; j<n; j++)
{
if(n%j==0)
sum=sum+j;
}
if(n==sum)
System.out.println("true")
else
System.out.println("false");
}
}
}
You are missing a semi-colon on line System.out.println("true").
I would recommend you to use any IDE to avoid these kind of errors.
I guess this is just syntax error at end of instruction using semicolon
You need to close the
if(n==sum)
System.out.println("true"); // semicolon

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

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

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.

How to check how many times one number appears in the second number? [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 5 years ago.
Improve this question
I'm new in Java and I have a task that's going like this:
Generate one number between 0-9 and a second number between 10-99.
You need to check how many times the first number appears in the second number.
(For example: The first number is 5 and the second one is 55 so the first number (5) apears 2 times in the second number (55) ).
Btw, you need to make it without arrays.
package randomNumber_01;
import java.util.Random;
public class third {
public static void main (String[] args){
Random n = new Random();
int first = n.nextInt(9)+1;
System.out.println("First numnber: "+first);
int second = n.nextInt(99)+10;
System.out.println("Second number: "+second);
System.out.println("I stuck here");
}
}
int count=0;
if((second%10)==first){
count++;
}
if(Math.round((second/10))==first){
count++;
}
System.out.println(first+" occurs "+count+" times in "+second);
Maybe this can help you ;), replace your last println by this 8 lines ;)
but as shmosel sait in comment, be carreful it's
int first = n.nextInt(10);
int second = n.nextInt(90)+10;

Java Program find string length from user input [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 9 years ago.
Improve this question
public class Strings {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String word1 = sc.next();
System.out.println(word1.length());
sc.close();
}
}
So i want to make a very simple program that takes in a user string and returns the length, but the program does not even run. I am using eclipse. Any tips?
Your program works fine.
Are you forgetting to type something into the console? It might appear to not be running but it actually is.
ALSO: If you are new to java/programming I would suggest using Netbeans over Eclipse. Netbeans offers a bit more support, although it is less flexible (something you shouldn't need to worry about right now).
Make sure that your are providing an input to calculate the length of word.
This is working fine if you providing an input, it is better to change your code as follows:
public class Strings {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your word: \n");
String word1 = sc.next();
System.out.println(word1.length());
sc.close();
}
}

Categories