Java Compilation

Java Compilation

How would i write a complete java program which, using a do-while loop, reads integers from the keyboard and sums them until the sum hits or exceeds 30. Then print out the sum.

is it like this:

import java.util.Scanner;
public class Sum
{
public static void main(String[] args)
{
int sum=0;
int sum1=0;
int total;
Scanner kb = new Scanner(System.in);
System.out.print("enter an integer: ");
sum = kb.nextInt();

do
{
total = sum + sum1;
System.out.print("enter an integer: ");
sum = kb.nextInt();
}
while(sum>=30)
System.out.println("Sum is " + total);
}
}

Here is an example of how i want the output to look like:

enter an integer: 35
Sum is 35

or this:

enter an integer: 20
enter an integer: -5
enter an integer: 9
enter an integer: 6
Sum is 30
View Answers

March 11, 2010 at 1:01 PM

Hi Friend,

Try the following code:

import java.util.Scanner;
public class Sum
{
public static void main(String[] args)
{
int sum=0;
Scanner kb = new Scanner(System.in);
do
{
System.out.print("enter an integer: ");
int num=kb.nextInt();
sum=sum+num;
}
while(sum!=30);
System.out.println("Sum is " +sum);
}
}

Thanks

Ads









Related Tutorials/Questions & Answers:
java Compilation
java program compilation time following error is occur in my system Note...java Compilation  import java .util.*; class ArrayListDemo... in Java 5 and later if you're using collections without type specifiers (e.g.
java Compilation
java program compilation time following error is occur in my system Note...java Compilation  import java .util.*; class ArrayListDemo... in Java 5 and later if you're using collections without type specifiers (e.g.
Advertisements
java Compilation
java program compilation time following error is occur in my system Note...java Compilation  import java .util.*; class ArrayListDemo... in Java 5 and later if you're using collections without type specifiers (e.g.
Java compilation error - Java Beginners
Java compilation error  Method overridden problems in compile time error
java compilation error - Hibernate
java compilation error  hi, i have made an registration page whosevalues should be inserted into database. i have used hibernate and eclipse it is working fine. but when i make a war file and deploy it using ant and then run
compilation error - Java Beginners
compilation error  class s { public static... is the reason?  Hi Friend, In Java there are some specific literal... not in the Hexadecimal or Octal. Here is fraction of Java code example which will help you
Java Compilation - Java Beginners
Java Compilation  How would i write a complete java program which, using a do-while loop, reads integers from the keyboard and sums them until the sum hits or exceeds 30. Then print out the sum. is it like this: import
Java Compilation - Java Beginners
Java Compilation  I want to write a complete java program that catenates the file named "first" with the named "second" and produces a file named "third". If either input file "first" or "second" does not exist in the current
Java Compilation - Java Beginners
Java Compilation  I want to write a Java program that asks the user for the names of 3 files. I want the first file to be opened for reading and the other two files to be opened for writing. I want the program to read
java compilation error - Java Beginners
java compilation error  i have downloaded java software(version 6... from where i should download java or what to do now?  Have you set the java_home and path in your environmental variables in "My Computer
Java Compilation error. Hibernate code problem. Struts first example - Hibernate
Java Compilation error. Hibernate code problem. Struts first example  Java Compilation error. Hibernate code problem. Struts first example
Does the object type known during compilation in Java?.
Does the object type known during compilation in Java?.  class A { } class B extends class A { } class checkType { public static void main(String[] args) { A ObjA = new B();/// Here ObjA is of type A--> this fact
compilation problem
compilation problem  struts 2.0 : i have included all 5 jars in lib folder,but its giving compilation problem that package open symphony doesnot exist ( though i imported it) & cant recognise SUCCESS etc symbol.i think i
compilation problem
compilation problem  struts 2.0 : i have included all 5 jars in lib folder,but its giving compilation problem that package open symphony doesnot exist ( though i imported it) & cant recognise SUCCESS etc symbol.i think i
compilation fail
compilation fail  package jsptube.tutorials.servletexample; import javax.servlet.GenericServlet; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import java.io.IOException; import
compilation error
compilation error  Hi my program below is not compiling,when I try to compile it i'm getting the error message "tool completed with exit code 1".The loop has to be at the end of the program can you help me. Blockquoteimport
compilation errors
compilation errors  package jsptube.tutorials.servletexample; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import
command prompt is compilation is not working
command prompt is compilation is not working   Dear java users can anyone help me to run the program in command line prompt i tried by selecting... and the class file is also getting generated when i run the class file using java
result after compilation
c file compilation
program created and compilation error of connection rightly created n tested
program created and compilation error of connection rightly created n tested  // I have made connection as the data source dsn2 and i have also created table named Tab1 in SQL server 2008..it is compiled correctly but is giving
Hibernate Configuration
org.hibernate.SessionFactory . Various XML mapping files or Java Annotations compilation build...The configuration of Hibernate is handled by the instance of org.hibernate.cfg.Configuration . It symbolizes the complete mapping between Java data types
Java
Java  What is a compilation unit
java & c
java & c  why c language is called top down approach? why java is called bottom to top approach?   In C, compilation stats from the very top of the program code. In Java, on the other hand, we have user defined data
java fundamental question related to string
java fundamental question related to string  public class Myclass...??); } } Output? a) the pgm will throw ClassCastException b) compilation fails since s==sb illegal c) compilation fails since s.equals(sb) illegal d
java fundamental question
java fundamental question  public class WrapTest3 { Public static void main(String args[]) { String s="98.6"; // insert code here } } Which will cause compilation error: a) float f1=Float.floatValue(s); b) float f2=new Float
java fundamental question
java fundamental question  public class TestVarArgs { Public void display(Double i, double ‚?¶ j) { System.out.println(‚??Double& var args‚... Double & Double c) compilation error due to ambiguous method calls d
php java
php java  why PHP is becoming more popular than java   PHP is more popular than Java because of following reasons: 1)PHP web hosting... Java for web development: Lower barrier to entry Dynamic typing Simpler hello
What are the features of web designing
of website solely depends upon the layout, structure and compilation of company
Which is better .Net or Java?
or Ahead of Time Compilation while Java has Just In Time compilation. Java...When a developer needs to choose between Java or .NET to develop a new..., it can also be used on other operating system. Java on the other hand
Java Generics
replace a suitable java type during compilation with the help of these parametric... .style1 { list-style-type: square; } Java Generics The Generics... compilation. The main reason of using Generics code is to assures type safety

Ads