Home > database >  I just beginning to learn java, and it seem like I have created an infinite loop here. I cant seem t
I just beginning to learn java, and it seem like I have created an infinite loop here. I cant seem t

Time:02-27

Project Summary: Response dial is always set to one of five settings: 1, 2, 3, 4, or 5. When a dial is handed to an audience member, it is initially set to 3, which indicates a neutral response. If the audience member has a positive reaction to what they are hearing, they can turn the dial to 4 or 5, which indicate agree and strongly agree respectively. Conversely, if they have a negative reaction, they can turn the dial to 2 or 1, which indicate disagree and strongly disagree.

Each response dial periodically checks its dial setting and compares it to the previous setting. The dial records the total number of times that the user choses each of the five settings. It also records the total number of times that the setting changes in a positive direction (e.g., 2 to 3), a negative direction (e.g., 5 to 3), or no change (e.g., 1 to 1).

Response dial data can be used to evaluate things like the persuasiveness of a campaign speech. For example, if an audience member chooses 4 or 5 many times and rarely changes the dial, they were probably persuaded by the speech. On the other hand, if the dial had many positive and negative changes, the audience member was probably conflicted about the candidate’s message.

Please help me fix this infinite loop.

**import java.util.Scanner;
public class Project5 {
    public static void main(String[]args) {
        
        System.out.println("Response Dial Simulator");
        System.out.println("-----------------------");
        
        final int SENTINEL = -1; //set -1 as an stop for the loop
        Scanner keyboard = new Scanner(System.in); //create a scanner
        int initialSetting = 3; // set initial setting
        
        // Priming Read
        System.out.println ("Initial setting: "   initialSetting);
        System.out.println ("Enter the next setting (1-5) or -1 to stop.");
        int nextSetting = keyboard.nextInt(); // take the user input for setting
        
        int currentSetting = initialSetting;
        // declare time chosen each number
        int timeChosen1 = 0;
        int timeChosen2 = 0;
        int timeChosen3 = 0;
        int timeChosen4 = 0;
        int timeChosen5 = 0;
        // declare changes
        int positiveChange = 0;
        int negativeChange = 0;
        int noChange = 0;
        
        while (nextSetting != SENTINEL) {
            
            if (nextSetting > currentSetting) {
            System.out.println ("Positive change: "   currentSetting   " to "   nextSetting);
            System.out.println ("Current setting: "   nextSetting);
            System.out.println ("Enter the next setting (1-5) or -1 to stop.");
            positiveChange  ;
            }
            
            else if (nextSetting < currentSetting) {
            System.out.println ("Negative change: "  currentSetting   " to "   nextSetting);
            System.out.println ("Current setting: "   nextSetting);
            System.out.println ("Enter the next setting (1-5) or -1 to stop.");
            negativeChange  ;
            
            }
            else {
                System.out.println("No change: "   currentSetting   " to "   nextSetting);
                System.out.println ("Current setting: "   nextSetting);
                System.out.println ("Enter the next setting (1-5) or -1 to stop.");
                noChange  =1;
                
                }
            
            
            System.out.println ("Response Summary");
            System.out.println ("----------------");
            
            
            if (nextSetting == 1) {
                timeChosen1  =1;
            }
            else if (nextSetting == 2) {
                timeChosen2  =1;
            }
            else if (nextSetting == 3) {
                timeChosen3  =1;
            }
            else if (nextSetting == 4) {
                timeChosen4  =1;
            }
            else if (nextSetting == 5) {
                timeChosen5  =1;
            }
            
        
            System.out.println ("1 was chosen "   timeChosen1   " time(s).");
            System.out.println ("2 was chosen "   timeChosen2   " time(s).");
            System.out.println ("3 was chosen "   timeChosen3   " time(s).");
            System.out.println ("4 was chosen "   timeChosen4   " time(s).");
            System.out.println ("5 was chosen "   timeChosen5   " time(s).");
            System.out.println ("\nPositive changes: "   positiveChange);
            System.out.println ("Negative changes: "   negativeChange);
            System.out.println ("No changes: "   noChange);
        }**

CodePudding user response:

Your loop depends on the variable nextSetting to know when to stop, but you only update this variable's value once before the loop starts. At the end of your loop, you need to set nextSetting to the user's next input.

CodePudding user response:

You have the response summary inside the loop, and you never take the user input except for the first time. I'm also pretty sure that the end of the code is missing some closing brackets.

  • Related