Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › I need some help with a java program
New Posts  All Forums:Forum Nav:

I need some help with a java program

post #1 of 2
Thread Starter 
Alrite well this what I have for now, but i'm not sure from here how to get it finished. Could anybody help me out? Here's the description:

Write a program for that allows a person to have 2 tries to guess the day of the week picked by the computer.

Description of Solution
(main method)
declare variable for computer's choice of weekday as a String(do not initialize it yet)
declare variables for person's guesses of weekday as Strings (do Not initialize them yet)

instantiate Random number generator (Random object)

initialize an integer to a random number between 1-7 (using Random generator)

use switch statement to set computer's choice of weekday (String), based on random integer

prompt and input person's first guess and store it in String declared at the top (use Scanner)

prompt and input person's second guess and store it in String declared a the top (use Scanner)

print out person's guesses

Here's my code:
import java.util.*;
import java.util.Scanner;

public class DayGuessing
{
//-----------------------------------------------------------------
// Plays a simple guessing game with the user.
//-----------------------------------------------------------------
public static void main (String[] args)
{
final int MAX = 7;
int answer, guess;

Scanner scan = new Scanner (System.in);
Random generator = new Random();

answer = generator.nextInt(MAX) + 1;

System.out.println ("Can you guess the weekday picked by the computer? "
+ "\
Please type out your guesses in the prompts below. ");

System.out.print ("\
Enter your first guess: ");
System.out.print ("\
Enter your second guess: ");

guess = scan.nextInt();

if (guess == answer)
System.out.println ("You got it! Good guessing!");
else
{
System.out.println ("That is not correct, sorry.");
System.out.println ("The number was " + answer);
}
}
}
post #2 of 2
Here is some code that should do the program. Feel free to change it around, and ask me to explain if needed

Code:
import java.util.*;
public class RandomNumber {
    public static void main(String[] args){
        Random rand = new Random();
        Scanner scan = new Scanner(System.in);
        int Weekday = rand.nextInt(7)+1;
        String day = "";
        switch(Weekday) {
            case 1: day = "monday"; break;
            case 2: day = "tuesday"; break;
            case 3: day = "wednesday"; break;
            case 4: day = "thursday"; break;
            case 5: day = "friday"; break;
            case 6: day = "saturday"; break;
            case 7: day = "sunday"; break;
            default: System.out.print("That is not a valid day"); break;
        }
        System.out.print("Enter your first guess: ");
        String guess1 = scan.nextLine();
        if(guess1.equalsIgnoreCase(day))
            System.out.print("You got it on the first guess!");
        else {
            System.out.print("Enter your second guess: ");
            String guess2 = scan.nextLine();
            if(guess2.equalsIgnoreCase(day))
                System.out.print("You got it on the second guess!");
            else {
                System.out.println("That is not correct, sorry!");
                System.out.println("The day was: "+day);
            }
        }
    }
}
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Pentium 4 HT 630 Stock 3.0GHz @ 3.83GHz Soltek Pro Series ATI Radeon 9700 Pro 1 GB DDR 
Hard DriveOptical DriveOSMonitor
Stock 120 GB HD LG DVD SuperMulti Windows XP Pro ViewSonic VP201s 
KeyboardPowerCaseMouse
Logitech Wave Keyboard Enermax P4 Stock PSU Stock Case Logitech Wave Mouse 
Mouse Pad
Map of London Underground 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Pentium 4 HT 630 Stock 3.0GHz @ 3.83GHz Soltek Pro Series ATI Radeon 9700 Pro 1 GB DDR 
Hard DriveOptical DriveOSMonitor
Stock 120 GB HD LG DVD SuperMulti Windows XP Pro ViewSonic VP201s 
KeyboardPowerCaseMouse
Logitech Wave Keyboard Enermax P4 Stock PSU Stock Case Logitech Wave Mouse 
Mouse Pad
Map of London Underground 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › I need some help with a java program