The while loop is supposed to take in the numbers via JOptionPane (with a WHILE loop) and summing the score. The problem is that I don't know how or where to place the WHILE loop. I have listed my program below. Please help.
import java.util.*;
import javax.swing.*;
public class Assignment3
{
public int readInput()
{
String firstNumber;
firstNumber =
JOptionPane.showInputDialog( "Enter first number" );
int number1;
number1 = Integer.parseInt( firstNumber );
String secondNumber;
secondNumber =
JOptionPane.showInputDialog( "Enter second number" );
int number2;
number2 = Integer.parseInt( secondNumber );
String thirdNumber;
thirdNumber =
JOptionPane.showInputDialog("Enter third number:");
int number3;
number3 = Integer.parseInt( thirdNumber );
String fourthNumber;
fourthNumber =
JOptionPane.showInputDialog( "Enter fourth number" );
int number4;
number4 = Integer.parseInt( fourthNumber );
String fifthNumber;
fifthNumber =
JOptionPane.showInputDialog( "Enter fifth number" );
int number5;
number5 = Integer.parseInt( fifthNumber );
int sum;
sum = number1 + number2 + number3 + number4 + number5;
}
public char gradeTest(int sum)
{
char grade;
if (sum %26gt;=90)
grade = 'A';
else if (sum %26gt;= 80)
grade = 'B';
else if (sum %26gt;= 70)
grade = 'C';
else if (sum %26gt;= 60)
grade = 'D';
else
grade = 'F';
}
public void writeOutput(int sum, char grade)
{
JOptionPane.showMessageDialog(
null, "The sum of these numbers is " + sum );
JOptionPane.showMessageDialog(
null, "Your final grade is = " + grade );
}
public static void main(String[] args)
{
Assignment3 a3 = new Assignment3();
int sum = readInput();
char grade = gradeTest(sum);
writeOutput(sum,grade);
}
}
}
I am a taking a class on beginning Java programming and I have to include a while loop.?
Hi,
Here's some example of while loop. Hope it helps. Sorry, the editor yahoo provides doesn't allow formatting.
package com.somesoft.core.utils.cui;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
*
* @author SuchittoPalit(PAUL)
*/
public class ScannerCUI {
// This method returns multiple word Character input as String.
// Checks whether the String is character or number.
// A Scanner is not safe for multithreaded use without
//external synchronization.
public static synchronized String getCharacters(String prompt) {
Scanner conin = new Scanner(System.in);
String strInput = "";
while(strInput.length() == 0) {
System.out.println( prompt );
if(!conin.hasNextInt()) { // hasNextInt() blocks
strInput = conin.nextLine(); // nextLine() and
//hasNextLine() also blocks
// but blank lines are also considered as lines by them.
}
else {
strInput = conin.nextLine();
strInput ="";
System.out.println( "YOU MUST ENTER CHARACTERS, NOT NUMBERS.");
}
}
return strInput;
}
// This method returns single integer input both +ve and -ve .
// If characters, non integers are entered at the prompt the
// program
// discards the character and prompts again for integer.
public static synchronized int getInts(String prompt) {
Scanner conin = new Scanner(System.in);
String intInput = "";
while(intInput.length() == 0) {
try {
System.out.println( prompt );
if (conin.hasNext()) { // hasNext() blocks
if (conin.hasNextInt())
intInput = conin.next();
else {
intInput = conin.nextLine();
intInput = "";
}
}
} catch (InputMismatchException imme) {
imme.printStackTrace();
}
}
return Integer.parseInt(intInput);
}
// For StandAlone Testing
public static void main (String[] args) {
String answer;
int i;
// This block allows stand-alone testing of the
// getCharacters()method
do {
System.out.println("#####FROM MAIN");
answer = getCharacters("Enter Names(enter exit to end): ");
if (! answer.equals("exit")) {
System.out.println("You said: " + answer);
}
} while (! answer.equals("exit")) ;
// This block allows stand-alone testing of the getInts()ethod
do {
System.out.println("#####FROM MAIN");
i = getInts("Enter Numbers(enter 0 to end): ");
if ( i != 0){
System.out.println("You said: " + i);
}
} while (i != 0) ;
}
}
Reply:You need to put while in the function readinput()
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment