Results 1 to 13 of 13
Thread: serious problem
- 01-05-2009, 05:21 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 19
- Rep Power
- 0
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.
- 01-05-2009, 06:15 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you clearly tell me where you stuck with? Checking such a long code is really mess.
- 01-05-2009, 06:21 AM #3
Member
- Join Date
- Dec 2008
- Posts
- 19
- Rep Power
- 0
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) {
- 01-05-2009, 06:28 AM #4
uh... why... ?
Just out of curiousity... why is the title of your post "Serious Problem" ?
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-05-2009, 06:52 AM #5
Member
- Join Date
- Dec 2008
- Posts
- 19
- Rep Power
- 0
i dunno to me its a serious problem....
- 01-05-2009, 07:17 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 01-05-2009, 07:21 AM #7
Member
- Join Date
- Dec 2008
- Posts
- 19
- Rep Power
- 0
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
- 01-05-2009, 07:24 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you show what you have done?
- 01-05-2009, 07:32 AM #9
Member
- Join Date
- Dec 2008
- Posts
- 19
- Rep Power
- 0
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); } } } }
- 01-05-2009, 07:45 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You want to do the same thing again and again on this condition.
Ok then, where/how you have loop this? I've seen that in other if-else condition statements you've use some boolean values.Java Code:if (win>race) { System.out.println("Invalid entry. Enter again "); win=sc.nextInt(); }
- 01-05-2009, 07:49 AM #11
Member
- Join Date
- Dec 2008
- Posts
- 19
- Rep Power
- 0
ive placed all of that in a do while loop starting just above there and ending here
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 wellJava Code:} while(nor==true && nor2==true && nor3==true); horseno++;
- 01-05-2009, 08:02 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
- 01-05-2009, 08:09 AM #13
Member
- Join Date
- Dec 2008
- Posts
- 19
- Rep Power
- 0
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); } } } } } //}


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks