| Author |
Message |
|
cesponj
|
Post subject: hi to all.... Posted: Fri Jul 30, 2010 7:48 am |
|
 |
| Pro Member |
 |
|
|
Joined: Thu Jun 24, 2010 7:35 am Posts: 45
|
|
i just have a question were can i put a code for this one that it will stop the program if i input a negative value
import java.io.*; import java.util.*;
public class pre_Exam { public static void main(String[] args) throws Exception { while(true) { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); float num;
System.out.print("how many input # of student:"); num=Float.parseFloat(input.readLine());
float [] stud=new float[50];
int x; for(x=0; x<num; x++) { System.out.print("Score of the student");System.out.print(x+1+":"); stud[x]=Float.parseFloat(input.readLine()); } Accept_score(stud,num); } } public static void Accept_score(float[] array1, float number) { float total=0; char grade='\u0000'; float score=100; float ave=0; int x; for(x=0;x<number;x++) { total+=array1[x]; ave=total/number; } for(x=0; x<score; x++) { if(score < ave - 10) { grade='F';
} if(score > ave + 10) { grade='C';
}
if(score > ave+11) { grade='A';
} } System.out.println("student #\tscore\t\tgrade"); for(x=0;x<number;x++) { System.out.println(+x+1+"\t\t"+array1[x]+"\t\t"+grade);
} Display_score(ave,total); } public static void Display_score(float ave, float total) { System.out.print("The total:"+total); System.out.print("And average is:"+ave);
} }
|
|
 |
|
 |
|
salboaski
|
Post subject: Re: hi to all.... Posted: Fri Jul 30, 2010 8:02 am |
|
 |
| n00b |
 |
|
|
Joined: Tue Jul 27, 2010 1:02 pm Posts: 4
|
|
Your code is never going to stop as long as you are using the "while(true)" statement.
As far as telling the program to stop if a negative value is entered, just add in an if statement before you make your call to the accept score method. Make it look like:
if (!num<0){ Accept_score(stud, num); } else{ System.out.println("Please enter a valid score."); }
|
|
 |
|
 |
|
Holiverh
|
Post subject: Re: hi to all.... Posted: Fri Jul 30, 2010 8:19 am |
|
 |
| Expert Poster |
 |
|
|
Joined: Wed Jul 28, 2010 1:01 am Posts: 127 Location: East Anglia, UK
|
Salboaski wrote: Your code is never going to stop as long as you are using the "while(true)" statement. Couldn't you just use a break statement inside a conditional block? This should cause it to break out of the loop and thus terminate. Sorry if this is completely incorrect; no spreken ze Java. And can i reccomend you palce all code in the appropriate tags.
_________________ Salus populi suprema lex esto.
|
|
 |
|
 |
|
cesponj
|
Post subject: Re: hi to all.... Posted: Fri Jul 30, 2010 8:33 am |
|
 |
| Pro Member |
 |
|
|
Joined: Thu Jun 24, 2010 7:35 am Posts: 45
|
|
no i mean it will stop asking the score of the student if i will input negative number
|
|
 |
|
 |
|
salboaski
|
Post subject: Re: hi to all.... Posted: Fri Jul 30, 2010 9:26 am |
|
 |
| n00b |
 |
|
|
Joined: Tue Jul 27, 2010 1:02 pm Posts: 4
|
Holiverh wrote: Salboaski wrote: Your code is never going to stop as long as you are using the "while(true)" statement. Couldn't you just use a break statement inside a conditional block? This should cause it to break out of the loop and thus terminate. Sorry if this is completely incorrect; no spreken ze Java. And can i reccomend you palce all code in the appropriate tags.You can use a break line to quit a while statement, but looking at his code, he doesn't have a break line in there. Also, it's a bit unconventional with a while loop, as it makes it a little harder to review the code. I've found that typically break lines are used to stop for loops after its iterated through an array where your code is looking for a specific value at a specific index. And thanks for the info with the code tags. It's been a while since I've been active on any forums. Usually i'm just a leech  Anyway Cespon, in response to your second message, we can employ Holiverh's suggestion to escape from your for loop. Place an if statement immediatly after your input buffer: Code: for(x=0; x<num; x++) { System.out.print("Score of the student");System.out.print(x+1+":"); stud[x]=Float.parseFloat(input.readLine()); if(stud[x]<0){ //This break line will end the current for loop break; } }
Your program will stop asking you for the score of your students, and continue in your while loop to the Accept Score method, and then repeat again from the top.
|
|
 |
|
 |
|
yasuodancez
|
Post subject: Re: hi to all.... Posted: Fri Jul 30, 2010 9:41 pm |
|
Joined: Sun May 16, 2010 3:53 am Posts: 252 Location: New York
|
salboaski wrote: Also, it's a bit unconventional with a while loop, as it makes it a little harder to review the code. I've found that typically break lines are used to stop for loops after its iterated through an array where your code is looking for a specific value at a specific index. . I'm sorry but no it's not unconventional to use a break with a while loop. I've seen break statements used way more with while loops than with for loops. It is called a loop and a half and I use them all the time. You don't know when a condition is going to end so you use a break statement when finally that condition is met to get out of the loop. For loops you do know when to leave the loop so you have a predetermined max end condition. There is no need to break. Code: try{ BufferedReader br = new BufferedReader( new InputStreamReader(System.in) ); while(true){
String line = br.readLine(); if(line == null){ break; } //Otherwise do something else continously such as parse the line and create an object or something
}
cesponj, Why are you parsing the read in line twice? Code: num=Float.parseFloat(input.readLine());
|
|
 |
|
 |
|
sh0x
|
Post subject: Re: hi to all.... Posted: Sat Jul 31, 2010 9:52 pm |
|
Joined: Mon May 17, 2010 3:52 am Posts: 1130 Location: Munich
|
|
for the record: while loop: you do not know how often you wanna do something, for loop: you know exactly how often you wanna do something, do while loop: you dont know how often you wanna do something but you wanna do it at least once break statement: its not unusual in any of them
@yasuodancez: hes not reading in the same thing twice, input is the reader, like with the scanner class, readLine will process one whole line and then the next and the next and the next etc etc, the first one (num =...) is for how often the loop is to be executed
@topic: there is one problem in your code, you define the float array with a fixed size of 50, while reading [num] times, so if the user would enter 51 you get an ArrayIndexOutOfBoundsException it should be: float [] stud = new float [num]; another thing is that you are defining num as a float too, its just a counter variable so it should be an integer, but thats not such a big deal i guess :>
|
|
 |
|
 |
|
yasuodancez
|
Post subject: Re: hi to all.... Posted: Sat Jul 31, 2010 11:26 pm |
|
Joined: Sun May 16, 2010 3:53 am Posts: 252 Location: New York
|
sh0x wrote: @yasuodancez: hes not reading in the same thing twice, input is the reader, like with the scanner class, readLine will process one whole line and then the next and the next and the next etc etc, the first one (num =...) is for how often the loop is to be executed
Ok i know what input and readline does you don't have to explain that to me. I didn't look close enough to see it doing something different. It will also help if users use the code tags so we can see the code formatted better.
|
|
 |
|
 |
|