Results 1 to 5 of 5
  1. #1
    Libertyman is offline Member
    Join Date
    Jun 2010
    Posts
    26
    Rep Power
    0

    Default 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.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Libertyman View Post
    System.out.println("Count is: "number=number+number);
    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.

  3. #3
    Libertyman is offline Member
    Join Date
    Jun 2010
    Posts
    26
    Rep Power
    0

    Default

    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 you
    Last edited by Libertyman; 06-21-2010 at 02:30 PM.

  4. #4
    Libertyman is offline Member
    Join Date
    Jun 2010
    Posts
    26
    Rep Power
    0

    Default

    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 20
    Last edited by Libertyman; 06-21-2010 at 04:10 PM.

  5. #5
    Libertyman is offline Member
    Join Date
    Jun 2010
    Posts
    26
    Rep Power
    0

    Default

    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

  1. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  2. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  3. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  4. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 10:12 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •