JAVA ENCODING HELP!!

09/28/2011 09:43 zepolzirk#1
Sir can you teach me about this?These program is not a fluent one. and i know that there is a more flexible program to this ..it's a factorial thing

this should be the output:

<<INVALID INPUT SHOULD START FROM 1 to 30>>>

Enter a Number:5
5!=5 x 4 x 3 x 2 x 1 = 120

Press "YES" to repeat: YES
Enter a Number: 10
10!=10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1=3628800

Press "YES" to repeat: YES
Enter a Number: 90
<<INVALID INPUT>>
Enter a Number: 3
3!=3 x 2 x 1=6
Press "YES" to repeat: No

Process Completed...

This is what i've done and maybe it's called a cheating XD i'm using JCREATOR LE 5.00 this is my compiler j2sdk1.4.2_13


Hope you can help me with this ! I will study all your teachings to me. thank you



PHP Code:
import java.io.*;
public class 
Factorial{
public static 
void main(String [] kimthrows IOException{
DataInputStream mik=new DataInputStream(System.in);

int num;
String ans="";

System.out.println("<<Valid input number must be in the range 1 to 30>>>");

do{
System.out.print("Enter a number: ");
num=Integer.parseInt(mik.readLine());

switch (
num){

case 
1:
num=num;
System.out.println("1!= 1 = "+num+"\n");
break;
case 
2:
num=num*1;
System.out.println("2!= 2 x 1 = "+num+"\n");
break;
case 
3:
num=num*2*1;
System.out.println("3!= 3 x 2 x 1 = "+num+"\n");
break;
case 
4:
num=num*3*2*1;
System.out.println("4!= 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
5:
num=num*4*3*2*1;
System.out.println("5!= 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
6:
num=num*5*4*3*2*1;
System.out.println("6!= 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
7:
num=num*6*5*4*3*2*1;
System.out.println("7!= 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
8:
num=num*7*6*5*4*3*2*1;
System.out.println("8!= 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
9:
num=num*8*7*6*5*4*3*2*1;
System.out.println("9!= 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
10:
num=num*9*8*7*6*5*4*3*2*1;
System.out.println("10!= 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
11:
num=num*10*9*8*7*6*5*4*3*2*1;
System.out.println("11!= 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
12:
num=num*11*10*9*8*7*6*5*4*3*2*1;
System.out.println("12!= 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
13:
num=num*12*11*10*9*8*7*6*5*4*3*2*1;
System.out.println("13!= 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
14:
num=num*13*12*11*10*9*8*7*6*5*4*3*2*1;
System.out.println("14!= 14 x 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
15:
num=num*14*13*12*11*10*9*8*7*6*5*4*3*2*1;
System.out.println("15!= 15 x 14 x 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
16:
num=num*15*14*13*12*11*10*9*8*7*6*5*4*3*2*1;
System.out.println("16!= 16 x 15 x 14 x 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
17:
num=num*16*15*14*13*12*11*10*9*8*7*6*5*4*3*2*1;
System.out.println("17!= 17 x 16 x 15 x 14 x 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
18:
num=num*17*16*15*14*13*12*11*10*9*8*7*6*5*4*3*2*1;
System.out.println("18!= 18 x 17 x 16 x 15 x 14 x 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
19:
num=num*18*17*16*15*14*13*12*11*10*9*8*7*6*5*4*3*2*1;
System.out.println("19!= 19 x 18 x 17 x 16 x 15 x 14 x 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
20:
num=num*19*18*17*16*15*14*13*12*11*10*9*8*7*6*5*4*3*2*1;
System.out.println("20!= 20 x 19 x 18 x 17 x 16 x 15 x 14 x 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
21:
num=num*20*19*18*17*16*15*14*13*12*11*10*9*8*7*6*5*4*3*2*1;
System.out.println("21!= 21 x 20 x 19 x 18 x 17 x 16 x 15 x 14 x 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
22:
num=num*21*20*19*18*17*16*15*14*13*12*11*10*9*8*7*6*5*4*3*2*1;
System.out.println("22!= 22 x 21 x 20 x 19 x 18 x 17 x 16 x 15 x 14 x 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
23:
num=num*22*21*20*19*18*17*16*15*14*13*12*11*10*9*8*7*6*5*4*3*2*1;
System.out.println("23!= 23 x 22 x 21 x 20 x 19 x 18 x 17 x 16 x 15 x 14 x 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
24:
num=num*23*22*21*20*19*18*17*16*15*14*13*12*11*10*9*8*7*6*5*4*3*2*1;
System.out.println("24!= 24 x 23 x 22 x 21 x 20 x 19 x 18 x 17 x 16 x 15 x 14 x 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
25:
num=num*24*23*22*21*20*19*18*17*16*15*14*13*12*11*10*9*8*7*6*5*4*3*2*1;
System.out.println("25!= 25 x 24 x 23 x 22 x 21 x 20 x 19 x 18 x 17 x 16 x 15 x 14 x 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
26:
num=num*25*24*23*22*21*20*19*18*17*16*15*14*13*12*11*10*9*8*7*6*5*4*3*2*1;
System.out.println("26!= 26 x 25 x 24 x 23 x 22 x 21 x 20 x 19 x 18 x 17 x 16 x 15 x 14 x 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
27:
num=num*26*25*24*23*22*21*20*19*18*17*16*15*14*13*12*11*10*9*8*7*6*5*4*3*2*1;
System.out.println("27!= 27 x 26 x 25 x 24 x 23 x 22 x 21 x 20 x 19 x 18 x 17 x 16 x 15 x 14 x 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
28:
num=num*27*26*25*24*23*22*21*20*19*18*17*16*15*14*13*12*11*10*9*8*7*6*5*4*3*2*1;
System.out.println("28!= 28 x 27 x 26 x 25 x 24 x 23 x 22 x 21 x 20 x 19 x 18 x 17 x 16 x 15 x 14 x 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
29:
num=num*28*27*26*25*24*23*22*21*20*19*18*17*16*15*14*13*12*11*10*9*8*7*6*5*4*3*2*1;
System.out.println("29!= 29 x 28 x 27 x 26 x 25 x 24 x 23 x 22 x 21 x 20 x 19 x 18 x 17 x 16 x 15 x 14 x 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
case 
30:
num=num*29*28*27*26*25*24*23*22*21*20*19*18*17*16*15*14*13*12*11*10*9*8*7*6*5*4*3*2*1;
System.out.println("30!= 30 x 29 x 28 x 27 x 26 x 25 x 24 x 23 x 22 x 21 x 20 x 19 x 18 x 17 x 16 x 15 x 14 x 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = "+num+"\n");
break;
default:
System.out.println(" <<Invalid Input>> \n");
break;

}
System.out.print("<<<Enter Yes to repeat or any key to stop>>> ");
ans=mik.readLine();
System.out.println("");
}while (
ans.equalsIgnoreCase("yes"));
}

09/28/2011 10:11 Obilee#2
what the...

HTML Code:
  public static void main (String[] args) {
       Scanner scan = new Scanner(System.in);
	System.out.print("n: ");
	int n = scan.nextInt();
	System.out.println("n! = "+fak(n));
   }
   private static long fak(int n){
      if (n==0||n==1) return 1;
      else return n*fak(n-1);
   }
09/28/2011 10:39 SmackJew#3
Quote:
Originally Posted by zepolzirk View Post
Press "YES" to repeat: YES
Where's the "YES" key? Can't find it.
09/28/2011 15:25 zepolzirk#4
Quote:
Originally Posted by SmackJew View Post
Where's the "YES" key? Can't find it.
ahh sorry my mistake it should be

String ans="YES";
09/28/2011 17:35 SmackJew#5
Quote:
Originally Posted by zepolzirk View Post
ahh sorry my mistake it should be

String ans="YES";
You don't even realize when you're being made fun of?
10/02/2011 21:54 .Infinite#6
€: Never mind... But I still think you should have a look at loops...