How to change double random value to int value?

I need to change double random number to int in my program. Please help
Here's my program
import java.io.*;
class GuessingGame
public static void main( String[] args ) throws IOException
int guess;
BufferedReader stdin = new BufferedReader(
new InputStreamReader( System.in ) );
String input;
double somevalue;
int attempt = 0;
boolean win = false;
somevalue=Math.random()*10 ;
while ( attempt < 3 && !win )
boolean correct = true;
//First Guess
System.out.println("\nI am thinking of a number from 1 to 10.");
System.out.println("You must guess what it is in three tries.");
System.out.println("Enter a guess:");
input = stdin.readLine();
guess = Integer.parseInt( input );
if ( guess != somevalue )
correct = false ;
System. out.println("Wrong");
//Second Guess
System.out.println("Enter a guess: ");
input = stdin.readLine();
guess = Integer.parseInt( input );
if ( guess != somevalue )
correct = false ;
System. out.println("Wrong");
//Third Guess
System.out.println("Enter a guess: ");
input = stdin.readLine();
guess = Integer.parseInt( input );
if ( guess != somevalue )
correct = false ;
System. out.println("Wrong");
//Result
if ( correct )
System. out.println("Right");
System.out.println("You won the game!");
win = true;
else
          System.out.println("The correct number was:"+ somevalue);
System.out.println("You lost");
attempt = attempt + 1;

Ceranith, thanks. It works now. You were very helpful. I searched and found your answer on a similar question using nextInt(int n). I tried to use that approach to my program (for learning purposes)as well, but it didn't work. Could you help to find out what's wrong?
Here it is:
import java.io.*;
import java.util.*;
class GuessingGame
public static void main( String[] args ) throws IOException
int guess;
BufferedReader stdin = new BufferedReader(
new InputStreamReader( System.in ) );
String input;
int attempt = 0;
boolean win = false;
int n=10;
Random numb = new Random();
int somevalue = numb.nextInt(int n)+1;
while ( attempt < 3 && !win )
boolean correct = true;
//First Guess
System.out.println("\nI am thinking of a number from 1 to 10.");
System.out.println("You must guess what it is in three tries.");
System.out.println("Enter a guess:");
input = stdin.readLine();
guess = Integer.parseInt( input );
if ( guess != somevalue )
correct = false ;
System. out.println("Wrong");
//Second Guess
System.out.println("Enter a guess: ");
input = stdin.readLine();
guess = Integer.parseInt( input );
if ( guess != somevalue )
correct = false ;
System. out.println("Wrong");
//Third Guess
System.out.println("Enter a guess: ");
input = stdin.readLine();
guess = Integer.parseInt( input );
if ( guess != somevalue )
correct = false ;
System. out.println("Wrong");
//Result
if ( correct )
System. out.println("Right");
System.out.println("You won the game!");
win = true;
else
          System.out.println("The correct number was:"+ somevalue);
System.out.println("You lost");
attempt = attempt + 1;

Similar Messages

  • How to change double i = Math.random(); value to (int) value

    I am trying to find out how to convert a given double value to integer, which is, to conver double myNumber = Math.random() to (int) value. Here is the sample program look like this.
    Thank you for the help.
    import javax.swing.JOptionPane;
    public class numbers
    public static void main(String[] args)
    int upperLimit = Integer.parseInt(JOptionPane.showInputDialog
    (null, "Enter the upper range integer."));
    double myNumber = Math.random();
    //Test of Random numbers.
    System.out.println("The Upper random number limit is: " +
    myNumber * upperLimit);
    } //End of main.
    } // End of class.

    Not sure exactly what you want and why you want to change the double to an int. I'm assuming by this you want to generate random integers, from 1 to the upper limit the user enters? If so, this is how I would do it:import java.util.*;
    import javax.swing.*;
    public class Numbers
        public static void main(String[] args)
            int upperLimit = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the upper range integer."));
            Random r = new Random();
            int myNumber = r.nextInt(upperLimit) + 1;
            System.out.println(myNumber);
    }

  • How to change double to int?

    i got a double
    double x
    x = 3 * 1.2
    how to change x to int type?
    thx a lot

    You can't change x, it's declared as type double.
    You can retrieve the value of x as an int by using a cast:
    int y = (int) x;

  • How should I change from Object value to int value?

    ArrayList ap = new ArrayList();
    ap.add(10);
    I want int value...
    How should I do?
    int a22 =(int)ap.get(0);
    It's not working.
    Help me.

    This is tricky. Before 1.5, you could not do ap.add(10), you would have to do
    ap.add(Integer.valueOf(10)); // OR
    ap.add(new Integer(10));
    This is called autoboxing and the compiler provides the extra code for you.
    If you want to get it out as an int, you have to choices
    1) Use generics
    List<Integer> ap = new ArrayList<Integer>();
    ap.add(10);
    int ten = ap.get(0);2) Cast
    List ap = new ArrayList();
    ap.add(10);
    int ten = ((Integer)ap.get(0).intValue();

  • How to change layout of new calculated item values?

    Hi,
    Is there a way on how to change colours or add a percentage symbol on the values of a calculated items?
    I do not want to change the source measures in the criteria area I used to get the calculated items results.
    Any idea?
    Regards
    Giuliano

    I solved this by using an other approach.
    Created a report and filtered the first resolts by certain value. Added a label.
    Used combine with similar request and filtered again.
    Created so many combines for the number of results I need to filters
    In result columns renamed the label with a description which is a common denominator for all the labels
    In pivot table, moved the main label to Rows and appled the sum
    the reults is below
    https://docs.google.com/file/d/0B0nRf4E3yBfpWS1WVjJjbWZyTG8/edit?usp=sharing
    Regards
    G

  • How to change double spaces to single space

    For some reason that I don't know about, the majority of the
    time that I download a PHP or HTML document from the web server to
    my local PC the document gets double spaces injected into each line
    of code. In HomeSite there is a simple command in the menu to
    change double spacing to single spacing. Why does Dreamweaver not
    have this, or if it does, then where is it? I cannot spend all of
    my time mannually removing each double spaced line. I tried the
    search and replace for this, but then I loose my tab formatting and
    it makes the code hard to read. Can anybody help or point me to an
    extension that works for this or sdomething? Or, do I just need to
    reload HomeSite and work in it instead of Dreamweaver?

    "carl@csi" <[email protected]> wrote in
    message
    news:fgn8sd$bn4$[email protected]..
    > For some reason that I don't know about, the majority of
    the time that I
    > download a PHP or HTML document from the web server to
    my local PC the
    > document
    > gets double spaces injected into each line of code. In
    HomeSite there is a
    > simple command in the menu to change double spacing to
    single spacing.
    > Why
    > does Dreamweaver not have this, or if it does, then
    where is it? I cannot
    > spend
    > all of my time mannually removing each double spaced
    line. I tried the
    > search
    > and replace for this, but then I loose my tab formatting
    and it makes the
    > code
    > hard to read. Can anybody help or point me to an
    extension that works for
    > this
    > or sdomething? Or, do I just need to reload HomeSite and
    work in it
    > instead of
    > Dreamweaver?
    Open the find/replace dialog, and check "Use regular
    expressions".
    then type the following in the search box
    \n\s*\n
    and type Ctrl-Enter in the replace box
    this should replace double line-breaks (with or without
    whitespace between
    them) with a single one.
    Joris van Lier

  • How to change BPS multiplanning area variable selected value

    Hi!
    I have a variable, Drop down list, filld with an exit. Now the variable is filld with values and the value selected last time is marked as selected when application is started. I need to start with the variable unselected or take control over whitch value to have as selected.
    Any sugestions how to do that?
    Edited by: Jan Kihlström on May 29, 2008 8:38 AM
    Edited by: Jan Kihlström on Jun 10, 2008 9:52 AM

    Hi,
    whenever we execute layout second time, it will always show the last saved variable value.
    what can you do in this case, clear the output of the variable exit at the start in the exit itself.

  • How to change Stored Procedure Parameters to a value in a formula?

    My Client is using Crystal 8.5 on MSSQL 2005.  The crystal report is called from vb6 application, the client does not have access to the vb6 source code (they are upgrading vb.net), therefore I can only work in the crystal report.
    The vb6 code updates 2 formula fields Start_Date and End_Date in the crystal report.  I would like to pass these values as parameters to the stored procedure and suppress the "Enter Parameter Values" box.
    Stored Procedure: tcs_AMP_eSuper
        Parameters:  @startDate, @EndDate
    What I would like in crystal:
        {?@startDate} = {@Start_Date}
        {?@endDate} = {@End_Date}
    Thanks
    Barry

    Hi Barry,
    You can't run a Stored Procedure with Parameters unless you fill in the values and you can't get the values unless you prompt for them or provide then in the connection properties in code.
    So in your app just fill in the parameter values when logging in. Create your own Param UI or get them from somewhere from a preselected list. As long as they are valid then CR won't prompt for them.
    I don't understand why this is such a problem for you? Parameters will prompt, it's by definition. Create a UDL file, open NotePad and then rename it to *.udl. Double click the file and the OLE DB Provider log on UI will pop up. Connect to your SP with parameters and then save it. Open in edit mode and you'll see the SQL to open the SP with Values in the connection.
    Something like: exec YourSP;1, startdateValue, Enddate value
    So in code use that line in the log on method and the user won't get prompted. How you fill in those dates is up to you.
    Don

  • How to change the decimal places in the value axis of a chart in WAD?

    Hello,
    The key figure in my chart has 2 decimal places and I want the value axis to show the same. How to do that?
    Thanks in advance, Nathalie

    Hi,
    change the Format property of the ValueAxis-Line to 0.00
    Then the axis displays its values including two digits after the decimal.
    Regards, Kai

  • How to change R/3 System ID & Client values for a Business System in ID

    I am a beginner in PI or XI. I am trying to modify R/3 System ID & Client# under Adapter Specific Identifiers of a Business System in ID. But both are in output mode and I tried to modify in SLD and then resynch at ID level, but I could not find a place where these parameters are specified for that Business System in SLD.
    Please help me!

    I have a Client 500 and R3 Server EN3 now these two things were already there on a Business System, but Admn did some thing on my test system and now we have another Business System pointing to TS of type WEBAS-ABAP and Admin assigned this same client 500 and EN3 server name in SLD to this new Business system, but as you know these changes will not reflect in my ID scenario automatically unless we do it manually with synchronization button under Adapter Specific Identifiers.
    So my requirement is now to remove those values from the old BS and keep them or get them through synch for my new BS in my ID scenario, which I am not able to do it. The new BS is pointing to WEBAS-ABAP type TS but the old one I think it is now not pointing to any TS any more, it just left like a orphan with no technical System, I think Admn did some magic with bad practice, I believe.
    HOw do I handle this situation?

  • How does one generate random integers; first 5 values occur 2 times more than later values

    for the following code, how can I get the first 5 values to occur twice as often as the last 5 values?
    Do I have to use nested loops?
    Begin
      For n in 1 .. 10
      Loop
         dbms_output.put_line (ROUND(dbms_random.value(1, 10)));
    End Loop;
    End;

    Hi,
    If you had a lottery with 10 people participating, and you wanted to fix it so that each person had an equal chance of winning, what could you do?  One thing you could do is give tickets numbered 1, 2, 3, ..., 10 to the people, and then pick an integer in the range 1-10 at random.
    If you had a lottery with 10 people participating, and you wanted to fix it so that 5 of the people were twice as likely to win as the other 5, what could you do?  One thing you could do is to give 2 tickets to the first 5 people, but only 1 ticket to the other 5.  Since you have 15 tickets now, you'd pick a random integer in the range 1-15.
    Here's one way to do that in SQL.  The query below conducts 15 thousand lotteries.  We want the numbers 1-5 to get picked about 2000 times each, and the numbers 6-10 to occur about 1000 times each:
    WITH got_n AS
        SELECT  MOD ( TRUNC (dbms_random.value (0, 15))
                    , 10
                    ) + 1    AS n
        FROM    dual
        CONNECT BY LEVEL <= 15000
    SELECT    n
    ,         COUNT (*)    AS cnt
    FROM      got_n
    GROUP BY  n
    ORDER BY  n
    Sample output:
             N        CNT
             1       2048
             2       1939
             3       2020
             4       2066
             5       2007
             6        998
             7        967
             8       1004
             9        980
            10        971

  • How to change background color in List of Values (LOV) ?

    Hiya
    In Forms 10g, the background color in list of values (LOV) is White whereas it was grey in Form 6i. Also, the position of 3 buttons i.e. Find, OK and Cancel on LOV is different than what it used to be in Forms 6i.
    Is it possible to change the background color and the position of buttons of LOV in Forms 10g ?
    Cheers
    Mayur

    I would like to share the outcome of this issue.
    I had posted this same issue on Metalink yesterday. They (Metalink guys) have confirmed that its an bug in Oracle 10g DS R2 and it would be fixed in subsequent releases.
    Following is an official response from Metalink:
    This issue has been addressed to Oracle Development to be investigated/fixed in the published Bug:
    Bug 1559776 <BACKGROUND COLOR SETTING FOR LOV OBJECT DOES NOT WORK ON WEB>
    Cheers
    Mayur

  • Conditional Formatting - how to change cell colour based on cell value

    I am in the process of moving some Excel spreadsheets into Numbers on my iPad. The Excel spreadsheet had conditional formatting in certain cells. If the cell value was less than a certain amount (<46 for example), the cell background would automatically turn yellow.
    Is there any way to make Numbers on iPad do this?
    Thanks for any help you can provide.
    Scott

    I beleive this is one of those doable tings on desktop that gets taken out on ipad. If you would like to suggest this as an idea for apple to put in, please go to http://www.apple.com/feedback

  • Change the Purchase Order : Item - Conditions value

    Hi,
    How to change the Purchase Order : Item - Conditions value.
    In my PO, Item conditions shows the SKTO - Cash Discount value is 0. I want to change the Discount value 10.
    How can i change it??

    Hi,
    Goto the Transaction code MN05 and give the CONDITION TYPE SKTO and change the value
    MN05 is the Transaction code for Purchase order  condition types.
    Please check and revert back if you need more details.
    regards,
    santosh

  • Help!!!!!  anyone know how to change a Double to an int??

    Help!!!!! anyone know how to change a Double to an int?? for use in an array[int].

    I don't know why would you want to depend on a double value to build your array but programmatically just use plain old cast: i = (int) double_value;
    Keep in mind though, the integer value will be truncated.

Maybe you are looking for

  • Changing order of sort categories...

    I have 5th gen ipod - not sure if i can do this? Whenever I search via "Genre", the next search category is "Artist". I want to try to change it so that the next search category is "Album" instead. Anyone have any ideas?

  • Web Applet Report that creates email for Opportunity Details

    Hi, I'm trying to create a report that creates an opportunity detail report. My Web applet works. The only issue is the HTML tags show up in the email, instead of formatting the narrative fields. Any input is appreciated. Regards, Jo Ann

  • VBScript input parameters

    Hi there everyone I need to deploy printers on a windows 7 machine through Group Policy. In the past a GPO has run a .vbs script that installs the printer including it's name, port, drivers (with location on server of those drivers). Recently it seem

  • How to use the Packager for iPhone ?

    Hi What`s up guys?? I Download the Packager for iPhone and i can`t used this ? please help me and show me in picture how to use this.

  • Message type and IDoc type to update a sales order

    I can create sales order at WE19 by the message type SALESORDER_CREATEFROMDAT2 and IDoc type SALESORDER_CREATEFROMDAT202 . Can I use the same message type and IDoc type to update a sales document (sales order) and also how can I do (I mean, which str