如何将变量添加到已设置的另一个变量

时间:2019-07-11 16:09:22

标签: java

我的作业是创建一个程序,该程序接受一个数字列表并打印出最高的数字,该数字可被四分之一。

列表如下所示:

12  
16  
87  
58  
25  
73  
86  
36  
79  
40  
12  
89  
32  

输入应为:
40,因为它是那里可以除以四的最高数字。

这是我的代码:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int saved = 0;
        int saved2 = 0;

        for (int i = scanner.nextInt(); i % 4 == 0; i = scanner.nextInt()) {
            for (boolean bull = true; bull == true; bull ^= true) {
                if (i > saved) {
                    saved -= saved2;
                    saved += i;
                    saved2 += i;
                }
            }

            System.out.println(saved);
        }
    }
}

我的代码输入为

12  
16  

我真的不明白为什么要这么做,但是在我看来我把变量加错了。有关添加变量的作业页面未指定如何相互添加变量。
是否有人有技巧来改进代码,或者找到一种方法来修复我的代码?谢谢。

3 个答案:

答案 0 :(得分:1)

欢迎使用Java。

首先,您说的是输入,但就是输出。输入是您输入的内容,输出是您得到打印的内容。

然后,您的for循环中有一个错误。您在一处发生了太多事情。通过实现的逻辑,只要输入的值不能被4除,程序就会退出循环的第一级。

如果您想了解更多https://www.learnjavaonline.org/en/Loops,请阅读for循环。

我建议从while循环开始。逻辑上应该是这样的:

1。创建变量以保存正确的答案saved

2。创建另一个以保存从控制台i读取的值

3。以条件while开始i = scanner.nextInt()循环

3.1检查刚刚输入的值i是否可除以4

3.2(如果是),则比较它是否大于之前保存的值(最初saved的值将为0)

3.3(如果较大),则将读取值i分配给saved

4。在循环结束时,您的saved变量中的最高数字将被四除。打印它。

答案 1 :(得分:0)

我会提供一些帮助,
How do I ask and answer homework questions?

for (int i = scanner.nextInt(); i % 4 == 0;i = scanner.nextInt())

只要所有输入都可以被4整除,它就读取,这就是为什么它以16结尾的原因,因为87不能被4整除。

for (boolean bull = true; bull == true ;bull ^= true)

这需要您解释,但是我很确定它无条件地只执行一次内部循环的主体。 (不能100%地确定,因为truefalse的表示形式在您的计算机中可能很奇怪。如果0是true的表示形式,即确实很奇怪,则这是一个无穷循环,它与您描述的输出不匹配...)

System.out.println(saved);

每个输入只执行一次,最后一个输入除外,后者不是4的倍数。

saved的值与输入相同,只要它在增加即可。

这些提示说明了意外的输出。
如果您仔细检查问题的根源,则应该可以改进编码尝试。

答案 2 :(得分:0)

这就是我如何快速固定您的代码的方式。 请注意,没有关于可能的最小值以及如何停止输入的声明。因此,该解决方案非常简单,只需读取输入,直到那里存在整数即可。

This article对于处理Scanner的输入可能很有用。

我希望代码中的注释会有所帮助。如有任何疑问,请添加评论。祝你好运!

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int currentMax = Integer.MIN_VALUE; // you may set negative or 0 if you know that all the input is positive
//      int saved2 = 0; // no need for this variable

        while (scanner.hasNextInt()) { // you can make a better input handling, especially if you know when it should end the input. Now it will end on any non-integer input line
            int i = scanner.nextInt();

            //      for (int i = scanner.nextInt(); i % 4 == 0; i = scanner.nextInt()) {
//          for (boolean bull = true; bull == true; bull ^= true) {
                if (((i % 4) == 0) && (i > currentMax)) {
                    currentMax = i;
//                  saved -= saved2;
//                  saved += i;
//                  saved2 += i;
//              }
            }
        }

        System.out.println(currentMax); // moved out of "for" or "while" cycles. Print the value after the input has ended.
    }
}