Results 1 to 13 of 13

Thread: serious problem

  1. #1
    cmizer is offline Member
    Join Date
    Dec 2008
    Posts
    19
    Rep Power
    0

    Default serious problem

    hey i need this program to do the checks, (starting at if (win>race) ) ask to reenter if the value is bad, move on to the next thing if its good or ask for re-entry again if its bad then move on to the next thing. After all this and printing whatever stats it needs to ask whether new data shud be entered via 1 or 2 for yes or no.

    Plz help me ive been doin this all day and im stuck

    Java Code:
     import java.util.Scanner;
    import java.io.*;
    
    public class Horse7
    {
    	public static void main (String[] args) throws IOException
    	{
    
    		Scanner sc=new Scanner(System.in);
    		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    
    
    		int horseId=0;
    		String horsename=" ";
    		int choice=0;
    		int year=0;
    		int race=0;
    		int win=0;
    		int place=0;
    		int show=0;
    		int horseno=0;
    		int racetotal=0;
    		int wint=0;
    		int placet=0;
    		int showt=0;
    		boolean i=true;
    		boolean nor=false;    
    		boolean nor2=false;    
    		boolean nor3=false;    
    		boolean o=false;
    
    
    
    		 while (o=true)
    		    {
    		
    			 
    			System.out.print("Please enter the horse ID number:\n");
    		    horseId=sc.nextInt();
    
                do
                {
    		    if (horseId>=20000&&horseId<=90000)    
    		    {
    		    i=false;
    		    break;
    		    }
    		    else
    		    {
    		     i=true;
    		      System.out.println("Invalid entry.Please enter the horse ID number:\n");
    		      horseId=sc.nextInt();
    		      if (horseId>=20000&&horseId<=90000)    
    			  i=false;
    		     
    		     
    		    }
                }while (i=true);
    
    
                
    		    
    		    
    			System.out.println("Please enter the horse`s name: ");
    			horsename=br.readLine();
    
    			System.out.println("Please enter " +horsename + "`s year of birth: ");
    			year=sc.nextInt();
    
    			System.out.println("Please enter the number of races " + horsename + " has entered: ");
    			race=sc.nextInt();    
    
    
    			System.out.println("Please enter the number of races " +horsename + " has won: "); 
    			win=sc.nextInt();
    
    		
    			//do
    			if (win>race)
    			{
    				nor=true;
    				System.out.println("Invalid entry. Enter again ");
    				win=sc.nextInt();
    			}
    			else
    			{
                    
    				System.out.println("Please enter the number of races in which " +horsename + " has placed: ");
    				place=sc.nextInt();
                 
    			//} while (nor=true);
    
    				if ((place>race)||(place+win>race))
    				{
    					nor2=true;
    					System.out.println("Invalid entry. Enter again ");
    					place=sc.nextInt();
    
    				}
    				else
    				{
    					System.out.println("Please enter the number of races in which " +horsename + " has shown: ");
    					show=sc.nextInt();
    
    
    
    					if ((show>race)||(place+win+show>race))
    					{
    						nor3=true;
    						System.out.println("Invalid entry. Enter again ");
    						show=sc.nextInt();
    					}
    					else
    					{
    
    
    						double winf =((double)(win)/race);
    						double placef =((double)(place)/race);
    						double showf = ((double)(show)/race) ;
    
    						int winp = (int)(winf*100);
    						int placep = (int)(placef*100);
    						int showp = (int)(showf*100);
    
    						System.out.println("\nHorse ID: " + horseId + "\nHorse name: " + horsename + "\nHorse birthday: " + year + "\nNumber of races entered: " + race +          
    								"\nNumber of times horse won: " + win +"\nNumber of times horse placed: " + place + "\nNumber of times horse shown: " + show );
    						System.out.println("\n" + horsename + " has won " + winp + "% of his races, has placed " + placep + "% and has shown " + showp + "%.") ; 
    
    						horseno++;
    						racetotal+=race;
    						wint+=win;
    						placet+=place;
    						showt+=show;
    						System.out.println("again? Y =1 or N =2");
    						choice=sc.nextInt();
    						if (choice==1)
    						{
    							o=true;
    						}
    						else
    							System.out.println("You have entered a total of " +horseno +" horses which have taken part in a total of " +racetotal +" races. These horses won a total of " +wint + " placed in a total of " + placet + " and shown in a total of " +showt + " .");
    
    
    					}
    
    				}
    			}
    		}
    	}
    }
    Last edited by cmizer; 01-05-2009 at 05:24 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    cmizer is offline Member
    Join Date
    Dec 2008
    Posts
    19
    Rep Power
    0

    Default

    from here i guess. i need this program to do the checks, (starting at if (win>race) ) ask to reenter if the value is bad, move on to the next thing if its good or ask for re-entry again if its bad then move on to the next thing. After all this and printing whatever stats it needs to ask whether new data shud be entered via 1 or 2 for yes or no. I think it`ll be necessary to read onwards from what i highlighted here....ignore the //do comment i was just trying sometin


    Java Code:
    System.out.println("Please enter the number of races " +horsename + " has won: "); 
    			win=sc.nextInt();
    
    		
    			//do
    			if (win>race)
    			{

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default uh... why... ?

    Just out of curiousity... why is the title of your post "Serious Problem" ?

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    cmizer is offline Member
    Join Date
    Dec 2008
    Posts
    19
    Rep Power
    0

    Default

    i dunno to me its a serious problem....

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    cmizer is offline Member
    Join Date
    Dec 2008
    Posts
    19
    Rep Power
    0

    Default

    i tried using a do while but it just messes everything up...can you show me how to fix it? like i out do above if(win>race) and while somewhere at the bottom after it prints the statistics. But it messes up if i enter a bad value, meaning a value for win which is more than race and so on for the other two checks

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  9. #9
    cmizer is offline Member
    Join Date
    Dec 2008
    Posts
    19
    Rep Power
    0

    Default

    i think this is it, in this though if a bad value is entered at the second check it makes u re-enter values twice, meaning it says invalid entry enter again then u enter and it asks for that same entery again, please enter number of times placed or shown, (it does this for both place and shown checks)

    Java Code:
     import java.util.Scanner;
    import java.io.*;
    
    public class Horse7
    {
    	public static void main (String[] args) throws IOException
    	{
    
    		Scanner sc=new Scanner(System.in);
    		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    
    
    		int horseId=0;
    		String horsename=" ";
    		int choice=0;
    		int year=0;
    		int race=0;
    		int win=0;
    		int place=0;
    		int show=0;
    		int horseno=0;
    		int racetotal=0;
    		int wint=0;
    		int placet=0;
    		int showt=0;
    		boolean i=true;
    		boolean nor=true;    
    		boolean nor2=true;    
    		boolean nor3=true;    
    		boolean o=false;
    
    
    
    		while (o=true)
    		{
    
    
    			System.out.print("Please enter the horse ID number:\n");
    			horseId=sc.nextInt();
    
    			do
    			{
    				if (horseId>=20000&&horseId<=90000)    
    				{
    					i=false;
    					break;
    				}
    				else
    				{
    					i=true;
    					System.out.println("Invalid entry.Please enter the horse ID number:\n");
    					horseId=sc.nextInt();
    					if (horseId>=20000&&horseId<=90000)    
    						i=false;
    
    
    				}
    			}while (i=true);
    
    
    
    
    
    			System.out.println("Please enter the horse`s name: ");
    			horsename=br.readLine();
    
    			System.out.println("Please enter " +horsename + "`s year of birth: ");
    			year=sc.nextInt();
    
    			System.out.println("Please enter the number of races " + horsename + " has entered: ");
    			race=sc.nextInt();    
    
    
    			System.out.println("Please enter the number of races " +horsename + " has won: "); 
    			win=sc.nextInt();
    
    
    			do
    			{
    				if (win>race)
    				{
    					
    					System.out.println("Invalid entry. Enter again ");
    					win=sc.nextInt();
    				}
    				else if(win<race)
    					nor=true;
    					System.out.println("Please enter the number of races in which " +horsename + " has placed: ");
    					place=sc.nextInt();
    
    
    					if ((place>race)||(place+win>race))
    					{
    						
    						System.out.println("Invalid entry. Enter again ");
    						place=sc.nextInt();
    
    					}
    					else
    					{
    						nor2=true;
    						System.out.println("Please enter the number of races in which " +horsename + " has shown: ");
    						show=sc.nextInt();
    
    
    
    						if ((show>race)||(place+win+show>race))
    						{
    							
    							System.out.println("Invalid entry. Enter again ");
    							show=sc.nextInt();
    						}
    						else
    						{
    							nor3=true;
    							double winf =((double)(win)/race);
    							double placef =((double)(place)/race);
    							double showf = ((double)(show)/race) ;
    
    							int winp = (int)(winf*100);
    							int placep = (int)(placef*100);
    							int showp = (int)(showf*100);
    
    							System.out.println("" + nor + nor2 + nor3);
    
    							System.out.println("\nHorse ID: " + horseId + "\nHorse name: " + horsename + "\nHorse birthday: " + year + "\nNumber of races entered: " + race +          
    									"\nNumber of times horse won: " + win +"\nNumber of times horse placed: " + place + "\nNumber of times horse shown: " + show );
    							System.out.println("\n" + horsename + " has won " + winp + "% of his races, has placed " + placep + "% and has shown " + showp + "%.") ; 
    							break;
    						}
    						
    					}
    					
    			}
    			while(nor==true && nor2==true && nor3==true);
    			horseno++;
    			racetotal+=race;
    			wint+=win;
    			placet+=place;
    			showt+=show;
    			System.out.println("again? Y =1 or N =2");
    			choice=sc.nextInt();
    			if (choice==1)
    			{
    				o=true;
    			}
    			else if(choice == 2)
    			{
    				System.out.println("You have entered a total of " +horseno +" horses which have taken part in a total of " +racetotal +" races. These horses won a total of " +wint + " placed in a total of " + placet + " and shown in a total of " +showt + " .");
    				System.exit(0);
    			}
    
    		}
    
    
    	
    	
    	}
    }

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You want to do the same thing again and again on this condition.

    Java Code:
    				if (win>race)
    				{
    					
    					System.out.println("Invalid entry. Enter again ");
    					win=sc.nextInt();
    				}
    Ok then, where/how you have loop this? I've seen that in other if-else condition statements you've use some boolean values.

  11. #11
    cmizer is offline Member
    Join Date
    Dec 2008
    Posts
    19
    Rep Power
    0

    Default

    ive placed all of that in a do while loop starting just above there and ending here

    Java Code:
     }
    			while(nor==true && nor2==true && nor3==true);
    			horseno++;
    i want it to keep asking until it gets a good value for win show and place, meaning this if ((place>race)||(place+win>race)) and the other one following it as well

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    That's what I'm talking. In that condition did you workout anything to keep within the loop. Think about that, if win>race is that effected to the loop continueous?

  13. #13
    cmizer is offline Member
    Join Date
    Dec 2008
    Posts
    19
    Rep Power
    0

    Default

    well i managed to fix the win>race and the ((place>race)||(place+win>race)) now its the third part that gives me trouble. if a bad value is enetered it asks for it twice an doesnt continue on wit the main while loop to ask if any more data shud be enetered wit 1 or 2. heres the code

    Java Code:
     import java.util.Scanner;
    import java.io.*;
    
    public class Horse7
    {
    	public static void main (String[] args) throws IOException
    	{
    
    		Scanner sc=new Scanner(System.in);
    		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    
    
    		int horseId=0;
    		String horsename=" ";
    		int choice=0;
    		int year=0;
    		int race=0;
    		int win=0;
    		int place=0;
    		int show=0;
    		int horseno=0;
    		int racetotal=0;
    		int wint=0;
    		int placet=0;
    		int showt=0;
    		boolean i=true;
    		boolean nor=true;    
    		boolean nor2=true;    
    		boolean nor3=true;    
    		boolean o=false;
    
    
    
    		while (o=true)
    		{
    
    
    			System.out.print("Please enter the horse ID number:\n");
    			horseId=sc.nextInt();
    
    			do
    			{
    				if (horseId>=20000&&horseId<=90000)    
    				{
    					i=false;
    					break;
    				}
    				else
    				{
    					i=true;
    					System.out.println("Invalid entry.Please enter the horse ID number:\n");
    					horseId=sc.nextInt();
    					if (horseId>=20000&&horseId<=90000)    
    						i=false;
    
    
    				}
    			}while (i=true);
    
    
    
    
    
    			System.out.println("Please enter the horse`s name: ");
    			horsename=br.readLine();
    
    			System.out.println("Please enter " +horsename + "`s year of birth: ");
    			year=sc.nextInt();
    
    			System.out.println("Please enter the number of races " + horsename + " has entered: ");
    			race=sc.nextInt();    
    
    
    			System.out.println("Please enter the number of races " +horsename + " has won: "); 
    			win=sc.nextInt();
    
    
    			//do
    			while(nor=true)
    			{
    				if (win>race)
    				{
    
    					System.out.println("Invalid entry. Enter again ");
    					win=sc.nextInt();
    				}
    				else if(win<race)
    					nor=false;
    				System.out.println("Please enter the number of races in which " +horsename + " has placed: ");
    				place=sc.nextInt();
    
    				while(nor2=true)
    				{
    
    					if ((place>race)||(place+win>race))
    					{
    
    						System.out.println("Invalid entry. Enter again ");
    						place=sc.nextInt();
    
    					}
    					else
    					{
    						if ((place>race)||(place+win>race))
    							nor2=false;
    						System.out.println("Please enter the number of races in which " +horsename + " has shown: ");
    						show=sc.nextInt();
    
    						//while(nor3=true)
    							//{
    
    							if ((show>race)||(place+win+show>race))
    							{
    
    								System.out.println("Invalid entry. Enter again ");
    								show=sc.nextInt();
    							}
    							else
    							{
    								//if ((show>race)||(place+win+show>race))
    								nor3=false;
    								double winf =((double)(win)/race);
    								double placef =((double)(place)/race);
    								double showf = ((double)(show)/race) ;
    
    								int winp = (int)(winf*100);
    								int placep = (int)(placef*100);
    								int showp = (int)(showf*100);
    
    								System.out.println("\nHorse ID: " + horseId + "\nHorse name: " + horsename + "\nHorse birthday: " + year + "\nNumber of races entered: " + race +          
    										"\nNumber of times horse won: " + win +"\nNumber of times horse placed: " + place + "\nNumber of times horse shown: " + show );
    								System.out.println("\n" + horsename + " has won " + winp + "% of his races, has placed " + placep + "% and has shown " + showp + "%.") ; 
    								//break;
    							}
    
    							}
    
    					}
    					//while(nor==true && nor2==true && nor3==true);
    					horseno++;
    					racetotal+=race;
    					wint+=win;
    					placet+=place;
    					showt+=show;
    					System.out.println("again? Y =1 or N =2");
    					choice=sc.nextInt();
    					if (choice==1)
    					{
    						o=true;
    					}
    					else if(choice == 2)
    					{
    						System.out.println("You have entered a total of " +horseno +" horses which have taken part in a total of " +racetotal +" races. These horses won a total of " +wint + " races, placed in a total of " + placet + " and shown in a total of " +showt + " .");
    						System.exit(0);
    					}
    
    				}
    
    
    
    			}
    			}
    		}
    	//}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •