0 Members and 2 Guests are viewing this topic.
import javax.swing.JOptionPane;public class while 2 { public static void main(String[] args) { int scores = 0; //scoretemp parsed as int here. int temp = 0; //temp var to compare lowest score int low = 0; //not sure about this, lowest score variable anyhow. for (int i=1;i>0;i++) { String scoretemp = JOptionPane.showInputDialog("Enter your test score (-1 to exit):"); int score = Integer.parseInt(scoretemp); //scoretemp is converted to an integer if (score = -1) { //if -1 is entered then.. break; //exit. [this will quit the whole loop.] } if (score >= 0){ //if test score is equal to 0 or greater than 0... //actual code here } }}}
for (i=1;i>0;i++);
public class average {public static void main(String []args) {public Arraylist<Integer> grades;double total=0.0;int minGrade=Integer.MAX_VALUE;int minIndex=-5;//Read input, adding each grade to the list with grades.add(new Integer(grade_value));for(int x=0;x<grades.size();x++) {if(grades.get(x).intValue()<minGrade) { minIndex=x;minGrade=grades.get(x).intValue();}}grades.remove(minIndex);for(int x=0;x<grades.size();x++) total+=grades.get(x).intValue();System.out.println(total/grades.size());}}
import javax.swing.JOptionPane;public class while2 { public static void main(String[] args) { int temp = 0; //temp var to compare lowest score int low = 0; //not sure about this, lowest score variable anyhow. initialization. int acc = 0; int total = 0; //total. boolean fail = false; int score = 0; if (score < 0) { //if -1 is entered then.. fail = true; //Some kind of failure code goes here. } else{ String scoretemp = JOptionPane.showInputDialog("Enter your test score (-1 to exit):"); score = Integer.parseInt(scoretemp); low = score; //assignment takes place <-- } while (!fail) { String scoretemp = JOptionPane.showInputDialog("Enter your test score (-1 to exit):"); score = Integer.parseInt(scoretemp); //scoretemp is converted to an integer if (score < 0) { //if -1 is entered then.. break; } else{ if (score < low) { //compares 0 to the score :S total += low; low = score; } else{ total += score; } } acc += 1; } System.out.print(low); //tests low to make sure of correct output}}
int sum = 0;int numScores = 0;int lowest = 101; //assuming 100 is the highest score possiblewhile(true){ String temp = JOptionPane.showInputDialog("Enter test score (negative to exit): "); int score = Integer.parseInt(temp); if(score < 0) break; numScores++; if(score < lowest) lowest = score; sum += score;}System.out.println("Average: " + ((sum - lowest) / (numScores - 1)));
You can get all your information from those three things. You shouldn't need any loops for this.
public static void main(String[] args){ int[] answers = recursion(0, 101, 0); System.out.println("Average: " + ((answers[2] - answers[1]) / (answers[0] - 1)));}public static int[] recursion(int numScores, int lowest, int sum){ String temp = JOptionPane.showInputDialog("Enter test score (negative to exit): "); int score = Integer.parseInt(temp); if(score < 0) return new int[] {numScores, lowest, sum}; return recursion(numScores + 1, score < lowest ? score : lowest, sum + score);}
Code: [Select]int sum = 0;int numScores = 0;int lowest = 101; //assuming 100 is the highest score possiblewhile(true){ String temp = JOptionPane.showInputDialog("Enter test score (negative to exit): "); int score = Integer.parseInt(temp); if(score < 0) break; numScores++; if(score < lowest) lowest = score; sum += score;}System.out.println("Average: " + ((sum - lowest) / (numScores - 1)));
A program that will read integer test scores from the keyboard until a negative value is typed in. The program will drop the lowest score from the total and print the average of the remaining scores.
inputs = []while True: n = input() if n<0: break inputs.append(n)lowest = inputs[0]for i in range(0,len(inputs)): if inputs[i]<=lowest: lowest=inputs[i] numberOfValues = len(inputs)sumOfValues = 0for i in range(0,len(inputs)): sumOfValues+=inputs[i] print "Average: " + str(sumOfValues/numberOfValues)print "Lowest: " + str(lowest)
inputs = []while True: n = input() if n<0: break inputs.append(n)lowest = inputs[0]sumOfValues = 0for i in range(0,len(inputs)): if inputs[i]<=lowest: lowest=inputs[i] sumOfValues+=inputs[i] print "Average: %d\nLowest: %d" % (sumOfValues/len(inputs),lowest)
grades = []n = lowest = float("inf") # 'inf' stands for infinity - this actually works.while n > 0: n = float(input("Grade: ")) if n > 0: if n < lowest: lowest = n grades.append(n)for i in range(grades.count(lowest)): grades.remove(lowest) print("Average = {0}\nLowest = {1}".format(sum(grades)/len(grades), lowest))