You are here:

# Java/Homework

Question

Assignment
Hey, I was writing this code for my assignment i have done as much i know how to do it but after that i dont know what to do

import java.text.DecimalFormat;
import java.util.Locale;
import java.util.Scanner;
import java.text.*;
import java.io.*;

class CoinToes
{
public static void main(String args[]) throws IOException
{
float numtails = 0;
float numcointosses = 0;
float correct = 0;
float incorrect = 0;

int toss, coinguess, randomguess;

Decimalformat pf2 = new Decimalformat ("###.0##");
Scanner input = new Scanner(System.in);
{
toss = (int)(2*Math.random())+1;

System.out.println("Will the coin toss come up Heads or Tails ? (\"H \"for heads and \"T for tails): ");
guess = input.nextline();
guess = guess.tolowerCase();
guess = guess.substring(0,1);

if( guess.equals("h"))
{
System.out.println("You guessed the coin will come up \"Heads\"");
coinguess = 1;
}
else if(guess.equals("t"))
{
System.out.println("You guessed the coin will come up \"Tails\"");
coinguess = 2;
}
else
{
System.out.println("You enter \"t guess t\" which is NOt \"T\" or \"H\"");
System.out.println("Randomly generating a guess for you ! ");
if(randomguess == 1)
{
coinguess = 1;
}
else
{
System.out.println("Random Guess is Tails");
coinguess = 2;
}
}
if (toss == 1)
{
}
else
{
numtails +=1;
System.out.println("coins comes up \"Tails\"");
}
numcointoses +=1;

System.out.print(outputlisting);

System.out.print("\nDo You wish to make another calculation (Y or N): ");

// Take only the first position for a Y or N

}

I have attached the assignment page too

This is mostly correct. You should run this though a compiler to have it show you what compiler errors you have. E,g, you have answer declared twice.

You still need to tell the user if the guess was correct or not. This is as simple as:
if(coinguess == toss)
{
//Correct guess code
}
else
{
//Incorrect guess code
}

Just tell the user that they guessed right/wrong, and increment the correct/incorrect.

You also need to display final stats (after the while loop)
To compute percentage, simply take correct/numcointosses * 100. Then use pf2 to format the number so you can show it to the user.

Java

Volunteer

#### Artemus Harper

##### Expertise

I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.

##### Experience

I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Organizations
Washington State University

Education/Credentials
MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.