Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Java Help

Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-28-2007, 10:17 AM
A.N.H A.N.H is offline
Newbie
 
Join Date: Apr 2007
Posts: 8
Rep Power: 0
A.N.H is on a distinguished road
Exclamation Problem with an exercise??

Hey all, how are u!!?

While reading my book and answering some exercises i got stuck in this exercise, it kinda easy i just got stuck in a small part. anyways here is the question, for u guys who have Java How to program ( Deitel) its page 282 question 6.29

it basicly says that i have to stimulate a coin tossing app, that in the end calculates the average times the coin landed on its head and on its tail.

here is my code

Code:
import java.util.Scanner;
import java.util.Random;;


public class TossCoin 
{
	int NumTails = 0;
	int NumHeads = 0;
	int AverageHeads;
	int AverageTails;
	int Sum = NumTails + NumHeads;
	
	private double CalculateAverage( int x, int y)
	{
		return ( x / y);
	}
	
	private void ToToss()
	{
		Random randomnum = new Random();
		
		if ( randomnum.nextInt(1) == 0)
		{
			NumTails++;
		}
		else
		{
			NumHeads++;
		}
	}
	
	public void Tossed()
	{
		
		
		System.out.println("to stop tossing press <ctrl> z otherwise press any key");
		Scanner input = new Scanner ( System. in );
		
		
		if ( input.hasNext())
		{
			ToToss();
		}
		else
		{
			if ( Sum == 0)
			{
				System.out.println("Average is zero");
			}
			
			else
			{
			System.out.println("Average of Tails is");
			CalculateAverage( NumTails, Sum);
			System.out.println("Average of Heads is");
			CalculateAverage( NumHeads, Sum);
			}
		}
	}
}
now the problem is when i try to run it, and i press any key the program stops, but it is supposed to contenuie and stop only after i press ctrl and z, can someone help me please.

here is the main method if u wants it.


Code:
public class TossCoinTest 
{
	public static void main(String args[])
	{
		TossCoin tossed = new TossCoin();
		tossed.Tossed();
		
	}

}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-28-2007, 02:36 PM
icepack's Avatar   
icepack icepack is offline
Programmer
 
Join Date: Jul 2006
Location: North Carolina
Posts: 115
Rep Power: 9
icepack is on a distinguished road
Send a message via AIM to icepack
Default

What would happen if you just removed the input scanner? i only skimmed through it, but do you ever take input from the keyboard?

ctrl z will stop whatever program from running regardless of whether or not the program is taking input.(at least in UNIX)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-29-2007, 02:01 PM
A.N.H A.N.H is offline
Newbie
 
Join Date: Apr 2007
Posts: 8
Rep Power: 0
A.N.H is on a distinguished road
Default

nvm i fixed the problem


i changed some of the code but it works now, plus the code is more elegant, i'll post the new code later.

thanks ice
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Peculiar UI Problem Needs Tackling adriyel C# Programming 2 04-06-2008 08:46 AM
i have a problem please help me!!!???? stack Java Help 8 09-22-2007 04:17 PM
[C] Comparison problem Alhazred C and C++ 1 08-29-2007 05:58 AM
Download Problem meimei3936 ionFiles 1 06-30-2007 05:00 PM
A small problem in the output The_Master C and C++ 3 12-13-2006 01:04 PM


All times are GMT -5. The time now is 11:36 PM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads