Java number game confused help any1!

The following simple game is played between a player and the computer. At the
start the player enters either the number one or two on the keyboard (the computer will also
choose either one or two at random). Then the computer draws two numbers between one and
one hundred at random. The human player wins if the number in position corresponding to the
number he selected is bigger than the number in position corresponding to the number chosen by
the computer. If the two numbers are the same (either because the player and the computer chose
the same value or because the random numbers in the chosen positions are the same) the game
ends in a draw. The code doen't work properly it etiher comes up with computer wins and draw at the same time! Also i need to display the numbers that are generated randomly can anyone help my code:
import java.lang.Object;
import java.lang.System;
import java.io.InputStream;
import java.util.Scanner;
class javataex1 {
public static void main(String[] args) {
// declarations
Scanner input = new Scanner (System.in);
int human ;
int computer;
int Number1;
int Number2;
//instructions
Number1=(int) Math.ceil(100*Math.random());
Number2=(int) Math.ceil(100*Math.random());
computer=(int) Math.ceil(2*Math.random());
System.out.print("Enter number 1 or 2: ");
human = (input.nextInt());
if (human!=1 && human!=2)
System.out.println("you entered wrong number");
else
if (human==computer)
System.out.print("Draw");
else
human=Number1;
computer=Number2;
if (human==computer)
System.out.print("Draw");
else
if (human>computer)
System.out.print("Human wins");
else
if (computer>human)
System.out.print("Computer wins");
}

Crosspost
http://forum.java.sun.com/thread.jspa?threadID=5245168&tstart=0
Only post on one forum at a time, please. I think someone answered you in the other thread anyway.
Illu

Similar Messages

  • Number game

    please help with this, if you can...I need to write a JAVA program to implement the secret number game. the program should create and store a random number between 1 and 10, inclusive. It should then prompt the user to guess the number. accepting user input, it should determine whether or not the guess was correct. If correct, it should print a contratulatory message and terminate. If the guess is incorrect, the program shoulod display a hint indicating that the guress was either too low ot too high. The program must validate user input. If the user enters a non-integer, it should be caught and output. If the user enters a guess outside of the valid range, an appropriate error message should be displayed either way, the user is then reprompted for a new guess.

    hi, i am giving you the code, with the specifications you have asked for, check for them, by running the code, and it will look nice if you implement this idea, to transform this code to an user interface using applets.
    import java.io.*;
    import java.util.Random;
    public class numberGame {
         public static void main (String args []) {
              boolean myBoolean = true;
              Random rand = new Random();
              int k = rand.nextInt(11);
              do{
                   try {
              BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
              System.out.println("Hi, Welcome to the secret game");
              System.out.println("Guess The Number");
              String str="";
              str = br.readLine();
              str.trim();
              System.out.println("you have entered this "+ str);
              int i =0;
              i = new Integer(str).intValue();
              numberGamePerform(i,k,myBoolean);
         }catch(Exception e){
              System.out.println("Error");
         } while (myBoolean!=false);
         public static void numberGamePerform(int i,int k,boolean myBoolean) {
              //System.out.println("My Guess "+k);
              if( i == k ) {
              System.out.println("Good Guess, Congratulations");
              myBoolean = false;
              System.exit(0);
              else {
                   System.out.println("Back Luck , Give it another Try");
                   myBoolean = true;
                   if(i<=2 && k>=8)
                   System.out.println("Input Guess too low");
                   else if (i >= 8 && k <= 2)
                   System.out.println("Input Guess too high");
                   else {
                        if((k>2&&k<=4)&&(i>=5&&i<8))
                        System.out.println("Guess relatively close but lies in the other half");
                        else
                        System.out.println("very close");
                   //if(i)

  • Is there a Java utility class to help with data management in a desktop UI?

    Is there a Java utility class to help with data management in a desktop UI?
    I am writing a UI to configure a network device that will be connected to the serial port of the computer while it is being configured. There is no web server or database for my application. The UI has a large number of fields (50+) spread across 16 tabs. I will write the UI in Java FX. It should run inside the browser when launched, and issue commands to the network device through the serial port. A UI has several input fields spread across tabs and one single Submit button. If a field is edited, and the submit button clicked, it issues a command and sends the new datum to the device, retrieves current value and any errors. so if input field has bad data, it is indicated for example, the field has a red border.
    Is there a standard design pattern or Java utility class to accomplish the frequently encountered, 'generic' parts of this scenario? lazy loading, submitting only what fields changed, displaying what fields have errors etc. (I dont want to reinvent the wheel if it is already there). Otherwise I can write such a class and share it back here if it is useful.
    someone recommended JGoodies Bindings for Swing - will this work well and in FX?

    Many thanks for the reply.
    In the servlet create an Arraylist and in th efor
    loop put the insances of the csqabean in this
    ArrayList. Exit the for loop and then add the
    ArrayList as an attribute to the session.I am making the use of Vector and did the same thing as u mentioned.I am using scriplets...
    >
    In the jsp retrieve the array list from the session
    and in a for loop step through the ArrayList
    retrieving each CourseSectionQABean and displaying.
    You can do this in a scriptlet but should also check
    out the jstl tags.I am able to remove this problem.Thanks again for the suggestion.
    AS

  • A problem with my Java Scrabble game

    Hello
    I am making a Java Scrabble game for a school assignment and I have a problem with the following:
    The number of players is input by the user (2 to 4) and i need to create a number of arrays based on the number of players. These arrays must have 7 playing pieces at all times until the "bag" of pieces has less pieces than those required to replace the ones a certain player just used (in wich case the bag would give that player all of it's remaining pieces and, from this point on, the player arrays would not necessarily need to have 7 pieces).
    this is my procedure to take a piece from the bag:
         public Piece takePiece()
              if (this.pieceCounter > 0) {
                   Arrays.sort(this.pieces, 0, this.pieceCounter);
                   int i = this.generator.nextInt(this.pieceCounter);
                   Piece p = this.pieces;
                   this.pieceCounter--;
                   this.pieces[i] = this.pieces[this.pieceCounter];
                   return p;
              else
                   return null;
    this is my Piece class:
    public class Piece implements Comparable {
         private int scorePiece;
         private char letterPiece;
         public Piece(char letterPiece, int scorePiece)
              this.scorePiece = scorePiece;
              this.letterPiece = letterPiece;
         public int scorePiece()
              return scorePiece;
         public String letterPiece()
              return letterPiece();
         public String toString()
              return letterPiece+ " " + scorePiece;
         public int compareTo(Object a)
              Piece piece= (Piece) a;
              return (int) letterPiece - (int) piece.letterPiece;
    }Thanks in advance for your help

    Ok, if i create a Players class and do this in Main class:
         if(nPlayers >= 2 && nPlayers <= 4)
              Players arrayPlayers[];
              arrayPlayers= new Players [nPlayers ];
              for(int i=0 ; i < nPlayers ; i++)
                       arrayPlayers[i] = new Players ();
              }My Players Class should contain something like this:
             // Array of 7 that contains Pieces...
         Piece[] pieces= new Piece[7];how can i get pieces from takePiece(); (Class Bag) and put them in the pieces arrays until takePiece(); returns null?

  • Simple java pathfinder game

    Hi
    Is any1 willing to make me a simple java pathfinder game in return for some money? details will be given on request...

    Sod off and do your own homework.

  • Java 3d game

    Hi,
    I hav made first person shooter in java, but a perso with no java sdk can play it. When i put it as an applet in java it doesnt work. Plese help
    How do i paly a java 3d game on a acomputer wiht no sdk?!?

    el--davido wrote:
    an applet that i want to put into a web browser. It contains a .obj file and its saying i dont not have permission to load it when i open it using the browser.
    I jus need it to run on a computer with no sdk, in any form, browser applt or a normal appletWell, have you signed the applet? If not do it.

  • In the previous version, the menu table in table options, there is an option that gives me the option: the Return key moves to next cell. I do not see this option in the new number. can you help me please?

    in the previous version  of Number, the menu table in table options, there is an option that gives me the option: the Return key moves to next cell. I do not see this option in the new number. can you help me please?

    Hi silvano,
    If you use a regular pattern when entering values, press enter (return) after entering the last value in a row. That will take you to the first Body Cell of the next row.
    Start in Cell B2
    1 Tab 2 Tab 3 Enter
    4 Tab 5 Tab 6 Enter
    7 Tab 8 Tab 9 Enter
    Now you are ready to type into B5 .
    Another way that some people find easier is to enter one column at a time
    Start at B2
    1 enter
    4 enter
    7 enter
    etc.
    Now start with C2.
    Use whatever suits your work flow.
    Regards,
    Ian.

  • HT5055 Just updated to lion and my cctv access has stopped functioning all I get is a white screen in the middle of the control panel where the camera shots should be.  I think it is caused by JAVA but am confused as when i view on snow leopard it works 

    Just updated to lion and my cctv access has stopped functioning all I get is a white screen in the middle of the control panel where the camera shots should be.  I think it is caused by JAVA but am confused as when i view on snow leopard it works  can you help

    Open "Java Preferences" either from spotlight or your utilities folder...it's probably going to say you need to install a java runtime. Then just click install!

  • I got an ipod touch from my friend, but can't figure out how to change the apple id to get games. Help?

    I got an ipod touch from my friend, but can't figure out how to change the apple id to get games. Help?

    Never mind, I got this.

  • HT3702 I'm trying to update a game and its telling me I need to view my billing.i check my billing and it isn't giving me a none option. It's forcing me to put a credit card in, in order to update a game. Help please:(

    I'm trying to update a game and its telling me I need to view my billing.i check my billing and it isn't giving me a none option. It's forcing me to put a credit card in, in order to update a game. Help please:(

    Well, that is probably becasue you owe a debt or something similar.
    Check with
    iTunes Store Support
    http://www.apple.com/emea/support/itunes/contact.html

  • I have Adobe Acrobat 9 Pro and the serial number.  It is asking for an older serial number?  Please help?

    I have Adobe Acrobat 9 Pro and the serial number.  It is asking for an older serial number?  Please help?

    Hi juneb55782770,
    I believe that you have an upgrade version of Adobe Acrobat Pro, in this case it will ask for the serial number of the previous qualifying version.
    You can find your old serial number using this link : Find your serial number quickly
    Hope this will resolve your issue.
    Please reply if you still experience any issue.
    Regards,
    Aadesh

  • Document number field F4 help in transaction cv01n

    Hi,
    I would like to not display deleted documents in Document Number field search help in CV01n transaction only.
    How do I go about doing this?
    Regards,
    K

    Hello,
    The search help used for document no is H_TDWA. Add a search help exit to this search help to filter out deleted document numbers. Search sdn for "search help exit" you eill get several links to implement it.
    Regards
    Vishal Kapoor

  • Java.awt.Toolkit - Coldfusion - HELP

    Greetings.
    I am having a hard time with the following code:
    <cfobject action="create" type="java" class="java.awt.Toolkit" name="fileObj">
    <cfscript>
    img = fileObj.getDefaultToolkit().getImage("images/products/39321.jpg");
    width = img.getwidth();
    height = img.getheight();
    </cfscript>
    <cfoutput>#height#</cfoutput>
    I keep getting the following error:
    java.lang.NoClassDefFoundError
    My version of Java is 1.4.2. I'm running Redhat Linux 4.0. Coldfusion MX 6.1.
    I know this isn't a Coldfusion forum, but I really need some help here.
    I'm new to Java technologies, so any help will be greatly appreciated.

    can ColdFusion load Java objects?
    Presuming that it can, then ColdFusion probably needs some configuration to tell it the Java directory or where the class files are (JavaHome/jre/lib/rt.jar)

  • Invalid Number Error Please help Urgent

    Hi,
    I am trying to create a report in discoverer where in I get an invalid number error for the statement
    ,DECODE(final.TYP,'Sales',to_char(final.future_supplpy),'B/O','B/O') sale_type.
    We have the requirement that when TYP is Sales i need to display future supply and when the TYP is B/O i need to display B/O.
    The query runs perfectly fine in TOAD but in Discoverer Desktop when i run the report i get an error message
    invalid number.
    Please help its urgent
    Thanks
    Ashwini

    Hi Ashwani
    First of all, your statement appears to not be handling anything other than Sales or B/O. Is that deliberate? If not, you need a default, catch all value at the end like this:
    DECODE(final.TYP,'Sales',NVL(final.future_supply,to_char(final.future_supply),'B/O','B/O', default_value) sale_type
    You also might want to make sure there aren't any NULL values in future_supply or in TYP and if there are you might want to consider using an NVL for these, like this:
    DECODE(final.TYP,'Sales',NVL(to_char(final.future_supply,NULL),'B/O','B/O', default_value) sale_type
    Also, when using DECODE you don't always need to use TO_CHAR as the DECODE conversion to a string typically takes place automatically. CASE is different as it insists that all results are of the same data type.
    Hope this helps
    Best wishes
    Michael

  • There is no rt.jar in java lib could anyone help me downloading it ,or tell me where is this rt.jar, there is no rt.jar in java lib could anyone help me downloading it ,or tell me where is this rt.jar

    there is no rt.jar in java lib could anyone help me downloading it ,or tell me where is this rt.jar, there is no rt.jar in java lib could anyone help me downloading it ,or tell me where is this rt.jar

    thx for your suggestion ..but i did try to skip my code..but i am sure if u guy understand what i am trying to ask ..so i try to show my all program for u guy better undrstand my problem ...the first three paragraph of code i hope u guy could focus on it ....i try ask the user to input the cd`s title to our insert list by using a function call readString( ) and i use this method to ask the user what cd they want to be delete from the list ...but it doesn`t work when i try to perform the delete cd funtion. At first, i think it should be my deleteCd( ) problem ..but i do some testing on it..it works fine if i pass a String directly to my function instead of using readString() function to perform ...therefore, i am sure my delete function working fine....i am thinking if it is my readString() problem make my deletion does not perform well...thx for u guy help

Maybe you are looking for