|
I wanted to print Factorial. So, I am sorry for the mistake in the question.
|
|
|
|
|
Your calculation is still wrong.
f(1) === 1 , but your code returns 2 .
And f(n) === n * f(n-1) , but your code is returning f(n-1) * f(n-2) .
Also, unless this is a homework question to test your ability to write recursive functions, it's much more efficient to use a loop to calculate factorials or the Fibonacci sequence.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Maybe you shouldn't give the factorial of 1 the value 2 as it is returning in your first "if" construct. Now I am not expert but I think this should work:
public class facto{
public static void main(String[] args){
System.out.println(dust(5));
}
static int dust(int n)
{
int output;
if(n==1){
return 1;
}
output = dust(n-1)* n;
return output;
}
}
I suppose that in your version the recursion is not able to complete properly because of "maybe" your trying to both return the value and calculate it at the same time. Hope the code snippet helps, I am just a beginner myself so apologies if I didn't get it right. P.S I know I haven't considered the condition of the input being 0.
modified 8-Feb-18 1:12am.
|
|
|
|
|
I have a code for Java in which I am creating 2 Array List from 2 dimensional array.. But I am getting an Array Out Of Bound exception.. I am passing 2 dimensional array to my class method answer.. But it is not working..
import java.io.*;
import java.util.*;
public class Abc {
static void answer(Integer[][] a,int l){
Integer[] un = new Integer[l];
Integer[] un1 = new Integer[l];
final Integer z = 0;
int max1=0,max2=0,count1=0,count2=0;
int ind=0;
ArrayList<integer> arr1 = new ArrayList<integer>(Arrays.asList(a[0][l]));
ArrayList<integer> arr2 = new ArrayList<integer>(Arrays.asList(a[1][l]));
for(int i=0;i<l;i++){
if(i="" %="" 2="=" 0){
="" max1="Collections.max(arr1);
" count1="" +="max1;
" ind="arr1.indexOf(max1);
" arr1.set(ind,z);
="" arr2.set(ind,z);
="" }
="" else{
="" max2="Collections.max(arr2);"
="" count2="" arr2.set(ind,0);
="" arr1.set(ind,0);
="" }
="" if(count1="" <="" count2){
="" system.out.println("second");
=""> count2){
System.out.println("First");
}
else{
System.out.println("Tie");
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int times = sc.nextInt();
for(int i=0;i
|
|
|
|
|
Please do not post the same question to several forums, stick to one single choice
Multi-posting the same question will not help you to get a quicker or better answer.
"I'm neither for nor against, on the contrary." John Middle
|
|
|
|
|
I have this HashMap: <integer,map<integer,string>>.
I want to delete all the Map of specific Integer.
For example, if my Map is:
<1,<11,'aa'><12,'bb'><13,'cc'>>
<2,<21,'dd'><22,'ee'>>
<3,<31,'ff'><32,'gg'><33,'hh'><34,'ii'>>
If the Input Is 2, I want it to be:
<1,<11,'aa'><12,'bb'><13,'cc'>>
<2,<0,null>>
<3,<31,'ff'><32,'gg'><33,'hh'><34,'ii'>>
How do I write it???
Help Me Please!
Thank You!
|
|
|
|
|
|
Thank You!
It's working...
|
|
|
|
|
I have Eclipse on my desktop at home, and on my netbook.
I have a workplace on a thumb drive.
I'm quite new to Eclipse, so this may be trivial to old hands, but it is currently a blockade to me.
The design and source code windows have disappeared. Previously, I could switch between them.
On my screen, I see MyProject.java in the one and only window pane in Eclipse. The font is too small for me to read. Previously, I was able to choose the font and size of the contents. When I try to do that now, no effect occurs on the source code.
The "Design" window pane has totally disappeared. What did I do to make that happen ? How do I get it back ?
I spent ten minutes trying to search and guess the menu system and got nowhere.
What do I click in order to ?...
- Return the "Design" window pane to my environment so that I can click on it and do the graphical stuff.
- Return the source code window pane to my sight which responds to Window -> Preferences -> General -> Appearance -> Colors And Fonts -> (So that I can do the actual code stuff)
Thank you for helping a clueless guy.
|
|
|
|
|
It is a while since I used eclipse, but as I recall there were menu items to show the various windows. There are also a couple of icons at the top right (I think) which allow you to show different layouts for different situations: one for development, and another for run/debug. Take a look at [^] which may give some clues. They do use some odd terminology but it does make more sense as you get used to it.
|
|
|
|
|
sir can u give me coding for fingerprint ,how to store,compare by using morphow device in java programming
|
|
|
|
|
|
Hi, I have a problem when running my project in NetBeans 8.2 and SQL server express 2014.
After doing the programming it shows the result as below
" com.microsoft.sqlserver.jdbc.sqlservExeption Login failed for user ''. ClientConnectionid: d963d05c-a68b-49c0-a7e9-88b829c96174 "
Can anybody help me with this issue, please?
Hehreby the quoding that i key in
"
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url= "jdbc:sqlserver://localhost:1433;"
+ "databaseName = testdb"
+ "user = sa; password = 12345";
Connection con = DriverManager.getConnection(url);
String sql = "Select* from test where username = ? and password =?";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString (1, username.getText());
pst.setString (2, passwrod.getText());
ResultSet rs = pst.executeQuery();
if (rs.next())
{
JOptionPane.showMessageDialog(null, "ok, it match");
}
else
{
JOptionPane.showMessageDialog(null, "not ok, try again!!");
username.setText("");
passwrod.setText("");
}
con.close();
}
catch (Exception e)
{
JOptionPane.showMessageDialog (null,e);
}
|
|
|
|
|
Login failed for user: usually means that some credential is not valid. Check that your userid and password are both correct. Also check any server logs to see if there is more informarion on exactly what went wrong.
|
|
|
|
|
Member 13601614 wrote: + "user = sa; password = 12345";
I cannot find any examples that would suggest that the spaces before and after the equals sign should be there.
|
|
|
|
|
How can i load a file into the JTextArea
|
|
|
|
|
Probably by reading the content, if necessary converting it into text, and copying it to the JTextxxx window. See The Java™ Tutorials[^] for suggestions and samples.
|
|
|
|
|
|
what is the advantage of declaring each variable in separate line?
Zaki
|
|
|
|
|
|
What is the advantage of posting the same message over and over again?!
Java1 - Java Discussion Boards[^] - 6th October - "what is the advantage of declaring each variable in separate line?"
Java1 - Java Discussion Boards[^] - 22nd December - "what is the advantage of declaring each variable in separate line?"
Java - Java Discussion Boards[^] - 22nd December - "what is the advantage of declaring each variable in separate line?"
You were given the answer back in October. It hasn't changed in the last two months.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
It's a way of trying to get the answer you want. 
|
|
|
|
|
It is one of those good practices which you're asked to follow while writing your code because it makes a lot cleaner and concise code. This is similar as to why we programmers focus so much on indentation... Because whenever you're working on a big project chances are you may not be the only person working on it, so if someone else has to review your code it'll be a lot easier for him/her to get a reference as to where was each variable declared. Because you might be well aware of the haunting NullPointerException which just pops out of no where and can be really trick to negotiate if your code isn't clear.
Hope this helps. Cheers.
modified 8-Feb-18 1:09am.
|
|
|
|
|
what is the advantage of declaring each variable in separate line?
Zaki
|
|
|
|
|