怎么避免重复创建if语句

来源:爱站网时间:2022-04-26编辑:网友分享
本篇文章主要来介绍下“怎么避免重复创建if语句”的相关内容,感兴趣的朋友可以跟随爱站技术频道小编来了解了解,如果你觉得文章不错可以分享给有需要的朋友。

问题描述


存在三个changeDial的if语句,这些语句打印并向适当的changeDial添加值(positiveChangeDial,negativeChangeDial和noChangeDial)。对5个不同的currentDial if语句中的每一个重复这些语句。有没有一种方法可以为三个if条件语句创建方法。为了避免重复。

   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("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("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("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("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("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();
    }
}

思路一:


这是很多代码,但是确实可以简化很多。我会用int[]代表[[开始。并且我将通过交换nochangeDialpositivechangeDial计数器来修复错误。并且在修改任何计数器之前,您应该测试SENTINEL。另外,您的评论似乎有些杂音。类似,Scanner input = new Scanner(System.in); final int SENTINEL = -1; int currentDial = 0, previousDial = 3, changeDial = 0; int[] dials = new int[5]; int negativechangeDial = 0, positivechangeDial = 0, nochangeDial = 0; System.out.println("Response Dial Simulator \n" + "-----------------------\n" + "Initial setting: " + previousDial); while (currentDial != SENTINEL) { System.out.println("Enter the next setting (1-5) or -1 to stop."); currentDial = input.nextInt(); if (currentDial == SENTINEL) { System.out.print("\nResponse Summary \n" + "----------------\n"); System.out.println("1 was chosen " + dials[0] + " time(s). \n" + "2 was chosen " + dials[1] + " time(s). \n" + "3 was chosen " + dials[2] + " time(s). \n" + "4 was chosen " + dials[3] + " time(s). \n" + "5 was chosen " + dials[4] + " time(s). \n"); System.out.println("Positive change: " + positivechangeDial + "\n" + "Negative change: " + negativechangeDial + "\n" + "No change: " + nochangeDial); break; } changeDial = currentDial - previousDial; dials[currentDial - 1]++; if (changeDial

看完怎么避免重复创建if语句的文章内容后,相信不少小伙伴都知道怎么处理这种问题了,想要获取更多技术文章内容,可以随时来我们爱站技术频道网站翻阅相关文章。

上一篇:ID更新SQLite特定字段的方法

下一篇:Java之TreeMap的初始大小介绍

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载