Ask your own question, for FREE!
Computer Science 63 Online
OpenStudy (sadicko):

1. Kyle is working on a loop that will execute several lines of code multiple times within a program. Each time the code executes the program should prompt the user to enter in a test grade. He is debating between the following two program statement setups A. for (int index = 0; grade < index; index++) print(“Please enter another grade); grade= keyboard.readDouble(); B. for (int index = 0; grade < index; index++) { print (“Please enter another grade); grade= keyboard.readDouble(); } what must kyle do

OpenStudy (woodrow73):

Neither of your choices are particularly good; it's best to let the user decide when to quit by entering what's called a 'sentinel' value; in my program the loop stops when the user enters -1. I added some other functionality, like adding all the entered grades into an ArrayList, so I can store the data and manipulate later how I'd like.. I also added input validation to make sure the entered value is a double, and added in helpful messages to help the user understand how to use it. ``` import java.util.ArrayList; import java.util.Scanner; public class LoopGrade { public static void main(String[] args) { double grade = 0; Scanner keyboard = new Scanner(System.in); ArrayList<Double> al = new ArrayList<Double>(); while(grade != -1) { System.out.println("Enter a grade: (or enter -1 to end program)"); String input = keyboard.nextLine(); if(isDouble(input)) { grade = Double.parseDouble(input); if(grade != -1) { al.add(grade); System.out.println("Grade \'" + String.valueOf(grade) + "\' successfully entered."); } else System.out.println("Exiting loop"); } else { //if not a double, tell user the error and continue back at the top of loop System.out.println("Error; what you entered was not a double value."); continue; } } keyboard.close(); //manipulate list data as you please.. I'll just loop and display it if(al.size() == 0) System.out.println("Error; you didn't input any grades."); else { StringBuilder sb = new StringBuilder(); double sum = 0; for(int i = 0; i < al.size(); i++) { sum += al.get(i); if(i != al.size()-1) sb.append(String.valueOf(al.get(i)) + ", "); else sb.append(String.valueOf(al.get(i)) + "."); } System.out.println("Grades entered:\n" + sb.toString()); System.out.println("Grade Average:\n"+String.valueOf(sum/(double)al.size())); } } /**Tests whether a String is a valid double value * @return boolean, true if the entered String is a valid double-value */ public static boolean isDouble(String dub) { try{ Double.parseDouble(dub); return true; }catch(NumberFormatException e) { return false; } } } ```

OpenStudy (anonymous):

Erm...this sounded like a simple multiple choice question and not an actual 'how should I implement this' - the concept they're trying to teach here (I believe) is scoping. The formatting of the question is a bit rough (likely due to the way it was pasted into this site)... To answer the OP's question: B. The scope of the loop defined in 'B' is clearly shown with the brackets attached to said loop statement and without them, the scope of the loop only goes to the end of a single statement (and not the entire block). The reason is that in 'A', the scope of the loop only applies to the print statement and not the grade assignment (keyboard.readDouble() is out of scope of the loop in 'A'). As such, this would have a couple of consequences: 1. The loop would be a tight loop with no breaks (likely resulting in a stint of high CPU usage, albeit brief) 2. Because of #1, 'Please enter another grade' would be printed very quickly, over and over, without pause until the very end where the user would only be able to enter one grade. In 'B' you can clearly see the scope of the loop is defined so that with each print statement, there is a readDouble statement to grab the user input.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!