Results 1 to 5 of 5
Thread: is my code okay (While)
- 06-21-2010, 09:13 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 26
- Rep Power
- 0
is my code okay (While)
i am trying to make java display the same question 10 times, that asks the user for a number.
so java should ask the user, give me a number , the user enters
then the next time the user enters the number, java should sum it with the previous number
and do so in a loop, till it reaches 10 times
import java.io.*;
class Ex1 {
public static void main(String[] args){
Console console=System.console();
System.out.println("Please enter your number");
String number;
number=console.readLine();
int count=1;
count=Integer.parseInt(number);
while (count < 10) {
System.out.println("Count is: "number=number+number);
count=count+1;
}
}
}
or is it something like that?
import java.io.*;
class Ex1 {
public static void main(String[] args){
Console console=System.console();
System.out.println("Please enter your number");
String number;
number=console.readLine();
int n1;
n1=Integer.parseInt(number);
int count=1;
while (count < 10) {
System.out.println("Count is: "number=n1+n1);
count=count+1;
}
}
}
or
import java.io.*;
class Ex1 {
public static void main(String[] args){
int count=0;
while (count < 10) {
Console console=System.console();
System.out.println("Please enter your number");
String number;
number=console.readLine();
int n1;
n1=Integer.parseInt(number);
System.out.println("Count is: "number=number+n1);
count=count+1;
}
}
}
import java.io.*;
class Ex1 {
public static void main(String[] args){
int count=0;
while (count < 10) {
Console console=System.console();
System.out.println("Please enter your number");
String number;
number=console.readLine();
int n1;
n1=Integer.parseInt(number);
System.out.println("Count is: "n1);
int n2;
System.out.println("Count is: "n2=n1+n2);
count=count+1;
}
}
}Last edited by Libertyman; 06-21-2010 at 10:14 AM.
- 06-21-2010, 12:45 PM #2
all your System.out.println() cause a compile error because the syntax is wrong. i wrote an other version which declare a int variable named sum and then prints out the count and the sum variable.
Java Code:import java.io.*; class Ex1 { public static void main(String[] args) { Console console = System.console(); System.out.println("Please enter your number "); String number; number = console.readLine(); int count = 1; count = Integer.parseInt(number); int sum = 0; while (count < 10) { sum += count; System.out.println("Count is: " + count + " and sum is: " + sum); count = count + 1; } } }
do not start this code in a ide like eclipse but start it from a os-box with java Ex1.
- 06-21-2010, 02:26 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 26
- Rep Power
- 0
aah.. i get it , you put 2 variables, count and sum
why count= 1 and not count = 0
and
how does the programs makes , add count and another count to equal sum
what i want java to do is to ask the same question 10 times, get each time a number, add it to the previous number
thank youLast edited by Libertyman; 06-21-2010 at 02:30 PM.
- 06-21-2010, 03:59 PM #4
Member
- Join Date
- Jun 2010
- Posts
- 26
- Rep Power
- 0
it isnt quite the code.
i need the sum to add my previous, number, i wonder how to do it
import java.io.*;
class Ex11 {
public static void main(String[] args) {
int count;
count=0;
while (count < 10) {
Console console = System.console();
System.out.println("Please enter your number ");
String number;
number = console.readLine();
int count1;
count1 = Integer.parseInt(number);
int sum = +count1;// i need help here
sum += count;
System.out.println("Count is: " + count1 + " and sum is: " + sum);
count = count + 1;
}
}
}
what i need the program to do
put number
5
your count 5, your sum= 5
put number
10
your count 10 your sum 15
put number
5
your count 5 yoru sum 20Last edited by Libertyman; 06-21-2010 at 04:10 PM.
- 06-21-2010, 04:00 PM #5
Member
- Join Date
- Jun 2010
- Posts
- 26
- Rep Power
- 0
import java.io.*;
class Ex11 {
public static void main(String[] args) {
int count;
count=0;
while (count < 10) {
Console console = System.console();
System.out.println("Please enter your number ");
String number;
number = console.readLine();
int count1 = 1;
count1 = Integer.parseInt(number);
int sum = count1+; // i need help in this part. to change it so it would add my previous count1
sum += count;
System.out.println("Count is: " + count1 + " and sum is: " + sum);
count = count + 1;
}
}
}
Similar Threads
-
can any one pls send me a sample code for calling a jsp code in swings
By sniffer139 in forum AWT / SwingReplies: 1Last Post: 03-04-2010, 11:19 AM -
Convert java code to midlet code
By coldvoice05 in forum New To JavaReplies: 1Last Post: 08-12-2009, 11:14 AM -
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 01:21 PM -
I need help fixing my code.. or non code?
By MrHuggykins in forum New To JavaReplies: 1Last Post: 03-19-2008, 10:12 PM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks


Bookmarks