Wrox Programmer Forums
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 5th, 2006, 07:22 AM
Registered User
 
Join Date: Jan 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Beginning java

this is about 1!+2!+3!+...+n!=?
below is program code,it can run but there is out error.

public class Demo{
public static void main(String args[]){
double num=1,sum=0;
int n=0;
System.out.print("Please input a number:");
try{
n=integer.parInt(System.in.read());
}catct(IOException e){}
for(int i=1;i<=n;i++){
num=num*i;
sum=sum+num;
System.out.println(n+"result is:"+sum);
}
}
}
when it run,
if i input 4,
output is "53 result is:....",
why?


 
Old January 12th, 2006, 08:20 AM
Registered User
 
Join Date: Jan 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You misspelled alot of things in the code...

Code:
import java.util.*;

public class Demo{

    public static void main(String args[]){

        int num=1,sum=0;

            System.out.print("Please input a number:");
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();


            for(int i=1;i<=n;i++){
                num=num*i;
                sum=sum+num;
            System.out.println(n+" result is:"+sum);

            }
    }
}
 
Old January 15th, 2006, 09:50 AM
Authorized User
 
Join Date: Dec 2005
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Below code is very good and sample
Scanner in = new Scanner(System.in);
int n = in.nextInt();



JAVA Email
http://www.wisesoft.biz/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginning Java? elvisfeverr Java Basics 3 September 16th, 2017 04:57 AM
Beginning Java 2 Gustavo Castro Java Basics 3 January 5th, 2008 12:08 AM
Beginning Programmer to Java - Need help please Seph Java Basics 2 May 28th, 2007 01:56 AM
Java Code - Wrox - Beginning JAVA - Ivor Horton ponguru Java Databases 3 May 18th, 2006 12:30 PM
New Beginning Java for v1.5 ? wpilgri BOOK: Beginning Java 2 6 May 10th, 2004 12:49 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.