Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,420 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,344 people online right now. Registration is fast and FREE... Join Now!




Program with Sentinel

 
Reply to this topicStart new topic

Program with Sentinel, Double post... topics merged

mse12
14 Oct, 2008 - 04:00 PM
Post #1

D.I.C Head
**

Joined: 21 Sep, 2008
Posts: 55

I am supposed to create a program that evaluates Letter Grades for specified scores such that the user does not enter the number of grades in advance. Instead, the user enters a sentinel value in order to stop entering grades. There should be a maximum limit of 100 grades, after which no more grades can be entered. I displayed a warning about the maximum. The problem is that the programmer does not know in advance how many iterations are necessary for the user-input loop. I am allowed to use whichever loop type I prefer. I ave everything done in the problem except I'm not entirely sure how to get the loop to terminate when a negative value is entered because it needs to terminate AND still display the result of all prior user input. I think I need to use something such as:

while(i !< 0);

Here is my code at the present: Any help would be greatly appreciated!

CODE


import javax.swing.JOptionPane;
public class AssignGrade {
public static void main(String[] args) {
    int[] scores = new int[100]; // Array scores
    int best = 0; // The best score
    char grade; // The grade
    // Read scores and find the best score
    for (int i = 0; i < 100; i++) {
      String scoreString = JOptionPane.showInputDialog("Please note that the maximum number of students is 100. \n Please enter a score (negative to quit):");
      // Convert string into integer
      scores[i] = Integer.parseInt(scoreString);
      if (scores[i] > best)
         best = scores[i];}
    // Declare and initialize output string
    String output = "";
    // Assign and display grades
    for (int i = 0; i < 100; i++) {
      if (scores[i] >= best - 10)
        grade = 'A';
      else if (scores[i] >= best - 20)
        grade = 'B';
      else if (scores[i] >= best - 30)
        grade = 'C';
      else if (scores[i] >= best - 40)
        grade = 'D';
      else
        grade = 'F';
      output += "Student " + i + " score is " +
      scores[i] + " and grade is " + grade + "\n";
    }
    // Display the result
    JOptionPane.showMessageDialog(null, output);
    }
  }




User is offlineProfile CardPM
+Quote Post

stauffski
RE: Program With Sentinel
14 Oct, 2008 - 05:18 PM
Post #2

D.I.C Head
**

Joined: 3 Nov, 2007
Posts: 139



Thanked: 16 times
My Contributions
You can use a break. Test for the -1 before you insert it into the loop. Also, create a variable that keeps track of the number of times the user inputs. This way you won't output a bunch of meaningless lines. Below will clarify:

java

import javax.swing.JOptionPane;
public class AssignGrade {
public static void main(String[] args) {
int[] scores = new int[100]; // Array scores
int best = 0; // The best score
int index;//variable to keep track of number of inputs
char grade; // The grade
// Read scores and find the best score
for (int i = 0; i < 100; i++) {
String scoreString = JOptionPane.showInputDialog("Please note that the maximum number of students is 100. \n Please enter a score (negative to quit):");
// Convert string into integer
if(Integer.parseInt(scoreString) == -1){
index = i; //last input index
break;
}
scores[i] = Integer.parseInt(scoreString);
if (scores[i] > best)
best = scores[i];}
// Declare and initialize output string
String output = "";
// Assign and display grades
for (int i = 0; i < index; i++) {//changed to index
if (scores[i] >= best - 10)
grade = 'A';
else if (scores[i] >= best - 20)
grade = 'B';
else if (scores[i] >= best - 30)
grade = 'C';
else if (scores[i] >= best - 40)
grade = 'D';
else
grade = 'F';
output += "Student " + i + " score is " +
scores[i] + " and grade is " + grade + "\n";
}
// Display the result
JOptionPane.showMessageDialog(null, output);
}
}


That should work for you. If you have any other questions just ask. Good Luck!
User is offlineProfile CardPM
+Quote Post

mse12
RE: Program With Sentinel
14 Oct, 2008 - 06:17 PM
Post #3

D.I.C Head
**

Joined: 21 Sep, 2008
Posts: 55

it does not compile due to the fact that index is not initialized within this line:

for (int i = 0; i < index; i++) {//changed to index

any ideas? thanks!
User is offlineProfile CardPM
+Quote Post

mse12
RE: Program With Sentinel
14 Oct, 2008 - 06:39 PM
Post #4

D.I.C Head
**

Joined: 21 Sep, 2008
Posts: 55

i am attempting to keep count of the iterations using the variable index, but when i move it out of its brackets to make it initialized in the loop it needs to be in, it de-initializes the integer i

any help would be greatly appreciated! thanks in advance!

CODE


import javax.swing.JOptionPane;
public class AssignGrade {
public static void main(String[] args) {
    int[] scores = new int[100]; // Array scores
    int best = 0; // The best score
    int index;//variable to keep track of number of inputs
    char grade; // The grade
    // Read scores and find the best score
    for (int i = 0; i < 100; i++) {
      String scoreString = JOptionPane.showInputDialog("Please note that the maximum number of students is 100. \n Please enter a score (negative to quit):");
      // Convert string into integer
      if(Integer.parseInt(scoreString) < 0){
        index = i; //last input index  
        break;
      }
      scores[i] = Integer.parseInt(scoreString);
      if (scores[i] > best)
         best = scores[i];}
    // Declare and initialize output string
    String output = "";
    // Assign and display grades
    for (int i = 0; i < index.length; i++) {
      if (scores[i] >= best - 10)
        grade = 'A';
      else if (scores[i] >= best - 20)
        grade = 'B';
      else if (scores[i] >= best - 30)
        grade = 'C';
      else if (scores[i] >= best - 40)
        grade = 'D';
      else
        grade = 'F';
      output += "Student " + i + " score is " +
      scores[i] + " and grade is " + grade + "\n";
    }
    // Display the result
    JOptionPane.showMessageDialog(null, output);
    }
  }



User is offlineProfile CardPM
+Quote Post

pbl
RE: Program With Sentinel
14 Oct, 2008 - 08:58 PM
Post #5

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
int index = 0; //variable to keep track of number of inputs

that should let the compiler think that you know what you are doing
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 01:08PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month