There are three if statements for changeDial that print and add value to the appropriate changeDial, (positiveChangeDial, negativeChangeDial and noChangeDial). These statements are repeated for each of the different 5 currentDial if statements. Is there a way to create a method for the three if conditional statements. Looking to avoid repetition.
import java.util.Scanner;
public class Project_5 {
public static void main(String[] args) {
// Declaration
Scanner input = new Scanner(System.in);
final int SENTINEL = -1;
int currentDial = 0, previousDial = 3, changeDial = 0;
int value1 = 0, value2 = 0, value3 = 0, value4 = 0, value5 = 0;
int negativechangeDial = 0, positivechangeDial = 0, nochangeDial = 0;
// Greeting
System.out.println(
"Response Dial Simulator \n" + "-----------------------\n" + "Initial setting: " + previousDial);
// Execution Loop
while (currentDial != SENTINEL) {
System.out.println("Enter the next setting (1-5) or -1 to stop.");
currentDial = input.nextInt();
changeDial = currentDial - previousDial;
// 1 Response Entered
if (currentDial == 1) {
value1++;
if (changeDial < 0) {
System.out.println("Negative change: " + previousDial + " to " + currentDial);
System.out.println("Current setting: " + currentDial);
negativechangeDial++;
previousDial = currentDial;
}
if (changeDial == 0) {
System.out.println("No change: " + previousDial + " to " + currentDial);
System.out.println("Current setting: " + currentDial);
positivechangeDial++;
previousDial = currentDial;
}
if (changeDial > 0) {
System.out.println("Positive change: " + previousDial + " to " + currentDial);
System.out.println("Current setting: " + currentDial);
nochangeDial++;
previousDial = currentDial;
}
}
// 2 Response Entered
if (currentDial == 2) {
value2++;
if (changeDial < 0) {
System.out.println("Negative change: " + previousDial + " to " + currentDial);
System.out.println("Current setting: " + currentDial);
negativechangeDial++;
previousDial = currentDial;
}
if (changeDial == 0) {
System.out.println("No change: " + previousDial + " to " + currentDial);
System.out.println("Current setting: " + currentDial);
positivechangeDial++;
previousDial = currentDial;
}
if (changeDial > 0) {
System.out.println("Positive change: " + previousDial + " to " + currentDial);
System.out.println("Current setting: " + currentDial);
nochangeDial++;
previousDial = currentDial;
}
}
// 3 Response Entered
if (currentDial == 3) {
value3++;
if (changeDial < 0) {
System.out.println("Negative change: " + previousDial + " to " + currentDial);
System.out.println("Current setting: " + currentDial);
negativechangeDial++;
previousDial = currentDial;
}
if (changeDial == 0) {
System.out.println("No change: " + previousDial + " to " + currentDial);
System.out.println("Current setting: " + currentDial);
nochangeDial++;
previousDial = currentDial;
}
if (changeDial > 0) {
System.out.println("Positive change: " + previousDial + " to " + currentDial);
System.out.println("Current setting: " + currentDial);
positivechangeDial++;
previousDial = currentDial;
}
}
// 4 Response Entered
if (currentDial == 4) {
value4++;
if (changeDial < 0) {
System.out.println("Negative change: " + previousDial + " to " + currentDial);
System.out.println("Current setting: " + currentDial);
negativechangeDial++;
previousDial = currentDial;
}
if (changeDial == 0) {
System.out.println("No change: " + previousDial + " to " + currentDial);
System.out.println("Current setting: " + currentDial);
nochangeDial++;
previousDial = currentDial;
}
if (changeDial > 0) {
System.out.println("Positive change: " + previousDial + " to " + currentDial);
System.out.println("Current setting: " + currentDial);
positivechangeDial++;
previousDial = currentDial;
}
}
// 5 Response Entered
if (currentDial == 5) {
value5++;
if (changeDial < 0) {
System.out.println("Negative change: " + previousDial + " to " + currentDial);
System.out.println("Current setting: " + currentDial);
negativechangeDial++;
previousDial = currentDial;
}
if (changeDial == 0) {
System.out.println("No change: " + previousDial + " to " + currentDial);
System.out.println("Current setting: " + currentDial);
nochangeDial++;
previousDial = currentDial;
}
if (changeDial > 0) {
System.out.println("Positive change: " + previousDial + " to " + currentDial);
System.out.println("Current setting: " + currentDial);
positivechangeDial++;
previousDial = currentDial;
}
}
// -1 Response Entered Quit
if (currentDial == SENTINEL) {
// Response Greeting
System.out.print("\nResponse Summary \n" + "----------------\n");
// Count Break Down
System.out.println("1 was chosen " + value1 + " time(s). \n" + "2 was chosen " + value2 + " time(s). \n"
+ "3 was chosen " + value3 + " time(s). \n" + "4 was chosen " + value4 + " time(s). \n"
+ "5 was chosen " + value5 + " time(s). \n");
// +/- Breakdown
System.out.println("Positive change: " + positivechangeDial + "\n" + "Negative change: "
+ negativechangeDial + "\n" + "No change: " + nochangeDial);
}
}
input.close();
}
}
Aucun commentaire:
Enregistrer un commentaire