Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    Libertyman is offline Member
    Join Date
    Jun 2010
    Posts
    26
    Rep Power
    0

    Default how do i write this code?

    the goal is to make the program get 10 random numbers and then choose the number which is the biggest. i think i am wrong somewhere. how can i write the code correctly?


    class Ex4
    {
    public static void main(String[] args)
    {
    int count;
    count=0;
    while(count<10)
    {
    Console console=System.console();
    System.out.println("put a number");
    String number
    number=console.readLine();
    int n1;
    n1=Integer.parseInt(number);

    System.out.println("put next number");
    String next_number
    next_number=console.readLine();
    int n2;
    n2=Integer.parseInt(next_number);
    System.out.println("biggest value"+(n2>n1?n2:n1));
    count=count+1;
    }

    }




    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default

    Can you describe the logic/design of your program?
    A good design before writing code, makes for a better solution.

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

    Default

    the program should give the user 10 questions/options to put any number he wants.

    after he put the 10 numbers when asked, the program at the end should choose the highest number

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default

    What you've just posted is a statement of the problem. It doesn't sound like the original problem??

    Now you need to come up with a design on how to solve that problem.
    That means what the steps are to solve the problem.

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

    Default

    it tells me that i have an error:

    Console console = System.console();

    Ex:java13 cant find symbol
    symbol class console
    Location class Ex4


    why? how do i correct that?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default

    Please copy and paste the FULL text of the error message. There is info there that you left out.

    Do you have an import statement for the package the Console class is in?

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Where did you get that code from in the first place? Read the API doc for System.console() and read the basic IO tutorial.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

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

    Default

    import java.io.*;
    class Ex4
    {
    public static void main(String[] args)
    {


    int count;
    count=0;
    while(count<10)
    {
    System.out.println("put a number");
    Console console=System.console();
    String number;
    number=console.readLine();
    int n1;
    n1=Integer.parseInt(number);

    System.out.println("Your number is "+ " " + n1);

    count=count+1;
    }
    int n2;


    System.out.println("Your Biggest number is"+ n2>n1?n2:n1);\\ i need to set this line to give me the biggest value that was displayed

    }




    }

    the program works like that. the user types 10 numbers. and what i need to do is in the last line present the largest number he typed.
    i need one command line to make this work, but i dont know what it is

  9. #9
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    For starters your code is fragile, but you seem to accept that. Secondly you never remember the numbers being entered, so you have no chance to find the biggest number.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

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

    Default

    please tell me how to do that. i want to get over with that exercise. i am workign on those codes since the morning


    what if i write that the user needs to type 10 numbers... - that would be n1

    would i be able to make a formala finding the largest number?
    Last edited by Libertyman; 06-21-2010 at 04:52 PM.

  11. #11
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    No way, it doesn't work that way. Learn! You've been given the link to the basic tutorials, find out how to use arrays and sorting algorythms, or do it with pen and paper and imagine there could be somebody asking you anytime about the biggest number so far.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default

    How would you do it if you were using a piece of paper and pencil?
    A user gives you a number
    You write it down on the paper
    You get the next number.
    Is this number bigger than the last number?
    If so, replace the last number written with the new number
    Loop back to get the next number

    When you are done, the number written down will be the largest.

    In a program use variables to save values instead of writing them down

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default

    do it with pen and paper
    @PhHein Great minds work in ....

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

    Default

    That would seem to be a long code.

    giving a value to each variable.

    like this

    int 1
    int b
    int c
    int d
    int e.

    i dont think it would be the answer.

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default

    giving a value to each variable.
    Can you explain your last post?

    There is only one variable to hold the current largest one.
    And one variable to hold what the user input.

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

    Default

    Java Code:
    import java.io.*;
    import java.io.*;
    class Ex4
    {
    	public static void main(String[] args)
    	{       
                           
                           
    		int count;
    		count=0;
    		while(count<10)
    		{         
                            System.out.println("put a number");  
                            Console console=System.console(); 
                            String number;
                            number=console.readLine();
                            int n1;
    		        n1=Integer.parseInt(number);    
                            int n2;
                            n2=n1       
                            System.out.println("Your number is "+ " " + n1 + "Biggest input" + (n2>n1)?n2:n1);
                            
    		        count=count+1;
    		}
                      
    
    	}
    
    
    
    
    }

    or


    Java Code:
    import java.io.*;
    class Ex4
    {
    	public static void main(String[] args)
    	{       
                           
                           
    		int count;
    		count=0;
    		while(count<10)
    		{         
                            System.out.println("put a number");  
                            Console console=System.console(); 
                            String number;
                            number=console.readLine();
                            int n1;
    		        n1=Integer.parseInt(number);    
                            int n2;
                            n2=n1>n2?n1:n2;       
                            System.out.println("Your number is "+ " " + n1 + "Biggest input"+ " " + n2);
                            
    		        count=count+1;
    		}
                      
    
    	}
    
    
    
    
    }
    why does he tell me that System.out.println line is wrong?
    why cant i use a trillneary expression?



    actually the whole think is a mess. i want to put a new statement out of the loop
    System.out.println(" your Biggest number" = )

    but i wonder how can i make the programme make that calculation
    Last edited by Libertyman; 06-21-2010 at 08:07 PM.

  17. #17
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Two problems: your use of = within the expression rather than ==, and your trying to compare a String with an int.

    Edit: I see that you modified your post, which I'll post here:
    Java Code:
                            int n1;
    		        n1=Integer.parseInt(number);    
                            int n2;
                            n2=n1>n2?n1:n2;       
                            System.out.println("Your number is "+ " " + n1 + "Biggest input"+ " " + n2);
    but you're still using = to compare one number with the other, not ==. Please understand that = assigns a value to a variable whereas == compares two variables. Also, you are comparing n1 with n2, but never assigned any value to n2 so it's meaningless. What is n2 supposed to represent?
    Last edited by curmudgeon; 06-21-2010 at 08:20 PM.

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

    Default

    i saw that problem with n2. i deleted the whole thing

    i want n2 to be the previous number..

    but because it is a loop..the whole thing is too confusing and tricky.

    i think someone who knows java well, should know..

    after i have the values from the loop. how can i make the application list the highest value from the list?

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default

    You still haven't come up with a design for how the program should work.
    Look at this:
    There are two variables: currVal and highestVal.
    Set highestVal to the smallest number possible so that any number input will be larger
    1) get the next number from user into currVal
    2) compare currVal to highestVal
    3) if currVal is greater than highestVal then set highestVal to currVal (ie save the new highest value)
    4) test if 10 numbers gotten yet (loop test)
    5) if < 10 numbers go to step 1)
    6) if 10 numbers, then highestVal has the highest value entered

  20. #20
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by Libertyman View Post
    i saw that problem with n2. i deleted the whole thing

    i want n2 to be the previous number..
    Then declare n2 before the loop even starts:
    Java Code:
    int n2;  // this is a variable declaration
    But you probably want to give it some initial value too, so you'll need to declare and assign it, again before the while loop:
    Java Code:
    int n2 = Integer.MIN_VALUE;  // this is a variable declaration with an assignment
    while (count < 10) {
      .....

Page 1 of 2 12 LastLast

Similar Threads

  1. What are you using to write your code?
    By CaptainMorgan in forum New To Java
    Replies: 995
    Last Post: Today, 09:29 AM
  2. Replies: 57
    Last Post: 07-15-2010, 04:10 AM
  3. How To Write Unmaintainable Code
    By tim in forum Forum Lobby
    Replies: 2
    Last Post: 02-07-2010, 09:19 PM
  4. Can I write code in 2 different files ?
    By fartek in forum New To Java
    Replies: 2
    Last Post: 02-14-2009, 03:48 PM
  5. Help me to write a secure code
    By Bhavis in forum Advanced Java
    Replies: 4
    Last Post: 01-21-2009, 06:06 AM

Posting Permissions

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